home *** CD-ROM | disk | FTP | other *** search
- \ SALLOC An example of memory allocation by Tom Zimmer
-
- comment:
-
- This file contains an example of how to use the memory allocation
- and deallocation words.
-
- this example allocates space at F-PC cold start time by placing the
- word MY-INIT into the defered word chain called INITSTUFF. You do not
- have to allocate your memory at this time, but it will prevent your memory
- allocation from interfering with the editors usage of memory. It is
- fairly easy using allocate and deallocate to cause memory to get fragmented
- to the point where almost any ALLOC will fail.
-
- comment;
-
- hidden clearmem forth \ make sure the editor is not using memory
-
- 16000 constant bytes_needed \ Size of the array I need in bytes.
-
- 0 value myseg \ a place to put my base segment pointer.
-
- : alloc-myseg ( --- ) \ allocate the space I need
- myseg ?exit \ don't allocate if already did it
- bytes_needed paragraph \ adjust needed to # of paragraphs
- alloc \ allocate the space
- \ test for error in alloc
- 8 = abort" Not enough memory for MYSEG"
- nip \ discard largest block available
- !> myseg ; \ asign start of array into MYSEG
-
- alloc-myseg \ really allocate the space now so we can experiment
-
- : my-init ( --- )
- defers initstuff \ inserted into INITSTUFF chain
- off> myseg
- alloc-myseg ;
-
- \ ' my-init is init-stuff \ un-comment this line to cause
- \ initialization to be done at cold boot
-
- \ If you need to deallocate the space that you have allocated, you can use
- \ DEALLOC to release the space back to DOS.
-
- : release-myseg ( --- )
- myseg dealloc abort" failed to deallocate MYSEG" ;
-
- \ The other operation you can perform on an allocated memory segment is
- \ to resize it to a new size. This is done with SETBLOCK.
-
- : resize-myseg ( n1 --- ) \ adjust myseg to new size n1 in bytes
- myseg swap paragraph setblock
- abort" Failed to resize MYSEG" ;
-
- \ To put something into the array MYSEG, I might do something like the
- \ following.
-
- : string-save ( | <string> --- ) \ accept a <string> to save
- ?cs: 0 word dup>r \ moving FROM here
- myseg 0 \ TO the beginning of MYSEG
- r> c@ 1+ CMOVEL ; \ move the data with CMOVE LONG
-
- \ And you can then display the text from MYSEG.
-
- : type-string ( --- )
- myseg 0 \ FROM myseg
- 2dup c@L >r \ fetch the count
- ?cs: here \ TO here
- r> 1+ CMOVEL \ move data to HERE
- here count type ; \ display it
-
-