home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / icemalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  5.6 KB  |  195 lines  |  [TEXT/MPS ]

  1.  
  2. /*
  3. **
  4. ** Notice.
  5. **
  6. ** This source code was written by Tim Endres. time@ice.com
  7. ** Copyright 1988-1991 © By Tim Endres. All rights reserved.
  8. **
  9. ** You may use this source code for any purpose you can dream
  10. ** of as long as this notice is never modified nor removed.
  11. **
  12. */
  13.  
  14. #ifndef __ICEMALLOC__
  15. #define __ICEMALLOC__
  16.  
  17. #include <sys/types.h>
  18.  
  19. #undef MALLOC_LOG
  20.  
  21. #ifdef MALLOC_LOG
  22. void MallocLog(const char * fmt, ...);
  23. #endif
  24.  
  25. /*
  26. ** Right now we are limiting the maximum single allocation unit to 16Meg.
  27. ** This way we can stuff the index to the next ptr hdr into the
  28. ** low 24 bits of a long, then slam 8 bits of flag information
  29. ** into the upper 8 bits of the same long. Not necessarily beautiful
  30. ** but compact and functional.
  31. */
  32.  
  33. /*
  34. **    _PM_DEBUG_LEVEL
  35. **
  36. **  1 - DPRINTF ERROR conditions.
  37. **  2 - DPRINTF *AND* DACTION ERROR conditions.
  38. **
  39. **  3 - DPRINTF WARNING conditions.
  40. **  5 - DPRINTF DEBUGING conditions.
  41. ** 10 - DPRINTF NOTES conditions.
  42. **
  43. */
  44.  
  45. #undef DEBUG
  46.  
  47. #ifdef DEBUG
  48.  
  49. #    define _PM_STATS
  50. #    define _PM_DYNAMIC_MERGING
  51. #    define _PM_DYNAMIC_FREE
  52.  
  53. #    define    _PM_DEBUG_LEVEL        1
  54.  
  55. #    define DPRINTF(level, parms)    { if ((level) <= pool_malloc_debug_level) { printf parms ; } }
  56. #    define DACTION(level, action)    { if ((level) <= pool_malloc_debug_level) { action } }
  57.  
  58. int        pool_malloc_debug_level = _PM_DEBUG_LEVEL;
  59.  
  60. #else
  61.  
  62. #    define _PM_DYNAMIC_MERGING
  63. #    define _PM_DYNAMIC_FREE
  64.  
  65. #    define    _PM_DEBUG_LEVEL        0
  66.  
  67. #    define DPRINTF(level, parms)
  68. #    define DACTION(level, action)
  69.  
  70. #endif DEVELOPMENT
  71.  
  72.  
  73. /*
  74. ** MEMORY PTR HEADER FLAG BITS:
  75. **
  76. ** 01 _PM_PTR_FREE        Is this piece of memory free?
  77. ** 02
  78. ** 04
  79. ** 08
  80. **
  81. ** 10
  82. ** 20
  83. ** 40
  84. ** 80 _PM_PTR_PARITY    This is a parity bit for debugging.
  85. **
  86. */
  87.  
  88. #define _PM_PTR_USED        0x01
  89. #define _PM_PTR_PARITY        0x80
  90.  
  91. #define _PM_MIN_ALLOC_SIZE    8
  92.  
  93. #define    ALIGNMENT                4        /* The 68020 likes things long aligned. */
  94. #define INT_ALIGN(i, r)            ( ((i) + ((r) - 1)) & ~((r) - 1) )
  95.  
  96.  
  97. #define SUGGESTED_BLK_SIZE        32768
  98.  
  99.  
  100. #define GET_PTR_FLAGS(hdr)    \
  101.     ( (u_long) ( (((hdr)->size) >> 24) & 0x000000FF ) )
  102. #define SET_PTR_USED(hdr)    \
  103.     ( (hdr)->size |= (((_PM_PTR_USED) << 24) & 0xFF000000) )
  104. #define SET_PTR_FREE(hdr)    \
  105.     ( (hdr)->size &= ~(((_PM_PTR_USED) << 24) & 0xFF000000) )
  106. #define IS_PTR_USED(hdr)    \
  107.     ( (GET_PTR_FLAGS(hdr) & _PM_PTR_USED) != 0 )
  108. #define IS_PTR_FREE(hdr)    \
  109.     ( (GET_PTR_FLAGS(hdr) & _PM_PTR_USED) == 0 )
  110.  
  111. #define GET_PTR_SIZE(hdr)    \
  112.     ( (u_long) ( ((hdr)->size) & 0x00FFFFFF ) )
  113. #define SET_PTR_SIZE(hdr, blksize)    \
  114.     ( (hdr)->size = ( ((hdr)->size & 0XFF000000) | ((blksize) & 0x00FFFFFF) ) )
  115.  
  116. typedef struct {
  117.     u_long        size;
  118.     } _mem_ptr_hdr, *_mem_ptr_hdr_ptr;
  119.  
  120.  
  121. /* There are two storage methods. Blocks smaller than 64 bytes are allocated
  122.    from a _MEM_BUCKET, larger blocks from a _MEM_BLK.
  123. */
  124.  
  125. typedef struct _MEM_BUCKET {
  126.     struct _MEM_BUCKET * next;                /* Next bucket                                         */
  127.     struct _MEM_BUCKET * prev;                /* Previous bucket                                    */
  128.     struct _MEM_POOL   * pool;                /* The bucket's pool                                    */
  129.     char *                    memory;            /* The bucket's allocated memory.                 */
  130.     char *                    free;                /* First free block                                     */
  131.     short                        max_count;        /* Total # of blocks                                 */
  132.     short                        free_count;        /* # of free blocks in this bucket                 */
  133. } _mem_bucket, *_mem_bucket_ptr;
  134.  
  135. typedef struct _MEM_BLK {
  136.     u_long                size;                /* The size of this block's memory. */
  137.     char                    *memory;            /* The block's allocated memory. */
  138.     u_long                max_free;        /* The maximum free size in the block */
  139.     struct _MEM_BLK    *next;            /* The next block in the pool block list. */
  140.     struct _MEM_POOL    *pool;            /* The block's pool. */
  141.     } _mem_blk, *_mem_blk_ptr;
  142.  
  143.  
  144. typedef struct _MEM_POOL {
  145.     int                    id;                /* The pool's ID.                             */
  146.     u_long                pref_blk_size;    /* The preferred size of new blks.        */
  147.     u_long                limit_blk_size;    /* Maximum size for a single blk.        */
  148.     _mem_bucket_ptr    blk_16;            /* Blocks <= 16 bytes                        */
  149.     _mem_bucket_ptr    free_16;            /* Blocks <= 16 bytes                        */
  150.     _mem_bucket_ptr    blk_32;            /* Blocks <= 32 bytes                        */
  151.     _mem_bucket_ptr    free_32;            /* Blocks <= 32 bytes                        */
  152.     _mem_bucket_ptr    blk_64;            /* Blocks <= 64 bytes                        */
  153.     _mem_bucket_ptr    free_64;            /* Blocks <= 64 bytes                        */
  154.     _mem_blk_ptr        blk_list;        /* The list of blocks in the pool.         */
  155.     struct _MEM_POOL    *next;            /* The next pool in the forest.             */
  156. #ifdef _PM_STATS
  157.     u_long                total_memory;    /* The total allocated memory by this pool */
  158.     u_long                total_storage;    /* The total malloc-able storage in this pool */
  159.     u_long                total_malloc;    /* The total malloc-ed storage not freed. */
  160.     u_long                max_blk_size;    /* The maximum block size allocated. */
  161.     float                    ave_req_size;    /* The ave allocated request size */
  162.     u_long                ave_req_total;    /* The total requests in the average. */
  163.     float                    ave_blk_size;    /* The ave sallocated blk size */
  164.     u_long                ave_blk_total;    /* The total blks in the average. */
  165. #endif
  166.     } _mem_pool, *_mem_pool_ptr;
  167.  
  168.  
  169.  
  170. extern _mem_pool    _mem_system_pool;
  171.  
  172. /*
  173. ** The memory pool forest. To the user, this is simply a disjoint
  174. ** group of memory pools, in which his memory pools lie. We keep
  175. ** it as a simple linked list. Forward linked, nothing fancy.
  176. **
  177. ** The default pool is simply the front pool in the pool forest list.
  178. */
  179. extern _mem_pool_ptr    _mem_pool_forest;
  180.  
  181. #define _default_mem_pool    _mem_pool_forest
  182.  
  183.  
  184. void    *                 pool_malloc(_mem_pool_ptr pool, u_long size);
  185. void    *                 pool_realloc(_mem_pool_ptr pool, void * ptr, u_long size);
  186. int                     pool_free(void * ptr);
  187. int                     free_pool(int id);
  188. int                     free_pool_memory(int id);
  189. _mem_pool_ptr        find_pool(int id);
  190. _mem_pool_ptr        new_malloc_pool(int id, u_long pref_blk_size);
  191. int                     set_default_pool(int id);
  192. int                     merge_free_list();
  193. void                     get_malloc_stats(long * total_memory, long * total_free, long * total_malloc);
  194.  
  195. #endif