12.7 Using two heaps. 4tH 4tH Forth compiler
Reklaam
Reklaam
CHAPTER 12. STANDARD LIBRARIES
[pragma] forcecellheap struct field: kind field: o1 field: o2 field: o3 field: val end-struct /paragraph include lib/ansmem.4th
\ force a CELL heap
\ define the node
\ name it "/paragraph"
\ leave the library to it
You can now be assured that the size of a paragraph equals a single node. Neat, huh?
173
12.7
Using two heaps
Due to 4tH’s Harvard architecture
3
, you cannot mix cells and characters. Of course, you can use a library to store numeric information
4
, but sometimes that is not sufficient. Fortunately, 4tH offers another library that allows you to have two heaps, one for your cells and one for your strings. Well, your first question probably is: how can I differentiate between the two? It’s easy: if you combine the two you allocate cells with ”ALLOCATE”,
”FREE”, ”RESIZE”, just as usual. If you want to allocate strings, you use ”CALLOCATE”,
”CFREE” and ”CRESIZE”.
In order to achieve that you have to include the cell library before the string library: include lib/memcell.4th
include lib/memchar.4th
If you don’t, memchar.4th will default to your usual ”ALLOCATE”, ”FREE” and ”RE-
SIZE” words. You can also determine the size of both heaps by setting a constant before including them. The size is in address units:
2048 constant /heap include lib/memcell.4th
8192 constant /heap include lib/memchar.4th
\ a 2K cell heap
\ an 8K string heap
Note the pragma forcecellheap is automatically set when you include memcell.4th, so you can use that one in your programs as well. Both libraries are drop-in replacements of ansmem.4th (for their respective datatype). So why two different implementations?
It largely depends what your program needs. If you require:
• A small implementation;
• Fairly robust;
• A few large allocations or a limited number of allocations of a similar size.
You’ll be well off with ansmem.4th. However, if you require:
• An unknown number of allocations of different sizes;
• A combination of a cell heap and a string heap.
You’ll be better off with memcell.4th and memchar.4th.
3 http://en.wikipedia.org/wiki/Harvard_architecture
4
See section 12.4.
Laadi alla
Reklaam