bool BinarySerializationContext_init(BinarySerializationContext * self, bool bigEndian) {
    call_super(init, self);

    self->jmpBuf = NULL;
    self->status = SERIALIZATION_ERROR_OK;

    self->bigEndian = bigEndian;
    self->memwriteContext = memwriteContextInit(NULL, 0, 0, true);
    memwrite(&self->memwriteContext, 4, self->bigEndian ? "Stem" : "metS");
    self->containerCount = 0;
    self->containerListSize = 1;
    self->containerStack = malloc(sizeof(struct BinarySerializationContext_containerNode) * self->containerListSize);
    self->finished = false;

    self->dispose = BinarySerializationContext_dispose;
    self->errorString = BinarySerialization_errorString;
    self->beginStructure = BinarySerializationContext_beginStructure;
    self->beginDictionary = BinarySerializationContext_beginDictionary;
    self->beginArray = BinarySerializationContext_beginArray;
    self->endStructure = BinarySerializationContext_endStructure;
    self->endDictionary = BinarySerializationContext_endDictionary;
    self->endArray = BinarySerializationContext_endArray;
    self->writeBoolean = BinarySerializationContext_writeBoolean;
    self->writeInt8 = BinarySerializationContext_writeInt8;
    self->writeUInt8 = BinarySerializationContext_writeUInt8;
    self->writeInt16 = BinarySerializationContext_writeInt16;
    self->writeUInt16 = BinarySerializationContext_writeUInt16;
    self->writeInt32 = BinarySerializationContext_writeInt32;
    self->writeUInt32 = BinarySerializationContext_writeUInt32;
    self->writeInt64 = BinarySerializationContext_writeInt64;
    self->writeUInt64 = BinarySerializationContext_writeUInt64;
    self->writeFloat = BinarySerializationContext_writeFloat;
    self->writeDouble = BinarySerializationContext_writeDouble;
    self->writeEnumeration = BinarySerializationContext_writeEnumeration;
    self->writeBitfield8 = BinarySerializationContext_writeBitfield8;
    self->writeBitfield16 = BinarySerializationContext_writeBitfield16;
    self->writeBitfield32 = BinarySerializationContext_writeBitfield32;
    self->writeBitfield64 = BinarySerializationContext_writeBitfield64;
    self->writeString = BinarySerializationContext_writeString;
    self->writeBlob = BinarySerializationContext_writeBlob;
    return true;
}
Exemplo n.º 2
0
int main(int argc, char * const * argv)
{
	int size = getBufferSize(argc, argv);
	short c = 0;

	meminit(size);
	memattach();

	do {
		errno = 0;
		if ((c = fgetc(stdin)) == (short) EOF && ferror(stdin) != 0)
		{
			fprintf(stderr, "%s: %s\n", appname, strerror(errno));
			memdetach();
			memrmv();
			return EXIT_FAILURE;
		}
		memwrite(c);
	} while (c != EOF);

	memdetach();

	return EXIT_SUCCESS;
}
Exemplo n.º 3
0
static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
{
    memwrite(req->t_msg + req->t_off, addr, len);
    req->t_off += len;
}
Exemplo n.º 4
0
static void cmos_write_mmio(uint8_t reg, uint8_t val)
{
    uint8_t data = val;

    memwrite(base + (uint32_t)reg_base + (uint32_t)reg, &data, 1);
}
Exemplo n.º 5
0
/**
 * Each thread copies memory from a remote memory buffer.
 */
