| 1 |
|
|---|
| 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" |
|---|
| 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 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 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 |
|
|---|
| 91 |
|
|---|
| 92 |
extern void abort proto((void)); |
|---|
| 93 |
extern void exit proto((int)); |
|---|
| 94 |
extern char *getenv proto((const char *)); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
#if 0 |
|---|
| 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 |
|
|---|
| 113 |
|
|---|
| 114 |
#if 0 |
|---|
| 115 |
|
|---|
| 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 |
|
|---|
| 130 |
extern int write |
|---|
| 131 |
proto((int , const char * , int )); |
|---|
| 132 |
extern int open proto((const char * , int , ...)); |
|---|
| 133 |
|
|---|
| 134 |
#define caddr_t char * |
|---|
| 135 |
extern caddr_t sbrk proto((int)); |
|---|
| 136 |
|
|---|
| 137 |
#ifdef _AIX |
|---|
| 138 |
extern caddr_t sbrk proto((int)); |
|---|
| 139 |
#endif |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 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 |
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 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 |
|
|---|
| 236 |
#define ALIGN long foo[2] |
|---|
| 237 |
#define NALIGN 8 |
|---|
| 238 |
#endif |
|---|
| 239 |
|
|---|
| 240 |
#if defined(__alpha) |
|---|
| 241 |
|
|---|
| 242 |
#define ALIGN long foo |
|---|
| 243 |
#define NALIGN 8 |
|---|
| 244 |
#endif |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
#ifndef ALIGN |
|---|
| 248 |
#define ALIGN long foo |
|---|
| 249 |
#define NALIGN 4 |
|---|
| 250 |
#endif |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 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 |
|
|---|
| 257 |
#define ALIGNPTR(X) SIMPLEALIGN(X, NALIGN) |
|---|
| 258 |
#else |
|---|
| 259 |
|
|---|
| 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 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 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 |
|
|---|
| 289 |
#if gcos |
|---|
| 290 |
#define BITSPERBYTE 9 |
|---|
| 291 |
#else |
|---|
| 292 |
#define BITSPERBYTE 8 |
|---|
| 293 |
#endif |
|---|
| 294 |
#endif |
|---|
| 295 |
|
|---|
| 296 |
#ifndef BITS |
|---|
| 297 |
#define BITS(type) (BITSPERBYTE * (int) sizeof(type)) |
|---|
| 298 |
#endif |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
#define HIBITSZ (((size_t) 1) << (BITS(size_t) - 1)) |
|---|
| 302 |
|
|---|
| 303 |
#endif |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 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 { |
|---|
| 320 |
size_t size; |
|---|
| 321 |
union word *next; |
|---|
| 322 |
union word *prev; |
|---|
| 323 |
univptr_t ptr; |
|---|
| 324 |
char c; |
|---|
| 325 |
int i; |
|---|
| 326 |
char *cp; |
|---|
| 327 |
char **cpp; |
|---|
| 328 |
int *ip; |
|---|
| 329 |
int **ipp; |
|---|
| 330 |
ALIGN; |
|---|
| 331 |
}; |
|---|
| 332 |
|
|---|
| 333 |
typedef union word Word; |
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 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 |
|---|
| 398 |
|
|---|
| 399 |
#define NEXT(ep) (((ep)-1)->next) |
|---|
| 400 |
#define PREV(ep) (((ep)-2)->prev) |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
#ifdef CSRI_DEBUG |
|---|
| 407 |
#define HEADERWORDS 2 |
|---|
| 408 |
#else |
|---|
| 409 |
#define HEADERWORDS 1 |
|---|
| 410 |
#endif |
|---|
| 411 |
|
|---|
| 412 |
#define TRAILERWORDS 1 |
|---|
| 413 |
|
|---|
| 414 |
#define FREEHEADERWORDS 1 |
|---|
| 415 |
|
|---|
| 416 |
#define FREETRAILERWORDS 3 |
|---|
| 417 |
|
|---|
| 418 |
#define ALLOC_OVERHEAD (HEADERWORDS + TRAILERWORDS) |
|---|
| 419 |
#define FREE_OVERHEAD (FREEHEADERWORDS + FREETRAILERWORDS) |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
#define ARENASTART 2 |
|---|
| 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 |
|---|
| 443 |
|
|---|
| 444 |
#define FIXEDOVERHEAD (2 + ALLOC_OVERHEAD) |
|---|
| 445 |
#endif |
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
#define PTR_IN_HEAP(p) \ |
|---|
| 452 |
((p) > _malloc_loword && (p) < _malloc_hiword && \ |
|---|
| 453 |
ALIGNPTR(p) == ((univptr_t) (p))) |
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 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 |
|---|
| 478 |
|
|---|
| 479 |
#define MAGIC_BYTE ((u_char) '\252') |
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
#define DONT_NEED_MAGIC(sp) \ |
|---|
| 489 |
(REALSIZE(sp) == ((SIZE(sp) - ALLOC_OVERHEAD) * sizeof(Word))) |
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
#define VALID_REALSIZE(sp) \ |
|---|
| 496 |
(REALSIZE(sp) < ((SIZE(sp) - ALLOC_OVERHEAD)*sizeof(Word))) |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
#define MAGIC_PTR(sp) ((u_char *)((sp) + HEADERWORDS) + REALSIZE(sp)) |
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
#define TOO_SMALL(sp) (SIZE(sp) < ALLOC_OVERHEAD) |
|---|
| 523 |
|
|---|
| 524 |
#else |
|---|
| 525 |
#define SET_REALSIZE(sp, n) |
|---|
| 526 |
#define VALID_MAGIC(sp) (1) |
|---|
| 527 |
#define TOO_SMALL(sp) (0) |
|---|
| 528 |
#endif |
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 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 |
|
|---|
| 539 |
|
|---|
| 540 |
#define is_power_of_2(x) ((((x) - 1) & (x)) == 0) |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 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 |
|---|
| 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 |
|---|
| 609 |
#define CHECKHEAP() |
|---|
| 610 |
#define CHECKFREEPTR(ep, msg) |
|---|
| 611 |
#define CHECKALLOCPTR(sp, msg) |
|---|
| 612 |
#endif |
|---|
| 613 |
|
|---|
| 614 |
#define FREEMAGIC '\125' |
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 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 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
#ifndef RCSID |
|---|
| 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 |
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 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 |
|---|
| 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 |
|---|
| 701 |
#define _malloc_version __MALE_version |
|---|
| 702 |
|
|---|
| 703 |
#define _malloc_memfunc __MALF_memfunc |
|---|
| 704 |
|
|---|
| 705 |
#endif |
|---|
| 706 |
#endif |
|---|
| 707 |
const char *_malloc_version = |
|---|
| 708 |
"CSRI, University of Toronto Malloc Version 1.18alpha"; |
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
size_t _malloc_minchunk = FIXEDOVERHEAD; |
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 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 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
size_t _malloc_sbrkunits = DEF_SBRKUNITS; |
|---|
| 752 |
|
|---|
| 753 |
Word *_malloc_mem = NULL; |
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
int _malloc_tracing = 0; |
|---|
| 761 |
char _malloc_statsbuf[128]; |
|---|
| 762 |
|
|---|
| 763 |
int _malloc_leaktrace = 0; |
|---|
| 764 |
|
|---|
| 765 |
#ifdef CSRI_PROFILESIZES |
|---|
| 766 |
int _malloc_scount[MAXPROFILESIZE]; |
|---|
| 767 |
#endif |
|---|
| 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 |
|---|
| 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_ |
|---|