home *** CD-ROM | disk | FTP | other *** search
/ Netscape Plug-Ins Developer's Kit / Netscape_Plug-Ins_Developers_Kit.iso / CGIPERL / MACPERL / MSRCE418.HQX / Perl Source ƒ / Perl / icemalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  5.6 KB  |  199 lines

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