#ident "$Id$" /* ----------------------------------------------------------------------- * * * Copyright 2000-2001 H. Peter Anvin - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, Inc., * 59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1, * incorporated herein by reference. * * ----------------------------------------------------------------------- */ #include #include #include #include #include #include #include #include "lpsm.h" #define COUNT 32768 #define SLOTS 1024 #define MAXLOG 26 void *areas[COUNT]; int sizes[COUNT]; void verify_no_overlap(int slot) { char *start = areas[slot]; char *end = start+sizes[slot]; char *xs, *xe; int i; for ( i = 0 ; i < SLOTS ; i++ ) { if ( i != slot && areas[i] ) { xs = areas[i]; xe = xs + sizes[i]; if ( xs < end && xe > start ) { printf("OVERLAP ALERT: [%p,%p) -> [%p,%p)\n", start, end, xs, xe); abort(); } } } } int main(int argc, char *argv[]) { int i, slot, newsize; double rnd; void **root_ptr; void *nptr; unsigned long occupied = 0; unlink("arena.dat"); 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]); return 1; } printf("Arena: initialized, %d root pointers at %p\n", LPSM_ROOT_POINTERS, root_ptr); lpsm_checkpoint(0.0, PSMSYNC_WAIT); for ( i = 0 ; i < COUNT ; i++ ) { slot = rand() % SLOTS; if ( areas[slot] ) { 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]; } else { rnd = (double)rand()/RAND_MAX; newsize = (int)pow(2.0, MAXLOG*pow(rnd,3.0)); nptr = lpsm_realloc(areas[slot], newsize); if ( nptr ) { printf("Realloc: %d bytes at %p -> %d bytes at %p\n", sizes[slot], areas[slot], newsize, nptr); occupied += newsize; occupied -= sizes[slot]; sizes[slot] = newsize; areas[slot] = nptr; verify_no_overlap(slot); } else { printf("Realloc: %d bytes at %p -> %d bytes FAILED\n", sizes[slot], areas[slot], 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]; } else { printf("Alloc: %d (0x%08x) bytes FAILED\n", sizes[slot], sizes[slot]); } verify_no_overlap(slot); } if ( (i & 255) == 0 ) { printf("Arena: checkpointing...\n"); lpsm_checkpoint(0.1, PSMSYNC_SKIP); } printf("Arena: %ld bytes officially occupied\n", occupied); } fprintf(stderr, "ready...\n"); lpsm_checkpoint(0.0, PSMSYNC_SYNC); /* Synchronous checkpoint */ return 0; }