Exemplo n.º 1
0
void *(Z_Malloc) (int size, int tag, void *user, const char *file, int line) {
	memblock_t *newblock;
	unsigned char *data;
	void *result;

	if (tag < 0 || tag >= PU_MAX)
		I_Error("Z_Malloc: tag out of range: %i (%s:%d)", tag, file,
			line);

	if (user == NULL && tag >= PU_PURGELEVEL)
		I_Error
		    ("Z_Malloc: an owner is required for purgable blocks (%s:%d)",
		     file, line);

	// Malloc a block of the required size

	newblock = NULL;

	if (!(newblock = (memblock_t *) malloc(sizeof(memblock_t) + size))) {
		if (Z_ClearCache(sizeof(memblock_t) + size))
			newblock =
			    (memblock_t *) malloc(sizeof(memblock_t) + size);
	}

	if (!newblock)
		I_Error("Z_Malloc: failed on allocation of %u bytes (%s:%d)",
			size, file, line);

	// Hook into the linked list for this tag type

	newblock->tag = tag;
	newblock->id = ZONEID;
	newblock->user = user;
	newblock->size = size;

	Z_InsertBlock(newblock);

	data = (unsigned char *)newblock;
	result = data + sizeof(memblock_t);

	if (user != NULL)
		*newblock->user = result;

#ifdef ZONEFILE
	Z_LogPrintf
	    ("* %p = Z_Malloc(size=%lu, tag=%d, user=%p, source=%s:%d)\n",
	     result, size, tag, user, file, line);
#endif

	return result;
}
Exemplo n.º 2
0
void *(Z_Realloc)(void *ptr, int size, int tag, void *user, const char *file, int line) {
    memblock_t *block;
    memblock_t *newblock;
    unsigned char *data;
    void *result;

    if(!ptr) {
        return (Z_Malloc)(size, tag, user, file, line);
    }

    if(size == 0) {
        (Z_Free)(ptr, file, line);
        return NULL;
    }

    if(tag < 0 || tag >= PU_MAX) {
        I_Error("Z_Realloc: tag out of range: %i (%s:%d)", tag, file, line);
    }

    if(user == NULL && tag >= PU_PURGELEVEL) {
        I_Error("Z_Realloc: an owner is required for purgable blocks (%s:%d)", file, line);
    }

    block = (memblock_t*)((byte *)ptr - sizeof(memblock_t));

    newblock = NULL;

    if(block->id != ZONEID) {
        I_Error("Z_Realloc: Reallocated a pointer without ZONEID (%s:%d)", file, line);
    }

    Z_RemoveBlock(block);

    block->next = NULL;
    block->prev = NULL;

    if(block->user) {
        *block->user = NULL;
    }

    if(!(newblock = (memblock_t*)realloc(block, sizeof(memblock_t) + size))) {
        if(Z_ClearCache(sizeof(memblock_t) + size)) {
            newblock = (memblock_t*)realloc(block, sizeof(memblock_t) + size);
        }
    }

    if(!newblock) {
        I_Error("Z_Realloc: failed on allocation of %u bytes (%s:%d)", size, file, line);
    }

    newblock->tag = tag;
    newblock->id = ZONEID;
    newblock->user = user;
    newblock->size = size;

    Z_InsertBlock(newblock);

    data = (unsigned char*)newblock;
    result = data + sizeof(memblock_t);

    if(user != NULL) {
        *newblock->user = result;
    }

#ifdef ZONEFILE
    Z_LogPrintf("* %p = Z_Realloc(ptr=%p, n=%lu, tag=%d, user=%p, source=%s:%d)\n",
                result, ptr, size, tag, user, file, line);
#endif

    return result;
}