programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

Working with dynamic memory

From: Alexander Seregin <a.seregin_at_foratec-com.ru>
Date: Mon, 28 Apr 2008 00:05:41 +0600

Hi group!

Everyone know there are operators for dynamic memory management in SwiftForth.
It use Win32 Global-functions but sometimes it seems unsuitable.
So I write a new class that use Win32 Heap-function. It can be useful in
multitasking with SwiftForth.
Each task has own heap. For now I prefer to have 1 Mb for heap that can be
extended up to 4 Mb.
I don't include utility Heap-function cause they require specific OS version,
I mean Win2000 as minimum.

#1024 CONSTANT 1K 1K 1K * CONSTANT 1M

Function: HeapCreate ( a b c -- d ) \ here should be double-minus, it is
omitted in documentation
Function: HeapDestroy ( a -- d )
Function: HeapAlloc ( a b c -- d )
Function: HeapFree ( a b c -- d )
Function: HeapReAlloc ( a b c d -- e )
Function: HeapSize ( a b c -- d )

CLASS HEAP
   VARIABLE HANDLE

   \ Win32-function should be preceded by double-colon, also omitted in docs,
check the search-order for answer
   : CONSTRUCT ( - ) 0 1M 4 1M * :: HeapCreate HANDLE ! ;
   : DESTRUCT ( - ) HANDLE @ :: HeapDestroy DROP ;
   : ALLOCATE ( size - a ) HANDLE @ HEAP_ZERO_MEMORY ROT :: HeapAlloc ;
   : FREE ( a - ) HANDLE @ 0 ROT :: HeapFree DROP ;
   : RESIZE ( a1 size - a2 ) HANDLE @ HEAP_ZERO_MEMORY 2SWAP :: HeapReAlloc ;
   : SIZEOF ( a - size ) HANDLE @ 0 ROT :: HeapSize ;

END-CLASS

\ The using

HEAP BUILDS H1 \ builds the heap-object
H1 CONSTRUCT-OBJECT \ creating the heap by

0 VALUE PTR
100 CELLS H1 ALLOCATE TO PTR
PTR H1 SIZEOF . \ should be 400

PTR 200 CELLS H1 RESIZE TO PTR
PTR H1 SIZEOF . \ should be 800

PTR H1 FREE
H1 DESTRUCT-OBJECT \ destroy heap

If you use NEW word for creating you don't need to call CONSTRUCT-OBJECT. It
is called automatically.
I don't test it with MAKES word but think must be the same as with NEW.

Alexander

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe" to sftalk-request_at_forth.com
For list command help, send subject "help" to sftalk-request_at_forth.com
Message archives are located at http://www.forth.com/archive/sftalk
----------------------------------------------------------------------
This list is a forum for SwiftForth users. For product support and
bug reports, please send email to support_at_forth.com
----------------------------------------------------------------------
Received on Sun Apr 27 2008 - 11:01:09 PDT

This archive was generated by hypermail 2.2.0 : Tue Dec 02 2008 - 03:04:44 PST