Rename memblock_s's size to block_size.

In order to improve the pointer checking, I plan on adding a size field to
represent the requested size.
This commit is contained in:
Bill Currie 2012-11-19 21:35:27 +09:00
parent 49ad301d3d
commit 8ee98e1ef5

View file

@ -71,7 +71,7 @@ static qboolean Cache_FreeLRU (void);
typedef struct memblock_s typedef struct memblock_s
{ {
int size; // including the header and possibly tiny fragments int block_size; // including the header and possibly tiny fragments
int tag; // a tag of 0 is a free block int tag; // a tag of 0 is a free block
struct memblock_s *next, *prev; struct memblock_s *next, *prev;
int id; // should be ZONEID int id; // should be ZONEID
@ -94,7 +94,7 @@ struct memzone_s
static int static int
z_block_size (memblock_t *block) z_block_size (memblock_t *block)
{ {
return block->size - sizeof (memblock_t) - 4; return block->block_size - sizeof (memblock_t) - 4;
} }
static int static int
@ -117,7 +117,7 @@ Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size)
zone->blocklist.prev = block; zone->blocklist.prev = block;
zone->blocklist.tag = 1; // in use block zone->blocklist.tag = 1; // in use block
zone->blocklist.id = 0; zone->blocklist.id = 0;
zone->blocklist.size = 0; zone->blocklist.block_size = 0;
zone->offset = zone_offset; zone->offset = zone_offset;
zone->ele_size = ele_size; zone->ele_size = ele_size;
zone->rover = block; zone->rover = block;
@ -130,7 +130,7 @@ Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size)
block->tag = 0; // free block block->tag = 0; // free block
block->id = ZONEID; block->id = ZONEID;
block->id2 = ZONEID; block->id2 = ZONEID;
block->size = size - sizeof (memzone_t); block->block_size = size - sizeof (memzone_t);
} }
VISIBLE void VISIBLE void
@ -171,12 +171,12 @@ Z_Free (memzone_t *zone, void *ptr)
} }
block->tag = 0; // mark as free block->tag = 0; // mark as free
zone->used -= block->size; zone->used -= block->block_size;
other = block->prev; other = block->prev;
if (!other->tag) { if (!other->tag) {
// merge with previous free block // merge with previous free block
other->size += block->size; other->block_size += block->block_size;
other->next = block->next; other->next = block->next;
other->next->prev = other; other->next->prev = other;
if (block == zone->rover) if (block == zone->rover)
@ -187,7 +187,7 @@ Z_Free (memzone_t *zone, void *ptr)
other = block->next; other = block->next;
if (!other->tag) { if (!other->tag) {
// merge the next free block onto the end // merge the next free block onto the end
block->size += other->size; block->block_size += other->block_size;
block->next = other->next; block->next = other->next;
block->next->prev = block; block->next->prev = block;
if (other == zone->rover) if (other == zone->rover)
@ -243,14 +243,14 @@ Z_TagMalloc (memzone_t *zone, int size, int tag)
base = rover = rover->next; base = rover = rover->next;
else else
rover = rover->next; rover = rover->next;
} while (base->tag || base->size < size); } while (base->tag || base->block_size < size);
// found a block big enough // found a block big enough
extra = base->size - size; extra = base->block_size - size;
if (extra > MINFRAGMENT) { if (extra > MINFRAGMENT) {
// there will be a free fragment after the allocated block // there will be a free fragment after the allocated block
new = (memblock_t *) ((byte *) base + size); new = (memblock_t *) ((byte *) base + size);
new->size = extra; new->block_size = extra;
new->tag = 0; // free block new->tag = 0; // free block
new->prev = base; new->prev = base;
new->id = ZONEID; new->id = ZONEID;
@ -258,7 +258,7 @@ Z_TagMalloc (memzone_t *zone, int size, int tag)
new->next = base->next; new->next = base->next;
new->next->prev = new; new->next->prev = new;
base->next = new; base->next = new;
base->size = size; base->block_size = size;
} }
base->tag = tag; // no longer a free block base->tag = tag; // no longer a free block
@ -268,10 +268,10 @@ Z_TagMalloc (memzone_t *zone, int size, int tag)
base->id = ZONEID; base->id = ZONEID;
base->id2 = ZONEID; base->id2 = ZONEID;
zone->used += base->size; zone->used += base->block_size;
// marker for memory trash testing // marker for memory trash testing
*(int *) ((byte *) base + base->size - 4) = ZONEID; *(int *) ((byte *) base + base->block_size - 4) = ZONEID;
return (void *) (base + 1); return (void *) (base + 1);
} }
@ -299,7 +299,7 @@ Z_Realloc (memzone_t *zone, void *ptr, int size)
Sys_Error ("Z_Realloc: realloced a freed pointer"); Sys_Error ("Z_Realloc: realloced a freed pointer");
} }
old_size = block->size; old_size = block->block_size;
old_size -= sizeof (memblock_t); // account for size of block header old_size -= sizeof (memblock_t); // account for size of block header
old_size -= 4; // space for memory trash tester old_size -= 4; // space for memory trash tester
old_ptr = ptr; old_ptr = ptr;
@ -339,14 +339,14 @@ Z_Print (memzone_t *zone)
break; // all blocks have been hit break; // all blocks have been hit
if (block->id != ZONEID || block->id2 != ZONEID) if (block->id != ZONEID || block->id2 != ZONEID)
Sys_Printf ("ERROR: block ids incorrect\n"); Sys_Printf ("ERROR: block ids incorrect\n");
if ( (byte *)block + block->size != (byte *)block->next) if ((byte *) block + block->block_size != (byte *) block->next)
Sys_Printf ("ERROR: block size does not touch the next block\n"); Sys_Printf ("ERROR: block size does not touch the next block\n");
if ( block->next->prev != block) if (block->next->prev != block)
Sys_Printf ("ERROR: next block doesn't have proper back link\n"); Sys_Printf ("ERROR: next block doesn't have proper back link\n");
if (!block->tag && !block->next->tag) if (!block->tag && !block->next->tag)
Sys_Printf ("ERROR: two consecutive free blocks\n"); Sys_Printf ("ERROR: two consecutive free blocks\n");
if (block->tag if (block->tag
&& (*(int *) ((byte *) block + block->size - 4) != ZONEID)) && (*(int *) ((byte *) block + block->block_size - 4) != ZONEID))
Sys_Printf ("ERROR: memory trashed in block\n"); Sys_Printf ("ERROR: memory trashed in block\n");
fflush (stdout); fflush (stdout);
} }
@ -360,7 +360,7 @@ Z_CheckHeap (memzone_t *zone)
for (block = zone->blocklist.next ; ; block = block->next) { for (block = zone->blocklist.next ; ; block = block->next) {
if (block->next == &zone->blocklist) if (block->next == &zone->blocklist)
break; // all blocks have been hit break; // all blocks have been hit
if ( (byte *)block + block->size != (byte *)block->next) if ((byte *) block + block->block_size != (byte *) block->next)
Sys_Error ("Z_CheckHeap: block size does not touch the next " Sys_Error ("Z_CheckHeap: block size does not touch the next "
"block\n"); "block\n");
if (block->next->prev != block) if (block->next->prev != block)
@ -389,14 +389,14 @@ Z_CheckPointer (const memzone_t *zone, const void *ptr, int size)
if (block->next == &zone->blocklist) if (block->next == &zone->blocklist)
break; // all blocks have been hit break; // all blocks have been hit
if (check < (const char *) block if (check < (const char *) block
|| check >= (const char *) block + block->size) || check >= (const char *) block + block->block_size)
continue; continue;
// a block that overlaps with the memory region has been found // a block that overlaps with the memory region has been found
if (!block->tag) if (!block->tag)
zone->error (zone->data, "invalid access to unallocated memory"); zone->error (zone->data, "invalid access to unallocated memory");
block_mem = (char *) &block[1]; block_mem = (char *) &block[1];
if (check < block_mem if (check < block_mem
|| check + size > block_mem + block->size - sizeof (*block)) || check + size > block_mem + block->block_size - sizeof (*block))
zone->error (zone->data, "invalid access to allocated memory"); zone->error (zone->data, "invalid access to allocated memory");
return; // access ok return; // access ok
} }