home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / objam01.lha / objam / runtime / zone.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-11  |  3.2 KB  |  161 lines

  1. /*
  2. ** ObjectiveAmiga: NeXTSTEP NXZone emulation under AmigaOS
  3. ** See GNU:lib/libobjam/ReadMe for details
  4. */
  5.  
  6.  
  7. #include <exec/memory.h>
  8. #include <exec/exec.h>
  9. #include <clib/alib_protos.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <stddef.h>
  13.  
  14. #include <clib/objc_protos.h>
  15.  
  16. #include "misc.h" /* For the ANSI function emulations */
  17.  
  18.  
  19. #define max(a,b) (((a)>(a))?(b):(b))
  20. #define min(a,b) (((a)>(b))?(b):(a))
  21.  
  22.  
  23. #ifdef AMIGAOS_39
  24. #define nxCreatePool(a,b,c)  CreatePool(a,b,c)
  25. #define nxDeletePool(a)      DeletePool(a)
  26. #define nxAllocPooled(a,b)   AllocPooled(a,b)
  27. #define nxFreePooled(a,b,c)  FreePooled(a,b,c)
  28. #else
  29. #define nxCreatePool(a,b,c)  LibCreatePool(a,b,c)
  30. #define nxDeletePool(a)      LibDeletePool(a)
  31. #define nxAllocPooled(a,b)   LibAllocPooled(a,b)
  32. #define nxFreePooled(a,b,c)  LibFreePooled(a,b,c)
  33. #endif
  34.  
  35.  
  36. /* Our real zone */
  37. struct __NXZone
  38. {
  39.   void *pool;
  40.   BOOL canFree;
  41.   char *name;
  42. };
  43.  
  44.  
  45. /* A memory block header */
  46. struct NXBlock
  47. {
  48.   int size; /* Without NXBlock */
  49.   struct __NXZone *zone;
  50. };
  51.  
  52.  
  53. /* Useful macros */
  54. #define BLOCK2PTR(b)  ((void *)(((size_t)b)+sizeof(struct NXBlock)))
  55. #define PTR2BLOCK(p)  ((struct NXBlock *)((size_t)p-sizeof(struct NXBlock)))
  56. #define ZONE          ((struct __NXZone *)(zone))
  57.  
  58.  
  59. NXZone *__DefaultMallocZone;
  60.  
  61.  
  62. NXZone *NXCreateZone(size_t startSize, size_t granularity, int canFree)
  63. {
  64.   void *pool;
  65.   NXZone *zone;
  66.  
  67.   if(!(pool=nxCreatePool(MEMF_CLEAR|MEMF_ANY, granularity, granularity))) return NULL;
  68.   if(!(zone=(struct __NXZone *)nxAllocPooled(pool,sizeof(struct __NXZone)))) { nxDeletePool(pool); return NULL; }
  69.  
  70.   ZONE->pool=pool;
  71.   ZONE->canFree=canFree;
  72.  
  73.   return zone;
  74. }
  75.  
  76.  
  77. NXZone *NXCreateChildZone(NXZone *parentZone, size_t startSize, size_t granularity, int canFree)
  78. {
  79.   return parentZone;
  80. }
  81.  
  82.  
  83. void NXMergeZone(NXZone *zone)
  84. {
  85. }
  86.  
  87.  
  88. NXZone *NXZoneFromPtr(void *ptr)
  89. {
  90.   if(!ptr) return NULL;
  91.   return (NXZone *)(((struct NXBlock *)((size_t)ptr-sizeof(struct NXBlock)))->zone);
  92. }
  93.  
  94.  
  95. void NXDestroyZone(NXZone *zone)
  96. {
  97.   nxDeletePool(ZONE->pool);
  98. }
  99.  
  100.  
  101. void *NXZoneMalloc(NXZone *zone, int size)
  102. {
  103.   struct NXBlock *block;
  104.  
  105.   if(!(block=(struct NXBlock *)nxAllocPooled(ZONE->pool,size+sizeof(struct NXBlock)))) return NULL;
  106.   block->size=size;
  107.   block->zone=zone;
  108.  
  109.   return BLOCK2PTR(block);
  110. }
  111.  
  112.  
  113. void *NXZoneCalloc(NXZone *zone, int numElements, int elementSize)
  114. {
  115.   return NXZoneMalloc(zone,numElements*elementSize);
  116. }
  117.  
  118.  
  119. void *NXZoneRealloc(NXZone *zone, void *block, int size)
  120. {
  121.   void *newBlock;
  122.  
  123.   if(!(newBlock=NXZoneMalloc(zone,size))) return NULL;
  124.   CopyMem( (APTR)block,(APTR)newBlock,min(PTR2BLOCK(block)->size,size) );
  125.   return newBlock;
  126. }
  127.  
  128.  
  129. void NXZoneFree(NXZone *zone, void *block)
  130. {
  131.   if(block&&zone)
  132.     if(ZONE->canFree)
  133.       nxFreePooled( ZONE->pool, (void *)(PTR2BLOCK(block)), PTR2BLOCK(block)->size+sizeof(struct NXBlock) );
  134. }
  135.  
  136.  
  137. void NXNameZone(NXZone *zone, const char *name)
  138. {
  139.   ZONE->name=(char *)name;
  140. }
  141.  
  142.  
  143. void NXZonePtrInfo(void *ptr)
  144. {
  145.   struct __NXZone *zone=PTR2BLOCK(ptr)->zone;
  146.  
  147.   printf("*** Memory pointer info:\n  Memory block at... %lx\n  In zone at........ %lx\n  With name......... %s\n",(int)ptr,(int)zone,(zone->name)?(zone->name):("<none>"));
  148. }
  149.  
  150.  
  151. NXZone *NXDefaultMallocZone(void)
  152. {
  153.   return __DefaultMallocZone;
  154. }
  155.  
  156.  
  157. int NXMallocCheck(void)
  158. {
  159.   return 0;
  160. }
  161.