home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / icemalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-14  |  4.7 KB  |  181 lines  |  [TEXT/ALFA]

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