[3ae31e9] | 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
| 2 | * MALLOC.H -- header file for A "smarter" malloc
|
---|
| 3 | * Version 4 -- 1988-05-03 -- USENET
|
---|
| 4 | *
|
---|
| 5 | * A "smarter" malloc Original code by William L. Sebok
|
---|
| 6 | *
|
---|
| 7 | * Modified for Aztec C-II by D.N. Lynx Crowe
|
---|
| 8 | *
|
---|
| 9 | * 1986-12-30 Made long constants explicit as fix for bug in compiler
|
---|
| 10 | * 1987-01-01 Made long constants into variables to work around compiler bug
|
---|
| 11 | * 1988-05-03 Modified to work on the Atari under GEMDOS
|
---|
| 12 | * 1988-05-03 Added define of SMARTMAL so we know what we've got
|
---|
| 13 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
| 14 | * Algorithm:
|
---|
| 15 | * Assign to each area an index "n". This is currently proportional to
|
---|
| 16 | * the log 2 of size of the area rounded down to the nearest integer.
|
---|
| 17 | * Then all free areas of storage whose length have the same index n are
|
---|
| 18 | * organized into a chain with other free areas of index n (the "bucket"
|
---|
| 19 | * chain). A request for allocation of storage first searches the list of
|
---|
| 20 | * free memory. The search starts at the bucket chain of index equal to
|
---|
| 21 | * that of the storage request, continuing to higher index bucket chains
|
---|
| 22 | * if the first attempt fails.
|
---|
| 23 | * If the search fails then new memory is allocated. Only the amount of
|
---|
| 24 | * new memory needed is allocated. Any old free memory left after an
|
---|
| 25 | * allocation is returned to the free list.
|
---|
| 26 | *
|
---|
| 27 | * All memory areas (free or busy) handled by malloc are also chained
|
---|
| 28 | * sequentially by increasing address (the adjacency chain). When memory
|
---|
| 29 | * is freed it is merged with adjacent free areas, if any. If a free area
|
---|
| 30 | * of memory ends at the end of memory (i.e. at the break), and if the
|
---|
| 31 | * variable "endfree" is non-zero, then the break is contracted, freeing
|
---|
| 32 | * the memory back to the system.
|
---|
| 33 | *
|
---|
| 34 | * Notes:
|
---|
| 35 | * ov_len field includes sizeof(struct overhead)
|
---|
| 36 | * adjacency chain includes all memory, allocated plus free.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | /* **************************** Machine Dependencies ******************** */
|
---|
| 40 |
|
---|
| 41 | /* the following items may need to be configured for a particular machine */
|
---|
| 42 |
|
---|
| 43 | #define void int /* Aztec C doesn't have voids */
|
---|
| 44 |
|
---|
| 45 | /* alignment requirement for machine (in bytes) */
|
---|
| 46 |
|
---|
| 47 | #define NALIGN 2
|
---|
| 48 |
|
---|
| 49 | /* size of an integer large enough to hold a character pointer */
|
---|
| 50 |
|
---|
| 51 | typedef long Size;
|
---|
| 52 |
|
---|
| 53 | /*
|
---|
| 54 | * CURBRK returns the value of the current system break, i.e., the system's
|
---|
| 55 | * idea of the highest legal address in the data area. It is defined as
|
---|
| 56 | * a macro for the benefit of systems that have provided an easier way to
|
---|
| 57 | * obtain this number (such as in an external variable)
|
---|
| 58 | */
|
---|
| 59 |
|
---|
| 60 | #ifndef CURBRK
|
---|
| 61 |
|
---|
| 62 | #define CURBRK sbrk(0)
|
---|
| 63 | extern char *sbrk();
|
---|
| 64 |
|
---|
| 65 | #else CURBRK
|
---|
| 66 |
|
---|
| 67 | #if CURBRK == curbrk
|
---|
| 68 | extern Size curbrk;
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | #endif CURBRK
|
---|
| 72 |
|
---|
| 73 | /*
|
---|
| 74 | * note that it is assumed that CURBRK remembers the last requested break to
|
---|
| 75 | * the nearest byte (or at least the nearest word) rather than the nearest page
|
---|
| 76 | * boundary. If this is not true then the following BRK macro should be
|
---|
| 77 | * replaced with one that remembers the break to within word-size accuracy.
|
---|
| 78 | */
|
---|
| 79 |
|
---|
| 80 | #ifndef BRK
|
---|
| 81 |
|
---|
| 82 | #define BRK(x) brk(x)
|
---|
| 83 | extern char *brk();
|
---|
| 84 |
|
---|
| 85 | #endif BRK
|
---|
| 86 |
|
---|
| 87 | /*
|
---|
| 88 | define NBUCKETS as 18 for big machines, 10 for small ones
|
---|
| 89 | and adjust the definitions of mlsizes[] and buckets[], below.
|
---|
| 90 | */
|
---|
| 91 |
|
---|
| 92 | #define NBUCKETS 18
|
---|
| 93 |
|
---|
| 94 | /* ***************** END of machine dependent portion ******************* */
|
---|
| 95 |
|
---|
| 96 | struct qelem {
|
---|
| 97 |
|
---|
| 98 | struct qelem *q_forw;
|
---|
| 99 | struct qelem *q_back;
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | struct overhead {
|
---|
| 103 |
|
---|
| 104 | struct qelem ov_adj; /* adjacency chain pointers */
|
---|
| 105 | struct qelem ov_buk; /* bucket chain pointers */
|
---|
| 106 | long ov_magic; /* MAGIC number */
|
---|
| 107 | Size ov_len; /* length of the area in bytes */
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | /*
|
---|
| 111 | * The following macros depend on the order of the elements in struct overhead
|
---|
| 112 | */
|
---|
| 113 |
|
---|
| 114 | #define TOADJ(p) ((struct qelem *)(p))
|
---|
| 115 | #define FROMADJ(p) ((struct overhead *)(p))
|
---|
| 116 | #define FROMBUK(p) ((struct overhead *)( (char *)p - sizeof(struct qelem)))
|
---|
| 117 | #define TOBUK(p) ((struct qelem *)( (char *)p + sizeof(struct qelem)))
|
---|
| 118 |
|
---|
| 119 | #define XM_FREE 0x548A934CL /* MAGIC for free blocks */
|
---|
| 120 | #define XM_BUSY 0xC139569AL /* MAGIC for busy blocks */
|
---|
| 121 |
|
---|
| 122 | #define M_FREE m_free
|
---|
| 123 | #define M_BUSY m_busy
|
---|
| 124 |
|
---|
| 125 | #ifdef MALLOC
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * return to the system memory freed adjacent to the break
|
---|
| 129 | * default is Off
|
---|
| 130 | */
|
---|
| 131 |
|
---|
| 132 | char endfree = 0; /* WARNING -- this isn't PROMable */
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
| 135 | * M_FREE and M_BUSY used to be MAGIC_FREE and MAGIC_BUSY
|
---|
| 136 | * m_free and m_busy were added to work around a BUG in Aztec CII
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | long m_free = XM_FREE;
|
---|
| 140 | long m_busy = XM_BUSY;
|
---|
| 141 |
|
---|
| 142 | /* head of adjacency chain */
|
---|
| 143 |
|
---|
| 144 | struct qelem adjhead = { &adjhead, &adjhead };
|
---|
| 145 |
|
---|
| 146 | /* *************** More Machine Dependencies *********************** */
|
---|
| 147 |
|
---|
| 148 | /* sizes of buckets currently proportional to log 2() */
|
---|
| 149 | /* must match NBUCKETS, above */
|
---|
| 150 |
|
---|
| 151 | Size mlsizes[] = {0, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
|
---|
| 152 |
|
---|
| 153 | /* trim here if on a small machine */
|
---|
| 154 |
|
---|
| 155 | 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304};
|
---|
| 156 |
|
---|
| 157 | /* head of bucket chains */
|
---|
| 158 | /* must match NBUCKETS, above */
|
---|
| 159 |
|
---|
| 160 | struct qelem buckets[NBUCKETS] = {
|
---|
| 161 |
|
---|
| 162 | &buckets[0], &buckets[0], &buckets[1], &buckets[1],
|
---|
| 163 | &buckets[2], &buckets[2], &buckets[3], &buckets[3],
|
---|
| 164 | &buckets[4], &buckets[4], &buckets[5], &buckets[5],
|
---|
| 165 | &buckets[6], &buckets[6], &buckets[7], &buckets[7],
|
---|
| 166 | &buckets[8], &buckets[8], &buckets[9], &buckets[9],
|
---|
| 167 |
|
---|
| 168 | /* trim here if on a small machine */
|
---|
| 169 |
|
---|
| 170 | &buckets[10], &buckets[10], &buckets[11], &buckets[11],
|
---|
| 171 | &buckets[12], &buckets[12], &buckets[13], &buckets[13],
|
---|
| 172 | &buckets[14], &buckets[14], &buckets[15], &buckets[15],
|
---|
| 173 | &buckets[16], &buckets[16], &buckets[17], &buckets[17]
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 | /* ********************* End of Machine Dependencies ********************* */
|
---|
| 177 |
|
---|
| 178 | void (*mlabort)() = {0};
|
---|
| 179 |
|
---|
| 180 | #else
|
---|
| 181 |
|
---|
| 182 | extern char endfree;
|
---|
| 183 | extern struct qelem adjhead, buckets[NBUCKETS];
|
---|
| 184 | extern Size mlsizes[NBUCKETS];
|
---|
| 185 | extern void (*mlabort)();
|
---|
| 186 | extern long m_free, m_busy;
|
---|
| 187 |
|
---|
| 188 | #endif
|
---|
| 189 |
|
---|
| 190 | extern void insque(), remque();
|
---|
| 191 | extern void free(), mllcerr();
|
---|
| 192 | extern char *malloc(), *realloc();
|
---|
| 193 |
|
---|
| 194 | #ifdef debug
|
---|
| 195 |
|
---|
| 196 | # define ASSERT(p,q) if (!(p)) mllcerr(q)
|
---|
| 197 |
|
---|
| 198 | #else
|
---|
| 199 |
|
---|
| 200 | # define ASSERT(p,q)
|
---|
| 201 |
|
---|
| 202 | #endif
|
---|
| 203 |
|
---|
| 204 | #define SMARTMAL
|
---|