Friday, June 06, 2008

Implicit Memory Manager for Forth

And so it is another one of those gee-I-ought-to-be-asleep-but-I'm-not kind of writing. Previously, I was talking about having to write a memory manager for my forth interpreter. The memory manager is actually done, and I think that it probably works. I'll probably write a stronger series of tests to ensure that it really works correctly, but at this point, I'm satisfied with what it can do.

The design for the memory manager is simple. It uses an implicit list to keep track of blocks of memory that exist in the forth virtual machine (fVM). Since all memory locations are allocated by cells, where each cell is 32-bits wide, there are no real alignment issues to worry about. I avoided an explicit list to reduce roughly three cells worth of memory that is required by that more advanced data structure. What this means is that the memory allocation and freeing are both linear in time with respect to the number of memory blocks that are in the fVM. Freeing uses coalescing, and the linear traversal required means that we can perform consistency checks in the fVM heap to prevent silly things from happening.

The memory manager is deliberately separated from the main code, thus allowing me to implement other versions should the need arise. The only thing that is still missing is the realloc() function, which might be useful in the case of trying to FORGET a word in forth.

The next step in the interpreter code, it seems, is to define the dictionary and some primitive instructions. Once these are done, I can start to compile the default dictionary and actually have a working forth implementation running.

Who knows, I might just put up the code once I have it all done.

No comments: