All of Lua's memory allocation (since 5.1) goes through a single function, the default version of which is in lauxlib.c. For some unexplained reason, it simply hard locks my Dreamcast with the default version, which looks like this:
Code: Select all
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
(void)osize;
if (nsize == 0) {
free(ptr);
return NULL;
}
else
{
return realloc(ptr, nsize);
}
}
I stuck some instrumenting code in there, and realised it wasn't reallocating memory that was causing the problem. It was allocating the memory in the first place. Swapping out the realloc with a malloc (and a memcpy in case it actually does want a realloc) made no difference whatsoever. Still locked up.
I ran the Lua library and a simple test program (PC compiled - runs all the non-interactive test programs supplied with Lua) through Valgrind to make sure there were no memory related problems in Lua itself. There were none - no leaks, no writing to unallocated memory, no overrunning allocated memory blocks, nothing.
So I replaced the allocator with a stupid one. It allocates a 1MB block of memory and carves it up very simply (basically a stack - allocations are handled by taking the next n bytes, reallocations are handled by memcpy, and frees are totally ignored). Now it works perfectly. I can't leave it like that, because it'll run out of memory very quickly, and when that happens it'll start overwriting whatever's sitting after the memory block (no bounds checking, because I'm not fixing up a hack that I shouldn't be needing anyway).
So, does anyone have any idea what could be causing this sort of problem?