heap object
Peter Verkaik
Posts: 3,956
Attached are a heap object and test program.
This provides dynamic memory use. Blocks can be allocated and freed in any order.
Support methods to check heap integrity,·retrieving length of allocated blocks,
retrieving length of largest free block.
regards peter
Post Edited (Peter Verkaik) : 1/2/2008 8:14:46 PM GMT
This provides dynamic memory use. Blocks can be allocated and freed in any order.
Support methods to check heap integrity,·retrieving length of allocated blocks,
retrieving length of largest free block.
regards peter
Post Edited (Peter Verkaik) : 1/2/2008 8:14:46 PM GMT
Comments
Steven
I added the method heap that returns the base address of the heap.
·* Example:
·* OBJ
·*·· hp1: "heap"
·*·· hp2: "heap"
·* VAR
·*·· byte heapArray[noparse][[/noparse]1024]·········· 'heapArray will be used to hold a heap
·*·· word block
·* PUB
·*·· hp1.create(@heapArray,1024)··· 'create heap with size 1024 bytes
·*·· if (hp1.available => 120)
·*···· block := hp1.allocate(120)·· 'allocate a block of 120 bytes
·*···· 'do something with block
·*···· hp1.free(block)
·*·· else
·*···· 'error No block allocated
·*
·* It is possible to create a heap inside a heap.
·* Example:
·*·· hp1.create(@heapArray,1024)
·*·· block := hp1.allocate(120)·········· 'allocate a block
·*·· hp2.create(block,hp1.length(block)) 'convert block to heap
·* This is useful if a task must keep track of multiple blocks (from heap hp2) but the task may be aborted.
·* In that case a single call to free will free all blocks allocated from heap hp2.
·*·· hp1.free(hp2.heap)
·*
regards peter
But now ...
Thanks!
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
Steven
(by calling create). By declaring it also in OBJ in other objects, these
objects can also allocate and free blocks of memory. Note that there
currently is no lock used in the heap object but that is easily added.
Point of concern: what if a dynamically loaded object also makes
use of the heap object? When unloading such an object it should free
its allocated blocks before it unloads, but I guess the stop method
of that object can do that (so unload calls stop first). The memory
used to store the object itself is of course freed by DOL.
regards peter
Steven
Here is another heap manager object for you to try, if you need one with locking built-in. Also included in the zip is a dynamic strings object, which demostrates how to use the heap manager.
-Phil