Memory Management - September 24th, 2002
Enth has two methods of allocating memory. ANS Forth's ALLOCATE and Enth's BLOCKS, which is basically just ALLOCATE with a 1024 byte granularity. Allocated memory can be a bit of a thorny issue. Some advocate not using it at all while others swear by it. It probably does have a place in the scheme of things as the Forth dictionary's ALLOT does not always match requirements. Dynamically managing memory in a dictionary also used for code compilation can be messy. Enth has a simple memory management scheme that uses two linked lists. The primary list always contains all allocated chunks of memory. ALLOCATE and FREE traverse this list. The secondary list is actually multiple lists. When memory is allocated to a task, the address is added to a linked list of all of that task's allocated memory. This means via the primary and secondary link fields, one can tell where a chuck of allocated memory is and which task it belongs too. Firstly this is used by the Multitasking. When a task is HALTed, erased or aborts execution due to an error, all of its allocated memory is FREEd. Chances are, that there is no memory left to be released back to the system for reallocation, because as good little programmers we keep track of what memory our code uses and make sure we clean up after ourselves! But it is nice to have a safety line. When bugs lurk and tasks crash, it is pleasant to have one's programming environment be able to recover. Secondly, this is used by MARKER. A MARKER in ANS Forth allows roll back of dictionary pointers and wordlists. A MARKER in Enth also keeps track of memory allocation. Execution of a MARKER will FREE any memory added to the task's allocated memory linked list subsequent to the MARKER's creation. This means that code compiled after a MARKER may allocate memory to its heart's content and rest assured that it will be cleaned up in one simple word later on. This can simplify code a great deal. This memory management does differ slightly from ANS Forth and can therefore not be called standard. It also does not nesassarily match well to all situations, just as the standard method does not always match. Both have their place and their uses. ANS Forth is not really designed to cater for the true multiuser/multitasking Forth. Niether is its memory management scheme. Enth's method is an experiment which so far I have found to be enjoyable and elegant. |