lua-users home
lua-l archive

Re: No realloc

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


> I'm considering using Lua on a system offering no realloc(): only 
> malloc() and free().
> 
> What loss of performance could I expect by using realloc=free+malloc, 
> assuming the allocator is stupid enough not to immediately recycle the 
> same block when there is enough space?
Why don't you test? It is quite easy to change the allocation function not
to use realloc; something like this (untested):
void *l_alloc (void *ud, void *block, size_t oldsize, size_t size) {
 if (size == 0) {
 free(block);
 return NULL;
 }
 else {
 void *newblock = malloc(size); /* alloc a new block */
 if (newblock == NULL) return NULL;
 if (block) {
 size_t commonsize = (oldsize < size) ? oldsize : size;
 memcpy(newblock, block, commonsize);
 free(block);
 }
 return newblock;
 }
}
-- Roberto

AltStyle によって変換されたページ (->オリジナル) /