[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Interpreter not building correct byte code
- From: Ralph Hempel <rhempel@...>
- Date: Sat, 23 Dec 2006 13:25:28 -0500
Tom Bates wrote:
After walking all the way through the lexical parsing on both the PC (which
was working properly) and the prototype hardware, I've since found that
there's a bug in our realloc function! <gasp> Not to mention a minor
inefficiency where if both the old size and new size are the same, it still
reallocates the block!
My problem was with realloc too. When reducing the size of a block
of memory to a misaligned value, the pointers in the reallocated block
pointed to a misaligned value for the next block.
Increasing the block size worked fine because it called malloc() which
forced a properly aligned block.
I worked around the bug temporarily in luaM_realloc by using
malloc,copy,free; that seems to work, and the interpreter is now functioning
on the prototype.
The memory allocator we're using does return aligned blocks. I would think
that's a real necessity in any environment. Does the C standard have
anything to say about this? The 68331 will get an odd address trap, but
natural boundaries aren't required (i.e. 4-byte alignment for longs) since
it's basically a 16-bit machine externally.
I don't think the C standard says anything about this, but I'd need
to check. Aligning things to 16 bit boundaries will avoid extra
data fetches, of course. I think you want things aligned as much
as possible for speed.
Since the allocations all appear to be rather small, I don't think we're
going to have any issues with memory allocation. It crossed my mind to
instrument the allocator as well.
It's the reallocs I'm worried about. Lets say when Lua parses some
code it wants a block of 32 bytes, and then it reduces the block to
8 bytes. Then we had better hope that the 24 byte block (less
attributes) can be used or merged with another subsequent block,
or we end up with pretty gaping holes in the memory space.
What's a Lua GEM? When the code seems stable, perhaps I'll post my thoughts
to get a "porting guide" started.
Search the group. Roberto and the others are thinking of putting
together a little book of short Gems, ala Bentley's "Programming
Gems" Putting Lua on embedded processors seems like a great topic!