I've been trying to get Lua 5.1 working on the Dreamcast for a while (about an hour). As of right now, it seems to be working correctly, except for one problem - I had to bypass the KOS built-in memory allocator to get the thing to work. Otherwise, it runs perfectly with absolutely no modifications.
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:
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);
}
}
Nice, simple, and should work. But it doesn't. KOS locks up and stops responding somewhere inside realloc after a few allocations, and frankly I wouldn't even know where to
start trying to debug that. The test program simply called lua_open, so it's not exactly doing anything demanding or difficult - just allocating a total of less than 2KB of memory.
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?