home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / programme / GoldED / developer / examples / syntax / warpcpp / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  1.6 KB  |  105 lines

  1. #include "defs.h"
  2.  
  3. /// "Prototypes"
  4.  
  5. Prototype long MemoryInit(void);
  6. Prototype void MemoryExit(void);
  7. Prototype APTR AllocVecPooled(ULONG,ULONG);
  8. Prototype void FreeVecPooled(APTR);
  9.  
  10. ///
  11. /// "Data"
  12.  
  13. static struct SignalSemaphore  MemorySemaphore;
  14. static APTR                    MemoryPool;
  15.  
  16. ///
  17. /// "MemoryInit"
  18.  
  19. long
  20. MemoryInit(void)
  21. {
  22.     InitSemaphore(&MemorySemaphore);
  23.  
  24.     if (MemoryPool = AsmCreatePool(MEMF_ANY | MEMF_PUBLIC, 4096, 4096, SysBase))
  25.         return(TRUE);
  26.     else
  27.         return(FALSE);
  28. }
  29.  
  30. ///
  31. /// "MemoryExit"
  32.  
  33. void
  34. MemoryExit(void)
  35. {
  36.     if (MemoryPool) {
  37.  
  38.         AsmDeletePool(MemoryPool, SysBase);
  39.  
  40.         MemoryPool = NULL;
  41.     }
  42. }
  43.  
  44. ///
  45. /// "AllocVecPooled"
  46.  
  47. APTR
  48. AllocVecPooled(ULONG size, ULONG flags)
  49. {
  50.     if (MemoryPool) {
  51.  
  52.         ULONG *data;
  53.  
  54.         size = (size + 3) & ~3;
  55.  
  56.         ObtainSemaphore(&MemorySemaphore);
  57.  
  58.         if (data = (ULONG *)AsmAllocPooled(MemoryPool, size + sizeof(ULONG), SysBase)) {
  59.  
  60.             ReleaseSemaphore(&MemorySemaphore);
  61.  
  62.             *data++ = size + sizeof(ULONG);
  63.  
  64.             if (flags & MEMF_CLEAR) {
  65.  
  66.                 register ULONG *memory = data;
  67.  
  68.                 size /= sizeof(ULONG);
  69.  
  70.                 do {
  71.  
  72.                     *memory++ = 0;
  73.  
  74.                 } while(--size);
  75.             }
  76.  
  77.             return((APTR)data);
  78.         }
  79.  
  80.         ReleaseSemaphore(&MemorySemaphore);
  81.     }
  82.  
  83.     return(NULL);
  84. }
  85.  
  86. ///
  87. /// "FreeVecPooled"
  88.  
  89. void
  90. FreeVecPooled(APTR memory)
  91. {
  92.     if (memory) {
  93.  
  94.         ULONG *data = (ULONG *)memory;
  95.  
  96.         ObtainSemaphore(&MemorySemaphore);
  97.  
  98.         AsmFreePooled(MemoryPool, &data[-1], data[-1], SysBase);
  99.  
  100.         ReleaseSemaphore(&MemorySemaphore);
  101.     }
  102. }
  103.  
  104. ///
  105.