aboutsummaryrefslogtreecommitdiffstats
path: root/testalloc.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2008-01-14 22:18:03 -0500
committerH. Peter Anvin <hpa@zytor.com>2008-01-14 22:18:03 -0500
commitf734372b23435748a9e794f3997e53c99fe1a786 (patch)
treecdd2a8c66705b995e8b867475179175f87579d94 /testalloc.c
parent18284e8f5c54510ee02d5a3513b79aa687f9ed6f (diff)
downloadlpsm-f734372b23435748a9e794f3997e53c99fe1a786.tar.gz
lpsm-f734372b23435748a9e794f3997e53c99fe1a786.tar.xz
lpsm-f734372b23435748a9e794f3997e53c99fe1a786.zip
64-bit fixes (still has errors, however)
Do a number of 64-bit fixes. In particular: * (1 << foo) is not safe if foo >= 32 * size_t is the appropriate type for "as big as we can fit"
Diffstat (limited to 'testalloc.c')
-rw-r--r--testalloc.c69
1 files changed, 37 insertions, 32 deletions
diff --git a/testalloc.c b/testalloc.c
index 3cccd26..79c2dba 100644
--- a/testalloc.c
+++ b/testalloc.c
@@ -23,24 +23,27 @@
#define SLOTS 1024
#define MAXLOG 26
-void *areas[COUNT];
-int sizes[COUNT];
+struct area_info {
+ void *ptr;
+ size_t size;
+ int atseq;
+} areas[COUNT];
void verify_no_overlap(int slot)
{
- char *start = areas[slot];
- char *end = start+sizes[slot];
+ char *start = areas[slot].ptr;
+ char *end = start+areas[slot].size;
char *xs, *xe;
int i;
for ( i = 0 ; i < SLOTS ; i++ ) {
- if ( i != slot && areas[i] ) {
- xs = areas[i];
- xe = xs + sizes[i];
+ if ( i != slot && areas[i].ptr ) {
+ xs = areas[i].ptr;
+ xe = xs + areas[i].size;
if ( xs < end && xe > start ) {
- printf("OVERLAP ALERT: [%p,%p) -> [%p,%p)\n",
- start, end, xs, xe);
+ printf("OVERLAP ALERT: [%p,%p) -> [%p,%p) (from sequence %d)\n",
+ start, end, xs, xe, areas[i].atseq);
abort();
}
}
@@ -62,7 +65,6 @@ int main(int argc, char *argv[])
unlink("arena.log");
memset(areas, 0, sizeof(areas));
- memset(sizes, 0, sizeof(sizes));
if ( !(root_ptr = lpsm_init("arena.dat", "arena.log")) ) {
fprintf(stderr, "%s: lpsm_arena_init() failed\n", argv[0]);
@@ -79,41 +81,44 @@ int main(int argc, char *argv[])
slot = rand() % SLOTS;
- if ( areas[slot] ) {
+ if ( areas[slot].ptr ) {
if ( rand() % 1 ) {
- printf("Free: %d (0x%08x) bytes at %p\n",
- sizes[slot], sizes[slot], areas[slot]);
- lpsm_free(areas[slot]);
- areas[slot] = NULL;
- occupied -= sizes[slot];
+ printf("Free: %zu (0x%zx) bytes at %p\n",
+ areas[slot].size, areas[slot].size, areas[slot].ptr);
+ lpsm_free(areas[slot].ptr);
+ areas[slot].ptr = NULL;
+ occupied -= areas[slot].size;
+ areas[slot].atseq = 0;
} else {
rnd = (double)rand()/RAND_MAX;
newsize = (int)pow(2.0, MAXLOG*pow(rnd,3.0));
- nptr = lpsm_realloc(areas[slot], newsize);
+ nptr = lpsm_realloc(areas[slot].ptr, newsize);
if ( nptr ) {
- printf("Realloc: %d bytes at %p -> %d bytes at %p\n",
- sizes[slot], areas[slot], newsize, nptr);
+ printf("Realloc: %zu bytes at %p -> %d bytes at %p\n",
+ areas[slot].size, areas[slot].ptr, newsize, nptr);
occupied += newsize;
- occupied -= sizes[slot];
- sizes[slot] = newsize;
- areas[slot] = nptr;
+ occupied -= areas[slot].size;
+ areas[slot].size = newsize;
+ areas[slot].ptr = nptr;
+ areas[slot].atseq = sequence;
verify_no_overlap(slot);
} else {
- printf("Realloc: %d bytes at %p -> %d bytes FAILED\n",
- sizes[slot], areas[slot], newsize);
+ printf("Realloc: %zu bytes at %p -> %d bytes FAILED\n",
+ areas[slot].size, areas[slot].ptr, newsize);
}
}
} else {
rnd = (double)rand()/RAND_MAX;
- sizes[slot] = (int)pow(2.0, MAXLOG*pow(rnd,5.0));
- areas[slot] = lpsm_malloc(sizes[slot]);
- if ( areas[slot] ) {
- printf("Alloc: %d (0x%08x) bytes at %p\n",
- sizes[slot], sizes[slot], areas[slot]);
- occupied += sizes[slot];
+ areas[slot].size = (int)pow(2.0, MAXLOG*pow(rnd,5.0));
+ areas[slot].ptr = lpsm_malloc(areas[slot].size);
+ if ( areas[slot].ptr ) {
+ printf("Alloc: %zu (0x%zx) bytes at %p\n",
+ areas[slot].size, areas[slot].size, areas[slot].ptr);
+ occupied += areas[slot].size;
+ areas[slot].atseq = sequence;
} else {
- printf("Alloc: %d (0x%08x) bytes FAILED\n",
- sizes[slot], sizes[slot]);
+ printf("Alloc: %zu (0x%zx) bytes FAILED\n",
+ areas[slot].size, areas[slot].size);
}
verify_no_overlap(slot);
}