lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm writing a binding for a C library with several kinds of structures
that are allocated thus:

    mystruct* M=alloc(mystruct);

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?

Dirk