#include enum { MAGIC = 0xbada110c, MAX2SIZE = 32, CUTOFF = 12, }; aggr Bucket { int size; int magic; Bucket *next; char data[1]; }; aggr Arena { Lock; Bucket *btab[MAX2SIZE]; }; intern Arena arena; #define datoff ((int)&((Bucket*)0)->data) void* malloc(uint size) { uint next; int pow, n; Bucket *bp, *nbp; for(pow = 1; pow < MAX2SIZE; pow++) { if(size <= (1<next; arena.unlock(); if(bp->magic != 0) abort(); bp->magic = MAGIC; memset(bp->data, 0, size); return bp->data; } size = sizeof(Bucket)+(1<next = (Bucket*)next; nbp->size = pow; nbp = nbp->next; } nbp->size = pow; } else { bp = sbrk(size); if((int)bp < 0) raise; } arena.unlock(); bp->size = pow; bp->magic = MAGIC; return bp->data; } void free(void *ptr) { Bucket *bp, **l; if(ptr == nil) return; /* Find the start of the structure */ bp = (Bucket*)((uint)ptr - datoff); if(bp->magic != MAGIC) abort(); bp->magic = 0; arena.lock(); l = &arena.btab[bp->size]; bp->next = *l; *l = bp; arena.unlock(); } void* realloc(void *ptr, uint n) { void *new; uint osize; Bucket *bp; if(ptr == nil) return malloc(n); /* Find the start of the structure */ bp = (Bucket*)((uint)ptr - datoff); if(bp->magic != MAGIC) abort(); /* enough space in this bucket */ osize = 1<size; if(osize >= n) return ptr; new = malloc(n); if(new == nil) return nil; memmove(new, ptr, osize); free(ptr); return new; }