void *buf_copy_func(void *arg)
{
	int i, j;
	size_t size = 0;
	struct buf_copy_data *data = (struct buf_copy_data *) arg;
	long memread_sum = 0;
	int start_entry_idx = 0;

	bind2node_id(data->node_id);
	data->copy_size = 0;
	if (use_remote) {
		/*
		 * We make sure threads on different nodes start to access memory
		 * from different nodes. In the current implementation, a thread
		 * always starts to copy data from the NUMA node whose node id is
		 * right behind its own node id. For example, the order of nodes
		 * visited by a memcpy thread on node 0 is 1, 2, 3; the order by
		 * a thread on node 1 is 2, 3, 0; etc.
		 */
		for (i = 0; i < NUM_NODES - 1; i++) {
			if (data->mode == MEMCPY_PULL || data->mode == MEMCPY_R2R
					|| data->mode == MEMREAD) {
				if (data->copy_entries[i].from.node_id == (data->node_id + 1)
						% NUM_NODES)
					break;
			}
			else if (data->mode == MEMCPY_PUSH || data->mode == MEMWRITE) {
				if (data->copy_entries[i].to.node_id == (data->node_id + 1)
						% NUM_NODES)
					break;
			}
		}
		assert(i != NUM_NODES - 1);
		start_entry_idx = i;
	}
	for (j = 0; j < NUM_COPY; j++)
		for (i = 0; i < NUM_NODES - 1; i++) {
			struct buf_copy_data::copy_entry *entry = &data->copy_entries[(i
					+ start_entry_idx) % (NUM_NODES - 1)];
			int size = entry->to.size;
			if (data->mode == MEMCPY_PULL) {
				if (use_remote) {
					assert(entry->to.node_id == data->node_id);
					assert(entry->from.node_id != data->node_id);
				}
				else {
					assert(entry->to.node_id == data->node_id);
					assert(entry->from.node_id == data->node_id);
				}
				assert(size == entry->from.size);
				memcpy(entry->to.addr, entry->from.addr, size);
			}
			else if (data->mode == MEMCPY_PUSH) {
				if (use_remote) {
					assert(entry->to.node_id != data->node_id);
					assert(entry->from.node_id == data->node_id);
				}
				else {
					assert(entry->to.node_id == data->node_id);
					assert(entry->from.node_id == data->node_id);
				}
				assert(size == entry->from.size);
				memcpy(entry->to.addr, entry->from.addr, size);
			}
			else if (data->mode == MEMCPY_R2R) {
				if (use_remote) {
					assert(entry->to.node_id != data->node_id);
					assert(entry->from.node_id != data->node_id);
				}
				else {
					assert(entry->to.node_id == data->node_id);
					assert(entry->from.node_id == data->node_id);
				}
				assert(size == entry->from.size);
				memcpy(entry->to.addr, entry->from.addr, size);
			}
			else if (data->mode == MEMREAD) {
				assert(entry->to.addr == NULL);
				if (use_remote)
					assert(entry->from.node_id != data->node_id);
				else
					assert(entry->from.node_id == data->node_id);
				memread_sum += memread(entry->from);
			}
			else if (data->mode == MEMWRITE) {
				if (use_remote)
					assert(entry->to.node_id != data->node_id);
				else
					assert(entry->to.node_id == data->node_id);
				assert(entry->from.addr == NULL);
				memwrite(entry->to);
			}
			data->copy_size += size;
		}
	return (void *) memread_sum;
}
Exemplo n.º 6
0
char * JSONEmitter_writeString(struct JSONNode * rootNode, enum JSONEmitterFormat format, size_t * outLength, struct JSONEmissionError * outError) {
	struct memwriteContext context;
	size_t subitemIndex;
	struct JSONNode * containerNode;
	struct nodeStackItem * nodeStack;
	size_t nodeStackAllocatedSize, nodeStackCurrentDepth;
	size_t indentIndex;
	
	context = memwriteContextInit(malloc(1), 0, 1, true);
	
	if (rootNode->type == JSON_TYPE_ARRAY) {
		memwrite(&context, 1, "[");
		
	} else if (rootNode->type == JSON_TYPE_OBJECT) {
		memwrite(&context, 1, "{");
		
	} else {
		free(context.data);
		if (outError != NULL) {
			outError->node = rootNode;
			outError->code = JSONEmissionError_invalidNodeType;
			outError->description = "Invalid root node type";
		}
		return NULL;
	}
	
	nodeStackAllocatedSize = 1;
	nodeStack = malloc(sizeof(struct nodeStackItem) * nodeStackAllocatedSize);
	nodeStackCurrentDepth = 0;
	containerNode = rootNode;
	subitemIndex = 0;
	
	if (format == JSONEmitterFormat_multiLine) {
		memwrite(&context, 1, "\n");
	}
	
	for (;;) {
		if (subitemIndex >= containerNode->value.count) {
			if (format == JSONEmitterFormat_multiLine) {
				if (containerNode->value.count > 0) {
					memwrite(&context, 1, "\n");
				}
				for (indentIndex = 0; indentIndex < nodeStackCurrentDepth; indentIndex++) {
					memwrite(&context, 1, "\t");
				}
			}
			
			if (containerNode->type == JSON_TYPE_ARRAY) {
				memwrite(&context, 1, "]");
				
			} else if (containerNode->type == JSON_TYPE_OBJECT) {
				memwrite(&context, 1, "}");
			}
			
			if (nodeStackCurrentDepth == 0) {
				break;
			}
			
			nodeStackCurrentDepth--;
			containerNode = nodeStack[nodeStackCurrentDepth].node;
			subitemIndex = nodeStack[nodeStackCurrentDepth].index + 1;
			
			continue;
		}
		
		if (subitemIndex > 0) {
			memwrite(&context, 1, ",");
			if (format == JSONEmitterFormat_singleLine) {
				memwrite(&context, 1, " ");
			} else if (format == JSONEmitterFormat_multiLine) {
				memwrite(&context, 1, "\n");
			}
		}
		
		if (format == JSONEmitterFormat_multiLine) {
			for (indentIndex = 0; indentIndex <= nodeStackCurrentDepth; indentIndex++) {
				memwrite(&context, 1, "\t");
			}
		}
		
		if (containerNode->type == JSON_TYPE_OBJECT) {
			char * escapedString;
			size_t length;
			
			if (containerNode->subitems[subitemIndex].key == NULL) {
				free(context.data);
				if (outError != NULL) {
					outError->node = &containerNode->subitems[subitemIndex];
					outError->code = JSONEmissionError_nullKeyString;
					outError->description = "Null key string";
				}
				free(nodeStack);
				return NULL;
			}
			
			memwrite(&context, 1, "\"");
			escapedString = escapeJSONString(containerNode->subitems[subitemIndex].key, containerNode->subitems[subitemIndex].keyLength, &length);
			memwrite(&context, length, escapedString);
			free(escapedString);
			memwrite(&context, 2, "\":");
			if (format != JSONEmitterFormat_compact) {
				memwrite(&context, 1, " ");
			}
		}
		
		if (containerNode->subitems[subitemIndex].type == JSON_TYPE_NUMBER) {
			char formattedNumber[32];
			
			snprintf(formattedNumber, 32, "%.29g", containerNode->subitems[subitemIndex].value.number);
			memwrite(&context, strlen(formattedNumber), formattedNumber);
			
		} else if (containerNode->subitems[subitemIndex].type == JSON_TYPE_STRING) {
			char * escapedString;
			size_t length;
			
			if (containerNode->subitems[subitemIndex].value.string == NULL) {
				free(context.data);
				if (outError != NULL) {
					outError->node = &containerNode->subitems[subitemIndex];
					outError->code = JSONEmissionError_nullValueString;
					outError->description = "Null value string";
				}
				free(nodeStack);
				return NULL;
			}
			
			memwrite(&context, 1, "\"");
			escapedString = escapeJSONString(containerNode->subitems[subitemIndex].value.string, containerNode->subitems[subitemIndex].stringLength, &length);
			memwrite(&context, length, escapedString);
			free(escapedString);
			memwrite(&context, 1, "\"");
			
		} else if (containerNode->subitems[subitemIndex].type == JSON_TYPE_BOOLEAN) {
			if (containerNode->subitems[subitemIndex].value.boolean) {
				memwrite(&context, 4, "true");
			} else {
				memwrite(&context, 5, "false");
			}
			
		} else if (containerNode->subitems[subitemIndex].type == JSON_TYPE_NULL) {
			memwrite(&context, 4, "null");
			
		} else if (containerNode->subitems[subitemIndex].type == JSON_TYPE_ARRAY) {
			if (containerNode->subitems[subitemIndex].value.count > 0 && containerNode->subitems[subitemIndex].subitems == NULL) {
				free(context.data);
				if (outError != NULL) {
					outError->node = &containerNode->subitems[subitemIndex];
					outError->code = JSONEmissionError_nullSubitems;
					outError->description = "Null subitems with nonzero count";
				}
				free(nodeStack);
				return NULL;
			}
			
			if (nodeStackAllocatedSize <= nodeStackCurrentDepth) {
				nodeStackAllocatedSize *= 2;
				nodeStack = realloc(nodeStack, sizeof(struct nodeStackItem) * nodeStackAllocatedSize);
			}
			nodeStack[nodeStackCurrentDepth].node = containerNode;
			nodeStack[nodeStackCurrentDepth].index = subitemIndex;
			nodeStackCurrentDepth++;
			
			memwrite(&context, 1, "[");
			if (format == JSONEmitterFormat_multiLine) {
				memwrite(&context, 1, "\n");
			}
			containerNode = &containerNode->subitems[subitemIndex];
			subitemIndex = 0;
			continue;
			
		} else if (containerNode->subitems[subitemIndex].type == JSON_TYPE_OBJECT) {
			if (containerNode->subitems[subitemIndex].value.count > 0 && containerNode->subitems[subitemIndex].subitems == NULL) {
				free(context.data);
				if (outError != NULL) {
					outError->node = &containerNode->subitems[subitemIndex];
					outError->code = JSONEmissionError_nullSubitems;
					outError->description = "Null subitems with nonzero count";
				}
				free(nodeStack);
				return NULL;
			}
			
			if (nodeStackAllocatedSize <= nodeStackCurrentDepth) {
				nodeStackAllocatedSize *= 2;
				nodeStack = realloc(nodeStack, sizeof(struct nodeStackItem) * nodeStackAllocatedSize);
			}
			nodeStack[nodeStackCurrentDepth].node = containerNode;
			nodeStack[nodeStackCurrentDepth].index = subitemIndex;
			nodeStackCurrentDepth++;
			
			memwrite(&context, 1, "{");
			if (format == JSONEmitterFormat_multiLine) {
				memwrite(&context, 1, "\n");
			}
			containerNode = &containerNode->subitems[subitemIndex];
			subitemIndex = 0;
			continue;
			
		} else {
			free(context.data);
			if (outError != NULL) {
				outError->node = &containerNode->subitems[subitemIndex];
				outError->code = JSONEmissionError_invalidNodeType;
				outError->description = "Invalid node type";
			}
			free(nodeStack);
			return NULL;
		}
		
		subitemIndex++;
	}
	free(nodeStack);
	
	memwrite(&context, 1, NULL);
	if (outLength != NULL) {
		*outLength = context.length - 1;
	}
	return context.data;
}
Exemplo n.º 7
0
//push data to stack
static INLINE void push(u8 data)
{
	memwrite(SP | 0x100,data);
	SP--;
}
Exemplo n.º 8
0
static void pngWriteFnMemwrite(png_structp pngWriteStruct, png_bytep data, png_size_t length) {
	memwrite((struct memwriteContext *) png_get_io_ptr(pngWriteStruct), length, data);
}