Shop OBEX P1 Docs P2 Docs Learn Events
heap object — Parallax Forums

heap object

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2008-01-09 05:50 in Propeller 1
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

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-02 21:21
    Now all that we need is the ability to dynamically load spin programs.

    Steven
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-02 22:24
    Minor update to make it easier to support heaps in heap.
    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
  • Nick MuellerNick Mueller Posts: 815
    edited 2008-01-02 22:50
    Oh, and today I thought about heaps.
    But now ...

    Thanks!

    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 07:52
    Peter, is it alright if I use this in DOL?

    Steven
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-08 08:29
    Sure. The heap object is·intended to be initialized from the top object.
    (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
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 10:50
    Thats the idea Peter. Thanks for permission.

    Steven
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-08 16:59
    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
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-09 05:50
    Thanks Phil
Sign In or Register to comment.