home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / salloc.seq < prev    next >
Text File  |  1988-12-01  |  3KB  |  72 lines

  1. \ SALLOC           An example of memory allocation      by Tom Zimmer
  2.  
  3. comment:
  4.  
  5.   This file contains an example of how to use the memory allocation
  6. and deallocation words.
  7.  
  8.   this example allocates space at F-PC cold start time by placing the
  9. word MY-INIT into the defered word chain called INITSTUFF.  You do not
  10. have to allocate your memory at this time, but it will prevent your memory
  11. allocation from interfering with the editors usage of memory.  It is
  12. fairly easy using allocate and deallocate to cause memory to get fragmented
  13. to the point where almost any ALLOC will fail.
  14.  
  15. comment;
  16.  
  17. hidden clearmem forth           \ make sure the editor is not using memory
  18.  
  19. 16000 constant bytes_needed     \ Size of the array I need in bytes.
  20.  
  21. 0 value myseg                   \ a place to put my base segment pointer.
  22.  
  23. : alloc-myseg   ( --- )                 \ allocate the space I need
  24.                 myseg ?exit             \ don't allocate if already did it
  25.                 bytes_needed paragraph  \ adjust needed to # of paragraphs
  26.                 alloc                   \ allocate the space
  27.                                         \ test for error in alloc
  28.                 8 = abort" Not enough memory for MYSEG"
  29.                 nip                     \ discard largest block available
  30.                 !> myseg ;              \ asign start of array into MYSEG
  31.  
  32. alloc-myseg     \ really allocate the space now so we can experiment
  33.  
  34. : my-init       ( --- )
  35.                 defers initstuff        \ inserted into INITSTUFF chain
  36.                 off> myseg
  37.                 alloc-myseg ;
  38.  
  39. \ ' my-init is init-stuff       \ un-comment this line to cause
  40.                                 \ initialization to be done at cold boot
  41.  
  42. \ If you need to deallocate the space that you have allocated, you can use
  43. \ DEALLOC to release the space back to DOS.
  44.  
  45. : release-myseg ( --- )
  46.                 myseg dealloc abort" failed to deallocate MYSEG" ;
  47.  
  48. \ The other operation you can perform on an allocated memory segment is
  49. \ to resize it to a new size. This is done with SETBLOCK.
  50.  
  51. : resize-myseg  ( n1 --- )      \ adjust myseg to new size n1 in bytes
  52.                 myseg swap paragraph setblock
  53.                 abort" Failed to resize MYSEG" ;
  54.  
  55. \ To put something into the array MYSEG, I might do something like the
  56. \ following.
  57.  
  58. : string-save   ( | <string> --- )      \ accept a <string> to save
  59.                 ?cs: 0 word dup>r       \ moving FROM here
  60.                 myseg 0                 \ TO the beginning of MYSEG
  61.                 r> c@ 1+ CMOVEL ;       \ move the data with CMOVE LONG
  62.  
  63. \ And you can then display the text from MYSEG.
  64.  
  65. : type-string   ( --- )
  66.                 myseg 0                 \ FROM myseg
  67.                 2dup c@L >r             \ fetch the count
  68.                 ?cs: here               \ TO here
  69.                 r> 1+ CMOVEL            \ move data to HERE
  70.                 here count type ;       \ display it
  71.  
  72.