On Wed, 19 May 2010 16:02:54 +0100 Anselm R Garbe <garb...@gmail.com> wrote:
> On 19 May 2010 15:55, Elmo Todurov <todu...@gmail.com> wrote: > > On Wed, May 19, 2010 at 5:15 PM, pancake <panc...@youterm.com> wrote: > >> it's not that 'complex'. code shouldnt look uglier with this change, it's > >> just to > >> replace current allocator, which you should do, because failed mallocs > >> must die(). > > > > Care to write a patch to prove your point? I must admit I've never > > written my own allocators. > > Something like this? > > void * > emalloc(uint size) { > void *ret = malloc(size); > if(!ret) > die("malloc", size); > return ret; > } > Here's a very generic version of what I proposed. In your case can be hardly simplified. http://radare.org/cgi-bin/hg/radare2/file/6c6000dd3695/libr/util/pool.c The specific version can be found in: hg clone http://hg.youterm.com/alt 8 static inline AltNode* node_alloc(AltTree *at) { 9 if (at->ncount >= ALLOC_POOL_SIZE) { 10 if (++at->npool >= ALLOC_POOL_COUNT ) { 11 fprintf (stderr, "FAIL: Cannot allocate more memory in the pool \n"); 12 exit(1); 13 } 14 at->nodes[at->npool] = malloc (sizeof (AltNode)*ALLOC_POOL_SIZE); 15 at->ncount = 0; 16 } 17 return &at->nodes[at->npool][at->ncount++]; 18 } 19 20 static AltNode *alt_node_new(AltTree *at) { 21 AltNode *node = node_alloc (at); 22 memset (node, 0, sizeof (AltNode)); 23 return node; 24 } The thing is how many pools you want to support. I would probably not support more than one pool. This concept/implementation of a memory pool allocator is the same as glib slices. But smarter, because you can just adapt it to your needs. --pancake