home *** CD-ROM | disk | FTP | other *** search
/ Amiga Computing 57 / ac057a.adf / Demos / AllocExample.bas next >
BASIC Source File  |  1989-03-16  |  1KB  |  34 lines

  1.  
  2. ' example showing how to safely allocate and free memory from HiSoft BASIC
  3. ' for the more advanced programmer only
  4.  
  5. ' SafeAlloc takes the same parameters as AllocMem in the exec.library
  6. ' but adds the memory to a special list. This means it will always
  7. ' deallocate the memory when the program finishes or stops with a
  8. ' run-time error. If there is not enough memory then a BASIC
  9. ' out-of-memory error will occur.
  10.  
  11. ' SafeFree takes the result of a previous SafeAlloc call and returns
  12. ' the memory block to the system. It only has an address parameter.
  13. ' If you try to give back memory that was not allocated with SafeAlloc
  14. ' then a fatal error 'memory list corrupt' will occur.
  15. ' If you pass 0 to this routine, no action will be taken. This makes
  16. ' clean-up code a lot easier.
  17.  
  18.  
  19. LIBRARY "hisoftbasic.library"
  20. DECLARE FUNCTION SafeAlloc&(length&,type&) LIBRARY
  21. DECLARE SUB SafeFree&(addr&) LIBRARY
  22.  
  23. ' useful options for type&
  24. CONST MEM_PUBLIC=1, MEM_CHIP=2, MEM_FAST=4
  25. MEM_CLEAR=&h10000                'cannot be a CONST
  26.  
  27. ' ultra-simple example
  28. block&=SafeAlloc&(40,MEM_PUBLIC OR MEM_CHIP)
  29. PRINT "Got some public chip memory at";block&
  30. SafeFree block&
  31. PRINT "Given back safely"
  32.  
  33.  
  34.