PennMUSH Community

root/1.8.3/trunk/src/csrimalloc.c

Revision 1278, 99.6 kB (checked in by shawnw, 2 weeks ago)

Prep for 1.8.3p7

Line 
1 /*  Author: Mark Moraes <moraes@csri.toronto.edu> */
2
3 #include "config.h"
4 #include <stdio.h>
5 #include <stddef.h>
6 #include <string.h>
7 #ifdef I_FCNTL
8 #include <fcntl.h>
9 #endif
10 #ifdef I_SYS_FILE
11 #include <sys/file.h>
12 #endif
13 #ifdef I_SYS_MMAN
14 #include <sys/mman.h>
15 #endif
16 #ifdef I_SYS_STAT
17 #include <sys/stat.h>
18 #endif
19 #ifdef I_SYS_TYPES
20 #include <sys/types.h>
21 #endif
22 #include "conf.h"
23 #include "externs.h"
24 #include "mymalloc.h"
25 #include "csrimalloc.h"         /* Must be after mymalloc.h! */
26
27 #ifdef CSRI_TRACE
28 #undef malloc(x)
29 #undef calloc(x, n)
30 #undef realloc(p, x)
31 #undef memalign(x, n)
32 #undef valloc(x)
33 #undef emalloc(x)
34 #undef ecalloc(x, n)
35 #undef erealloc(p, x)
36 #undef strdup(p)
37 #undef strsave(p)
38 #undef cfree(p)
39 #undef free(p)
40 extern char *strdup(const char *);
41 #endif
42
43
44 /*  Author: Mark Moraes <moraes@csri.toronto.edu> */
45 /*  Modified by Javelin to replace references to _mal_statsfile
46  *  with stderr, to make glibc happy.
47  */
48 #ifndef __DEFS_H__
49 #define __DEFS_H__
50 /*
51  *  This file includes all relevant include files, and defines various
52  *  types and constants. It also defines lots of macros which operate on
53  *  the malloc blocks and pointers, to try and keep the ugliness
54  *  abstracted away from the source code.
55  */
56
57 #include <errno.h>
58
59 #ifndef REGISTER
60 #ifdef __GNUC__
61    /* gcc probably does better register allocation than I do */
62 #define REGISTER
63 #else
64 #define REGISTER register
65 #endif
66 #endif
67
68 #if defined(__STDC__)
69 #define proto(x)        x
70 #else
71 #define proto(x)        ()
72 #define const
73 #define volatile
74 #endif
75
76 #ifndef EXTERNS_H__
77 #define EXTERNS_H__
78
79 /* Lots of ugliness as we cope with non-standardness */
80
81 /*
82  *  Malloc definitions from General Utilities <stdlib.h>. Note that we
83  *  disagree with Berkeley Unix on the return type of free/cfree.
84  */
85 extern univptr_t malloc proto((size_t));
86 extern univptr_t calloc proto((size_t, size_t));
87 extern univptr_t realloc proto((univptr_t, size_t));
88 extern Free_t free proto((univptr_t));
89
90 /* General Utilities <stdlib.h> */
91
92 extern void abort proto((void));
93 extern void exit proto((int));
94 extern char *getenv proto((const char *));
95
96 /*
97  *  Input/Output <stdio.h> Note we disagree with Berkeley Unix on
98  *  sprintf().
99  */
100
101 #if 0                           /* can't win with this one */
102 extern int sprintf proto((char *, const char *, ...));
103 #endif
104
105 extern int fputs proto((const char *, FILE *));
106 extern int fflush proto((FILE *));
107 #if 0
108 extern int setvbuf proto((FILE *, char *, int, memsize_t));
109 #endif
110
111
112 /* Character Handling: <string.h> */
113
114 #if 0
115 /* We'd better not have to do this - Javelin */
116
117 #ifndef HAS_MEMSET
118 extern univptr_t memset proto((univptr_t, int, memsize_t));
119 #endif
120
121 #ifndef HAS_MEMCPY
122 extern univptr_t memcpy proto((univptr_t, const univptr_t, memsize_t));
123 #endif
124
125 extern char *strcpy proto((char *, const char *));
126 extern memsize_t strlen proto((const char *));
127 #endif
128
129 /* UNIX -- unistd.h */
130 extern int write
131 proto((int /*fd */ , const char * /*buf */ , int /*nbytes */ ));
132 extern int open proto((const char * /*path */ , int /*flags */ , ...));
133
134 #define caddr_t char *
135 extern caddr_t sbrk proto((int));
136
137 #ifdef _AIX                     /* IBM AIX doesn't declare sbrk, but is STDHEADERS. */
138 extern caddr_t sbrk proto((int));
139 #endif
140
141 /* Backwards compatibility with BSD/Sun -- these are going to vanish one day */
142 extern univptr_t valloc proto((size_t));
143 extern univptr_t memalign proto((size_t, size_t));
144 extern Free_t cfree proto((univptr_t));
145
146 /* Malloc definitions - my additions.  Yuk, should use malloc.h properly!!  */
147 extern univptr_t emalloc proto((size_t));
148 extern univptr_t ecalloc proto((size_t, size_t));
149 extern univptr_t erealloc proto((univptr_t, size_t));
150 extern char *strsave proto((const char *));
151 #ifdef CSRI_DEBUG
152 extern void mal_debug proto((int));
153 extern int mal_verify proto((int));
154 #endif
155 extern void mal_dumpleaktrace proto((FILE *));
156 extern void mal_heapdump proto((FILE *));
157 extern void mal_leaktrace proto((int));
158 extern void mal_mmap proto((char *));
159 extern void mal_sbrkset proto((int));
160 extern void mal_slopset proto((int));
161 #ifdef CSRI_PROFILESIZES
162 extern void mal_statsdump proto((FILE *));
163 #endif
164 extern void mal_trace proto((int));
165
166 /* Internal definitions */
167 extern int __nothing proto((void));
168 extern univptr_t _mal_sbrk proto((size_t));
169 extern univptr_t _mal_mmap proto((size_t));
170
171 #ifdef HAVE_MMAP
172 extern int madvise proto((caddr_t, size_t, int));
173 extern caddr_t mmap proto((caddr_t, size_t, int, int, int, off_t));
174 #endif
175
176 #endif /* EXTERNS_H__ */                        /* Do not add anything after this line */
177
178 /* $Id: csrimalloc.c 1.23.1.8 Wed, 26 May 2004 09:52:42 -0500 dunemush $ */
179 #ifndef __ASSERT_H__
180 #define __ASSERT_H__
181 #ifdef CSRI_DEBUG
182 #define ASSERT(p, s) \
183         ((void)(!(p)?__m_botch((s),"",(univptr_t)0,0,__FILE__,__LINE__):0))
184 #define ASSERT_SP(p, s, s2, sp) \
185         ((void)(!(p)?__m_botch((s),(s2),(univptr_t)(sp),0,__FILE__,__LINE__):0))
186 #define ASSERT_EP(p, s, s2, ep) \
187         ((void)(!(p)?__m_botch((s),(s2),(univptr_t)(ep),1,__FILE__,__LINE__):0))
188
189 extern int __m_botch
190 proto((const char *, const char *, univptr_t, int, const char *, int));
191 #else
192 #define ASSERT(p, s)
193 #define ASSERT_SP(p, s, s2, sp)
194 #define ASSERT_EP(p, s, s2, ep)
195 #endif
196 #endif /* __ASSERT_H__ */                        /* Do not add anything after this line */
197 #ifndef EXIT_FAILURE
198 #define EXIT_FAILURE    1
199 #endif
200
201 #ifdef __STDC__
202 #include <limits.h>
203 #ifndef BITSPERBYTE
204 #define BITSPERBYTE     CHAR_BIT
205 #endif
206 #else
207 #include <values.h>
208 #endif
209
210 /* $Id: csrimalloc.c 1.23.1.8 Wed, 26 May 2004 09:52:42 -0500 dunemush $ */
211 #ifndef __ALIGN_H__
212 #define __ALIGN_H__
213 /*
214  * 'union word' must be aligned to the most pessimistic alignment needed
215  * on the architecture (See align.h) since all pointers returned by
216  * malloc and friends are really pointers to a Word. This is the job of
217  * the 'foo' field in the union, which actually decides the size. (On
218  * Sun3s, 1 int/long (4 bytes) is good enough, on Sun4s, 8 bytes are
219  * necessary, i.e. 2 ints/longs)
220  */
221 /*
222  * 'union word' should also be the size necessary to ensure that an
223  * sbrk() immediately following an sbrk(sizeof(union word) * n) returns
224  * an address one higher than the first sbrk. i.e. contiguous space from
225  * successive sbrks. (Most sbrks will do this regardless of the size -
226  * Sun's doesnt) This is not vital - the malloc will work, but will not
227  * be able to coalesce between sbrk'ed segments.
228  */
229
230 #if defined(sparc) || defined(__sparc__) || defined(__sgi) || defined(__hpux)
231 /*
232  * Sparcs require doubles to be aligned on double word, SGI R4000 based
233  * machines are 64 bit, this is the conservative way out
234  */
235 /* this will waste space on R4000s or the 64bit UltraSparc */
236 #define ALIGN long foo[2]
237 #define NALIGN 8
238 #endif
239
240 #if defined(__alpha)
241 /* 64 bit */
242 #define ALIGN long foo
243 #define NALIGN 8
244 #endif
245
246 /* This default seems to work on most 32bit machines */
247 #ifndef ALIGN
248 #define ALIGN long foo
249 #define NALIGN 4
250 #endif
251
252 /* Align with power of 2 */
253 #define SIMPLEALIGN(X, N) (univptr_t)(((size_t)((char *)(X) + (N-1))) & ~(N-1))
254
255 #if     NALIGN == 2 || NALIGN == 4 || NALIGN == 8 || NALIGN == 16
256   /* if NALIGN is a power of 2, the next line will do ok */
257 #define ALIGNPTR(X) SIMPLEALIGN(X, NALIGN)
258 #else
259   /* otherwise we need the generic version; hope the compiler isn't too smart */
260 static size_t _N = NALIGN;
261 #define ALIGNPTR(X) (univptr_t)((((size_t)((univptr_t)(X)+(NALIGN-1)))/_N)*_N)
262 #endif
263
264 /*
265  * If your sbrk does not return blocks that are aligned to the
266  * requirements of malloc(), as specified by ALIGN above, then you need
267  * to define SBRKEXTRA so that it gets the extra memory needed to find
268  * an alignment point. Not needed on Suns on which sbrk does return
269  * properly aligned blocks. But on U*x Vaxen, A/UX and UMIPS at least,
270  * you need to do this. It is safer to take the non Sun route (!! since
271  * the non-sun part also works on Suns, maybe we should just remove the
272  * Sun ifdef)
273  */
274 #if ! (defined(sun) || defined(__sun__))
275 #define SBRKEXTRA               (sizeof(Word) - 1)
276 #define SBRKALIGN(x)    ALIGNPTR(x)
277 #else
278 #define SBRKEXTRA               0
279 #define SBRKALIGN(x)    (x)
280 #endif
281
282 #ifndef BITSPERBYTE
283   /*
284    * These values should work with any binary representation of integers
285    * where the high-order bit contains the sign. Should be in values.h,
286    * but just in case...
287    */
288   /* a number used normally for size of a shift */
289 #if gcos
290 #define BITSPERBYTE     9
291 #else                           /* ! gcos */
292 #define BITSPERBYTE     8
293 #endif                          /* gcos */
294 #endif                          /* BITSPERBYTE */
295
296 #ifndef BITS
297 #define BITS(type)      (BITSPERBYTE * (int) sizeof(type))
298 #endif                          /* BITS */
299
300 /* size_t with only the high-order bit turned on */
301 #define HIBITSZ (((size_t) 1) << (BITS(size_t) - 1))
302
303 #endif /* __ALIGN_H__ */                        /* Do not add anything after this line */
304
305 /*
306  * We assume that FREE is a 0 bit, and the tag for a free block, Or'ing the
307  * tag with ALLOCED should turn on the high bit, And'ing with SIZEMASK
308  * should turn it off.
309  */
310
311 #define FREE            ((size_t) 0)
312 #define ALLOCED         (HIBITSZ)
313 #define SIZEMASK        (~HIBITSZ)
314
315 #define MAXBINS         8
316
317 #define DEF_SBRKUNITS   1024
318
319 union word {                    /* basic unit of storage */
320   size_t size;                  /* size of this block + 1 bit status */
321   union word *next;             /* next free block */
322   union word *prev;             /* prev free block */
323   univptr_t ptr;                /* stops lint complaining, keeps alignment */
324   char c;
325   int i;
326   char *cp;
327   char **cpp;
328   int *ip;
329   int **ipp;
330    ALIGN;                       /* alignment stuff - wild fun */
331 };
332
333 typedef union word Word;
334
335 /*
336  *  WARNING - Many of these macros are UNSAFE because they have multiple
337  *  evaluations of the arguments. Use with care, avoid side-effects.
338  */
339 /*
340  * These macros define operations on a pointer to a block. The zero'th
341  * word of a block is the size field, where the top bit is 1 if the
342  * block is allocated. This word is also referred to as the starting tag.
343  * The last word of the block is identical to the zero'th word of the
344  * block and is the end tag.  IF the block is free, the second last word
345  * is a pointer to the next block in the free list (a doubly linked list
346  * of all free blocks in the heap), and the third from last word is a
347  * pointer to the previous block in the free list.  HEADERWORDS is the
348  * number of words before the pointer in the block that malloc returns,
349  * TRAILERWORDS is the number of words after the returned block. Note
350  * that the zero'th and the last word MUST be the boundary tags - this
351  * is hard-wired into the algorithm. Increasing HEADERWORDS or
352  * TRAILERWORDS suitably should be accompanied by additional macros to
353  * operate on those words. The routines most likely to be affected are
354  * malloc/realloc/free/memalign/mal_verify/mal_heapdump.
355  */
356 /*
357  * There are two ways we can refer to a block -- by pointing at the
358  * start tag, or by pointing at the end tag. For reasons of efficiency
359  * and performance, free blocks are always referred to by the end tag,
360  * while allocated blocks are usually referred to by the start tag.
361  * Accordingly, the following macros indicate the type of their argument
362  * by using either 'p', 'sp', or 'ep' to indicate a pointer. 'p' means
363  * the pointer could point at either the start or end tag. 'sp' means it
364  * must point at a start tag for that macro to work correctly, 'ep'
365  * means it must point at the end tag. Usually, macros operating on free
366  * blocks (NEXT, PREV, VALID_PREV_PTR, VALID_NEXT_PTR) take 'ep', while
367  * macros operating on allocated blocks (REALSIZE, MAGIC_PTR,
368  * SET_REALSIZE) take 'sp'. The size field may be validated using either
369  * VALID_START_SIZE_FIELD for an 'sp' or VALID_END_SIZE_FIELD for an
370  * 'ep'.
371  */
372 /*
373  *  SIZE, SIZEFIELD and TAG are valid for allocated and free blocks,
374  *  REALSIZE is valid for allocated blocks when debugging, and NEXT and
375  *  PREV are valid for free blocks. We could speed things up by making
376  *  the free list singly linked when not debugging - the double link are
377  *  just so we can check for pointer validity. (PREV(NEXT(ep)) == ep and
378  *  NEXT(PREV(ep)) == ep). FREESIZE is used only to get the size field
379  *  from FREE blocks - in this implementation, free blocks have a tag
380  *  bit of 0 so no masking is necessary to operate on the SIZEFIELD when
381  *  a block is free. We take advantage of that as a minor optimization.
382  */
383 #define SIZEFIELD(p)            ((p)->size)
384 #define SIZE(p)                 ((size_t) (SIZEFIELD(p) & SIZEMASK))
385 #define ALLOCMASK(n)            ((n) | ALLOCED)
386 #define FREESIZE(p)             SIZEFIELD(p)
387 /*
388  *  FREEMASK should be (n) & SIZEMASK, but since (n) will always have
389  *  the hi bit off after the conversion from bytes requested by the user
390  *  to words.
391  */
392 #define FREEMASK(n)             (n)
393 #define TAG(p)                  (SIZEFIELD(p) & ~SIZEMASK)
394
395 #ifdef CSRI_DEBUG
396 #define REALSIZE(sp)            (((sp)+1)->size)
397 #endif                          /* CSRI_DEBUG */
398
399 #define NEXT(ep)                (((ep)-1)->next)
400 #define PREV(ep)                (((ep)-2)->prev)
401
402 /*
403  *  HEADERWORDS is the real block header in an allocated block - the
404  *  free block header uses extra words in the block itself
405  */
406 #ifdef CSRI_DEBUG
407 #define HEADERWORDS             2       /* Start boundary tag + real size in bytes */
408 #else                           /* ! CSRI_DEBUG */
409 #define HEADERWORDS             1       /* Start boundary tag */
410 #endif                          /* CSRI_DEBUG */
411
412 #define TRAILERWORDS            1
413
414 #define FREEHEADERWORDS         1       /* Start boundary tag */
415
416 #define FREETRAILERWORDS        3       /* next and prev, end boundary tag */
417
418 #define ALLOC_OVERHEAD  (HEADERWORDS + TRAILERWORDS)
419 #define FREE_OVERHEAD   (FREEHEADERWORDS + FREETRAILERWORDS)
420
421 /*
422  *  The allocator views memory as a list of non-contiguous arenas. (If
423  *  successive sbrks() return contiguous blocks, they are colaesced into
424  *  one arena - if a program never calls sbrk() other than malloc(),
425  *  then there should only be one arena. This malloc will however
426  *  happily coexist with other allocators calling sbrk() and uses only
427  *  the blocks given to it by sbrk. It expects the same courtesy from
428  *  other allocators. The arenas are chained into a linked list using
429  *  the first word of each block as a pointer to the next arena. The
430  *  second word of the arena, and the last word, contain fake boundary
431  *  tags that are permanantly marked allocated, so that no attempt is
432  *  made to coalesce past them. See the code in dumpheap for more info.
433  */
434 #define ARENASTART              2       /* next ptr + fake start tag */
435
436 #ifdef CSRI_DEBUG
437   /*
438    * 1 for prev link in free block - next link is absorbed by header
439    * REALSIZE word
440    */
441 #define FIXEDOVERHEAD           (1 + ALLOC_OVERHEAD)
442 #else                           /* ! CSRI_DEBUG */
443   /* 1 for prev link, 1 for next link, + header and trailer */
444 #define FIXEDOVERHEAD           (2 + ALLOC_OVERHEAD)
445 #endif                          /* CSRI_DEBUG */
446
447 /*
448  * Check that pointer is safe to dereference i.e. actually points
449  * somewhere within the heap and is properly aligned.
450  */
451 #define PTR_IN_HEAP(p) \
452         ((p) > _malloc_loword && (p) < _malloc_hiword && \
453          ALIGNPTR(p) == ((univptr_t) (p)))
454
455 /* Check that the size field is valid */
456 #define VALID_START_SIZE_FIELD(sp) \
457         (PTR_IN_HEAP((sp) + SIZE(sp) - 1) && \
458          SIZEFIELD(sp) == SIZEFIELD((sp) + SIZE(sp) - 1))
459
460 #define VALID_END_SIZE_FIELD(ep) \
461         (PTR_IN_HEAP((ep) - SIZE(ep) + 1) && \
462          SIZEFIELD(ep) == SIZEFIELD((ep) - SIZE(ep) + 1))
463
464
465 #define ulong unsigned long
466
467 #ifdef CSRI_DEBUG
468   /*
469    * Byte that is stored at the end of each block if the requested size
470    * of the block is not a multiple of sizeof(Word). (If it is a multiple
471    * of sizeof(Word), then we don't need to put the magic because the
472    * endboundary tag will be corrupted and the tests that check the
473    * validity of the boundary tag should detect it
474    */
475 #ifndef u_char
476 #define u_char          unsigned char
477 #endif                          /* u_char */
478
479 #define MAGIC_BYTE              ((u_char) '\252')
480
481   /*
482    * Check if size of the block is identical to requested size. Typical
483    * tests will be of the form DONT_NEED_MAGIC(p) || something for
484    * short-circuited protection, because blocks where DONT_NEED_MAGIC is
485    * true will be tested for boundary tag detection so we don't need the
486    * magic byte at the end.
487    */
488 #define DONT_NEED_MAGIC(sp) \
489         (REALSIZE(sp) == ((SIZE(sp) - ALLOC_OVERHEAD) * sizeof(Word)))
490
491   /*
492    * VALID_REALSIZE must not be used if either DONT_NEED_MAGIC or TOO_SMALL
493    * are true
494    */
495 #define VALID_REALSIZE(sp) \
496         (REALSIZE(sp) < ((SIZE(sp) - ALLOC_OVERHEAD)*sizeof(Word)))
497
498   /* Location of the magic byte */
499 #define MAGIC_PTR(sp)           ((u_char *)((sp) + HEADERWORDS) + REALSIZE(sp))
500
501   /*
502    * malloc code should only use the next three macros SET_REALSIZE,
503    * VALID_MAGIC and TOO_SMALL, since they are the only ones which have
504    * non-CSRI_DEBUG (null) alternatives
505    */
506
507   /* Macro sets the realsize of a block if necessary */
508 #define SET_REALSIZE(sp, n) \
509         (TOO_SMALL(sp) ? 0 : \
510          (REALSIZE(sp) = (n), \
511           DONT_NEED_MAGIC(sp) || (*MAGIC_PTR(sp) = MAGIC_BYTE)))
512
513   /* Macro tests that the magic byte is valid if it exists */
514 #define VALID_MAGIC(sp) \
515         (TOO_SMALL(sp) || DONT_NEED_MAGIC(sp) || \
516          (VALID_REALSIZE(sp) && (*MAGIC_PTR(sp) == MAGIC_BYTE)))
517
518   /*
519    * Bytes left over memalign may be too small to hold the real size, they
520    * may be 1 word, i.e start and end tag stored in same word!
521    */
522 #define TOO_SMALL(sp) (SIZE(sp) < ALLOC_OVERHEAD)
523
524 #else                           /* ! CSRI_DEBUG */
525 #define SET_REALSIZE(sp, n)
526 #define VALID_MAGIC(sp) (1)
527 #define TOO_SMALL(sp)           (0)
528 #endif                          /* CSRI_DEBUG */
529
530 /*
531  *  Check that a free list ptr points to a block with something pointing
532  *  back. This is the only reason we use a doubly linked free list.
533  */
534 #define VALID_NEXT_PTR(ep) (PTR_IN_HEAP(NEXT(ep)) && PREV(NEXT(ep)) == (ep))
535 #define VALID_PREV_PTR(ep) (PTR_IN_HEAP(PREV(ep)) && NEXT(PREV(ep)) == (ep))
536
537 /*
538  *  quick bit-arithmetic to check a number (including 1) is a power of two.
539  */
540 #define is_power_of_2(x) ((((x) - 1) & (x)) == 0)
541
542 /*
543  *  An array to try and keep track of the distribution of sizes of
544  *  malloc'ed blocks
545  */
546 #ifdef CSRI_PROFILESIZES
547 #define MAXPROFILESIZE 2*1024
548 #define COUNTSIZE(nw) (_malloc_scount[((nw) < MAXPROFILESIZE) ? (nw) : 0]++)
549 #else
550 #define COUNTSIZE(nw)
551 #endif
552
553 #ifndef USESTDIO
554   /*
555    * Much better not to use stdio - stdio calls malloc() and can
556    * therefore cause problems when debugging heap corruption. However,
557    * there's no guaranteed way to turn off buffering on a FILE pointer in
558    * ANSI.  This is a vile kludge!
559    */
560 #define fputs(s, f)             write(fileno(f), (s), strlen(s))
561 #define setvbuf(f, s, n, l)     __nothing()
562 #define fflush(f)               __nothing()
563 #endif                          /* USESTDIO */
564
565 #ifdef CSRI_TRACE
566   /*
567    * Prints a trace string. For convenience, x is an
568    * sprintf(_malloc_statsbuf, ...) or strcpy(_malloc_statsbuf, ...);
569    * something which puts the appropriate text to be printed in
570    * _malloc_statsbuf - ugly, but more convenient than making x a string.
571    */
572 #define PRCSRI_TRACE(x) \
573   if (_malloc_tracing) { \
574         (void) x; \
575         (void) fputs(_malloc_statsbuf, stderr); \
576   } else \
577         _malloc_tracing += 0
578 #else
579 #define PRCSRI_TRACE(x)
580 #endif
581
582 #ifdef CSRI_DEBUG
583 #define CHECKHEAP() \
584   if (_malloc_debugging >= 2) \
585         (void) mal_verify(_malloc_debugging >= 3); \
586   else \
587         _malloc_debugging += 0
588
589 #define CHECKFREEPTR(ep, msg) \
590   if (_malloc_debugging >= 1) { \
591         ASSERT_EP(PTR_IN_HEAP(ep), "pointer outside heap", (msg), (ep)); \
592         ASSERT_EP(TAG(ep) == FREE, "already-allocated block", (msg), (ep)); \
593         ASSERT_EP(VALID_END_SIZE_FIELD(ep), "corrupt block", msg, (ep)); \
594         ASSERT_EP(VALID_NEXT_PTR(ep), "corrupt link to next free block", (msg), (ep)); \
595         ASSERT_EP(VALID_PREV_PTR(ep), "corrupt link to previous free block", (msg), (ep)); \
596   } else \
597         _malloc_debugging += 0
598
599 #define CHECKALLOCPTR(sp, msg) \
600   if (_malloc_debugging >= 1) { \
601         ASSERT_SP(PTR_IN_HEAP(sp), "pointer outside heap", (msg), (sp)); \
602         ASSERT_SP(TAG(sp) != FREE, "already-freed block", (msg), (sp)); \
603         ASSERT_SP(VALID_START_SIZE_FIELD(sp), "corrupt block", (msg), (sp)); \
604         ASSERT_SP(VALID_MAGIC(sp), "block with end overwritten", (msg), (sp)); \
605   } else \
606         _malloc_debugging += 0
607
608 #else                           /* !CSRI_DEBUG */
609 #define CHECKHEAP()
610 #define CHECKFREEPTR(ep, msg)
611 #define CHECKALLOCPTR(sp, msg)
612 #endif                          /* CSRI_DEBUG */
613
614 #define FREEMAGIC               '\125'
615
616 /*
617  *  Memory functions but in words. We just call memset/memcpy, and hope
618  *  that someone has optimized them. If you are on pure 4.2BSD, either
619  *  redefine these in terms of bcopy/your own memcpy, or
620  *  get the functions from one of 4.3src/Henry Spencer's strings
621  *  package/C News src
622  */
623 #define MEMSET(p, c, n) \
624         (void) memset((univptr_t) (p), (c), (memsize_t) ((n) * sizeof(Word)))
625 #define MEMCPY(p1, p2, n) \
626         (void) memcpy((univptr_t) (p1), (univptr_t) (p2), \
627                                   (memsize_t) ((n) * sizeof(Word)))
628
629
630 #ifdef CSRI_DEBUG
631 #define DMEMSET(p, n)   MEMSET((p), FREEMAGIC, (n))
632 #else
633 #define DMEMSET(p, n)
634 #endif
635
636 /*
637  *  Thanks to Hugh Redelmeier for pointing out that an rcsid is "a
638  *  variable accessed in a way not obvious to the compiler", so should
639  *  be called volatile. Amazing - a use for const volatile...
640  */
641 #ifndef RCSID                   /* define RCSID(x) to nothing if don't want the rcs headers */
642 #if defined(lint) || defined(__STRICT_ANSI__)
643 #define RCSID(x)
644 #else
645 #define RCSID(x)                static const volatile char *rcsid = x ;
646 #endif
647 #endif
648
649 #endif /* __DEFS_H__ */                        /* Do not add anything after this line */
650
651 /*  Author: Mark Moraes <moraes@csri.toronto.edu> */
652
653 /*
654  *  All globals are names starting with _malloc, which should not clash
655  *  with anything else.
656  */
657 /*
658  *  Remember to initialize the variable in globals.c if you want, and
659  *  provide an alternative short name in globrename.h
660  */
661 /* $Id: csrimalloc.c 1.23.1.8 Wed, 26 May 2004 09:52:42 -0500 dunemush $ */
662 #ifndef __GLOBALRENAME_H__
663 #define __GLOBALRENAME_H__
664 /*
665  *  Renaming all external symbols that are internal to the malloc to be
666  *  unique within 6 characters for machines whose linkers just can't keep
667  *  up. We hope the cpp is smart enough - if not, get GNU cccp or the
668  *  cpp that comes with X Windows Version 11 Release 3.
669  */
670 #ifdef SHORTNAMES
671 #define _malloc_minchunk        __MAL1_minchunk
672
673 #define _malloc_rovers          __MAL2_rovers
674 #define _malloc_binmax          __MALH_binmax
675 #define _malloc_firstbin        __MALG_firstbin
676 #define _malloc_lastbin         __MAL3_lastbin
677 #define _malloc_hiword          __MAL4_hiword
678 #define _malloc_loword          __MAL5_loword
679
680 #define _malloc_sbrkunits       __MAL6_sbrkunits
681
682 #define _malloc_mem                     __MAL7_mem
683
684 #define _malloc_tracing         __MAL8_tracing
685 #define _malloc_statsbuf        __MALA_statsbuf
686
687 #define _malloc_leaktrace       __MALB_leaktrace
688
689 #ifdef CSRI_PROFILESIZES
690 #define _malloc_scount          __MALC_scount
691 #endif                          /* CSRI_PROFILESIZES */
692
693 #ifdef CSRI_DEBUG
694 /*
695  *  0 or 1 means checking all pointers before using them. Reasonably
696  *  thorough.  2 means check the entire heap on every call to
697  *  malloc/free/realloc/memalign. (the rest call these)
698  */
699 #define _malloc_debugging       __MALD_debugging
700 #endif                          /* CSRI_DEBUG */
701 #define _malloc_version         __MALE_version
702
703 #define _malloc_memfunc         __MALF_memfunc
704
705 #endif /* SHORTNAMES */                        /* Do not add anything after this line */
706 #endif /* __GLOBALRENAME_H__ */                         /* Do not add anything after this line */
707 const char *_malloc_version =
708   "CSRI, University of Toronto Malloc Version 1.18alpha";
709
710 /*
711  *  _malloc_minchunk is the minimum number of units that a block can be
712  *  cut down to.  If the difference between the required block size, and
713  *  the first available block is greater than _malloc_minchunk, the
714  *  block is chopped into two pieces, else the whole block is returned.
715  *  The larger this is, the less fragmentation there will be, but the
716  *  greater the waste. The actual minimum size of a block is therefore
717  *  _malloc_minchunk*sizeof(Word) This consists of one word each for the
718  *  boundary tags, one for the next and one for the prev pointers in a
719  *  free block.
720  */
721 size_t _malloc_minchunk = FIXEDOVERHEAD;
722
723 /*
724  * _malloc_rovers is the array of pointers into that each point 'someplace'
725  * into a list of free blocks smaller than a specified size. We start our
726  * search for a block from _malloc_rovers, thus starting the search at a
727  * different place everytime, rather than at the start of the list.  This
728  * improves performance considerably, sez Knuth
729  */
730 Word *_malloc_rovers[MAXBINS + 1] = { NULL };
731
732 const size_t _malloc_binmax[MAXBINS] = {
733   8, 16, 32, 64, 128, 256, 512, 1024
734 };
735
736 int _malloc_firstbin = 0;
737 int _malloc_lastbin = 0;
738 Word *_malloc_hiword = NULL;
739 Word *_malloc_loword = NULL;
740
741 /*
742  *  _malloc_sbrkunits is the multiple of sizeof(Word) to actually use in
743  *  sbrk() calls - _malloc_sbrkunits should be large enough that sbrk
744  *  isn't called too often, but small enough that any program that
745  *  mallocs a few bytes doesn't end up being very large. I've set it to
746  *  1K resulting in a sbrk'ed size of 8K. This is (coincidentally!) the
747  *  pagesize on Suns.  I think that this seems a reasonable number for
748  *  modern programs that malloc heavily. For small programs, you may
749  *  want to set it to a lower number.
750  */
751 size_t _malloc_sbrkunits = DEF_SBRKUNITS;
752
753 Word *_malloc_mem = NULL;
754
755 /*
756  *  Do not call any output routine other than fputs() - use sprintf() if
757  *  you want to format something before printing. We don't want stdio
758  *  calling malloc() if we can help it
759  */
760 int _malloc_tracing = 0;        /* No tracing */
761 char _malloc_statsbuf[128];
762
763 int _malloc_leaktrace = 0;
764
765 #ifdef CSRI_PROFILESIZES
766 int _malloc_scount[MAXPROFILESIZE];
767 #endif                          /* CSRI_PROFILESIZES */
768
769 #ifdef CSRI_DEBUG
770 /*
771  *  0 or 1 means checking all pointers before using them. Reasonably
772  *  thorough.  2 means check the entire heap on every call to
773  *  malloc/free/realloc/memalign. (the rest call these)
774  */
775 int _malloc_debugging = 0;
776 #endif                          /* CSRI_DEBUG */
777
778 univptr_t(*_malloc_memfunc) proto((size_t)) = _mal_sbrk;
779
780 #ifndef __GLOBALS_H__
781 #define __GLOBALS_H__
782 /*
783  *  Remember to initialize the variable in globals.c if you want, and
784  *  provide an alternative short name in globrename.h
785  */
786
787     extern
788       size_t
789       _malloc_minchunk;
790
791     extern Word *
792       _malloc_rovers[];
793     extern const
794       size_t
795       _malloc_binmax[];
796     extern int
797       _malloc_firstbin;
798     extern int
799       _malloc_lastbin;
800     extern Word *
801       _malloc_