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