[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Memory allocation for userdata
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 16 Dec 2011 09:54:09 +0200
I'm writing a binding for a C library with several kinds of structures
that are allocated thus:
where alloc is a macro
#define alloc(obj) (obj*)calloc(1,sizeof(obj))
Back in my youth we were told not to do that when the application, as
this one does, only frees the structures right at the end. Instead we
were supposed to allocate a memory heap, reallocate it to double size
when it gets exhausted, and write a function `alloc` that simply
serves up bits off that heap.
I've been out of C for, oh, twelve years I suppose, and it occurs to
me that in the meantime the algorithm behind `calloc` might have
become clever enough to do just that, one heap per process.
In a Lua binding there is the added possibility of allocating the
required memory via lua_newuserdata.
What do you think?