home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / zone.h < prev   
Encoding:
C/C++ Source or Header  |  1996-09-11  |  3.2 KB  |  126 lines

  1. // Copyright 1988-1996 NeXT Software, Inc.
  2.  
  3. #ifndef _OBJC_ZONE_H_
  4. #define _OBJC_ZONE_H_
  5.  
  6. #import <stddef.h>
  7. #import <objc/objc-api.h>    // for OBJC_EXPORT
  8.  
  9. #ifdef WIN32
  10. #if !defined(_USE_PDO_MALLOC)
  11. #define _USE_PDO_MALLOC
  12. #include <malloc.h>
  13. /* Normally this won't matter since we use the system's free.  The only 
  14.    case where it become imperitive that this is used is for memory that
  15.    was vm_allocate'ed and then later NXAddRegion'ed.  In this case, 
  16.    we need to track that memory so that vm_deallocate is called on it 
  17.    instead of free. */
  18. #define free(ptr) pdo_free(ptr)
  19. #define malloc(size) pdo_malloc(size)
  20. #define realloc(ptr,size) pdo_realloc(ptr,size)
  21. #define calloc(num,size) pdo_calloc(num,size)
  22. #endif /* !defined(_USE_PDO_MALLOC) */
  23. #endif /* WIN32 */
  24.  
  25. /*
  26.  * Interface to zone based malloc.
  27.  */
  28.  
  29. typedef struct _NXZone {
  30.     void *(*realloc)(struct _NXZone *zonep, void *ptr, size_t size);
  31.     void *(*malloc)(struct _NXZone *zonep, size_t size);
  32.     void (*free)(struct _NXZone *zonep, void *ptr);
  33.     void (*destroy)(struct _NXZone *zonep);
  34.           /* Implementation specific entries */
  35.           /* Do not depend on the size of this structure */
  36. } NXZone;
  37.  
  38. #define NX_NOZONE  ((NXZone *)0)
  39.  
  40. /*
  41.  * Returns the default zone used by the malloc(3) calls.
  42.  */
  43. OBJC_EXPORT NXZone *NXDefaultMallocZone(void);
  44.  
  45. /* 
  46.  * Create a new zone with its own memory pool.
  47.  * If canfree is 0 the allocator will never free memory and mallocing will be fast
  48.  */
  49. OBJC_EXPORT NXZone *NXCreateZone(size_t startSize, size_t granularity, int canFree);
  50.  
  51. /*
  52.  * Create a new zone who obtains memory from another zone.
  53.  * Returns NX_NOZONE if the passed zone is already a child.
  54.  */
  55. OBJC_EXPORT NXZone  *NXCreateChildZone(NXZone *parentZone, size_t startSize, size_t granularity, int canFree);
  56.  
  57. /*
  58.  * The zone is destroyed and all memory reclaimed.
  59.  */
  60. #define NXDestroyZone(zonep) \
  61.     ((*(zonep)->destroy)(zonep))
  62.     
  63. /*
  64.  * Will merge zone with the parent zone. Malloced areas are still valid.
  65.  * Must be an child zone.
  66.  */
  67. OBJC_EXPORT void NXMergeZone(NXZone *zonep);
  68.  
  69. #define NXZoneMalloc(zonep, size) \
  70.     ((*(zonep)->malloc)(zonep, size))
  71.  
  72. #define NXZoneRealloc(zonep, ptr, size) \
  73.     ((*(zonep)->realloc)(zonep, ptr, size))
  74.     
  75. #define NXZoneFree(zonep, ptr) \
  76.     ((*(zonep)->free)(zonep, ptr))
  77.  
  78. /*
  79.  * Calls NXZoneMalloc and then bzero.
  80.  */
  81. OBJC_EXPORT void *NXZoneCalloc(NXZone *zonep, size_t numElems, size_t byteSize);
  82.  
  83. /*
  84.  * Returns the zone for a pointer.
  85.  * NX_NOZONE if not in any zone.
  86.  * The ptr must have been returned from a malloc or realloc call.
  87.  */
  88. OBJC_EXPORT NXZone *NXZoneFromPtr(void *ptr);
  89.  
  90. /*
  91.  * Debugging Helpers.
  92.  */
  93.  
  94.  /*  
  95.   * Will print to stdout if this pointer is in the malloc heap, free status, and size.
  96.   */
  97. OBJC_EXPORT void NXZonePtrInfo(void *ptr);
  98.  
  99. /*
  100.  * Will verify all internal malloc information.
  101.  * This is what malloc_debug calls.
  102.  */
  103. #if !defined(WIN32)
  104. OBJC_EXPORT int NXMallocCheck(void);
  105. #endif
  106.  
  107. /*
  108.  * Give a zone a name.
  109.  *
  110.  * The string will be copied.
  111.  */
  112. OBJC_EXPORT void NXNameZone(NXZone *z, const char *name);
  113.  
  114. /*
  115.  * Prototypes
  116.  */
  117.  
  118. #if !defined(__MACH__)
  119. OBJC_EXPORT void *pdo_malloc (size_t size);
  120. OBJC_EXPORT void *pdo_realloc (void *ptr, size_t size);
  121. OBJC_EXPORT void *pdo_calloc (size_t num, size_t size);
  122. OBJC_EXPORT void pdo_free (void* ptr);
  123. #endif
  124.  
  125. #endif /* _OBJC_ZONE_H_ */
  126.