diff options
| author | Paul Eggert | 2011-09-30 10:44:37 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-09-30 10:44:37 -0700 |
| commit | 535272bb73eff5a5db01301e5b60f6128d6dc788 (patch) | |
| tree | b41937c81a61bf737ef15efb434fc06338da25b4 /src/alloc.c | |
| parent | 38532ce6ecb33efee5f697886d9fd20a3c2cda01 (diff) | |
| parent | f701dc2abbcfb64c0c4d02ac899dccb03ee2b246 (diff) | |
| download | emacs-535272bb73eff5a5db01301e5b60f6128d6dc788.tar.gz emacs-535272bb73eff5a5db01301e5b60f6128d6dc788.zip | |
Merge from trunk.
Diffstat (limited to 'src/alloc.c')
| -rw-r--r-- | src/alloc.c | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/alloc.c b/src/alloc.c index 286421ebe47..bfb40e6c09a 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -490,36 +490,45 @@ buffer_memory_full (ptrdiff_t nbytes) | |||
| 490 | /* Check for overrun in malloc'ed buffers by wrapping a header and trailer | 490 | /* Check for overrun in malloc'ed buffers by wrapping a header and trailer |
| 491 | around each block. | 491 | around each block. |
| 492 | 492 | ||
| 493 | The header consists of 16 fixed bytes followed by | 493 | The header consists of XMALLOC_OVERRUN_CHECK_SIZE fixed bytes |
| 494 | XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original block size | 494 | followed by XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original |
| 495 | in little-endian order. The trailer consists of 16 fixed bytes. | 495 | block size in little-endian order. The trailer consists of |
| 496 | XMALLOC_OVERRUN_CHECK_SIZE fixed bytes. | ||
| 496 | 497 | ||
| 497 | The header is used to detect whether this block has been allocated | 498 | The header is used to detect whether this block has been allocated |
| 498 | through these functions -- as it seems that some low-level libc | 499 | through these functions, as some low-level libc functions may |
| 499 | functions may bypass the malloc hooks. | 500 | bypass the malloc hooks. */ |
| 500 | */ | ||
| 501 | |||
| 502 | 501 | ||
| 503 | #define XMALLOC_OVERRUN_CHECK_SIZE 16 | 502 | #define XMALLOC_OVERRUN_CHECK_SIZE 16 |
| 504 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD \ | 503 | #define XMALLOC_OVERRUN_CHECK_OVERHEAD \ |
| 505 | (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE) | 504 | (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE) |
| 506 | 505 | ||
| 507 | /* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to | 506 | /* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to |
| 508 | hold a size_t value and (2) so that the header size is a multiple | 507 | hold a size_t value and (2) the header size is a multiple of the |
| 509 | of the alignment that Emacs needs. */ | 508 | alignment that Emacs needs for C types and for USE_LSB_TAG. */ |
| 510 | #define alignof(type) offsetof (struct { char c; type x; }, x) | 509 | #define XMALLOC_BASE_ALIGNMENT \ |
| 511 | #define XMALLOC_BASE_ALIGNMENT \ | 510 | offsetof ( \ |
| 512 | max (max (alignof (double), alignof (long double)), alignof (intmax_t)) | 511 | struct { \ |
| 512 | union { long double d; intmax_t i; void *p; } u; \ | ||
| 513 | char c; \ | ||
| 514 | }, \ | ||
| 515 | c) | ||
| 513 | #ifdef USE_LSB_TAG | 516 | #ifdef USE_LSB_TAG |
| 514 | # define XMALLOC_HEADER_ALIGNMENT max (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT) | 517 | /* A common multiple of the positive integers A and B. Ideally this |
| 518 | would be the least common multiple, but there's no way to do that | ||
| 519 | as a constant expression in C, so do the best that we can easily do. */ | ||
| 520 | # define COMMON_MULTIPLE(a, b) \ | ||
| 521 | ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b)) | ||
| 522 | # define XMALLOC_HEADER_ALIGNMENT \ | ||
| 523 | COMMON_MULTIPLE (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT) | ||
| 515 | #else | 524 | #else |
| 516 | # define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT | 525 | # define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT |
| 517 | #endif | 526 | #endif |
| 518 | #define XMALLOC_OVERRUN_SIZE_SIZE \ | 527 | #define XMALLOC_OVERRUN_SIZE_SIZE \ |
| 519 | (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ | 528 | (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ |
| 520 | + XMALLOC_HEADER_ALIGNMENT - 1) \ | 529 | + XMALLOC_HEADER_ALIGNMENT - 1) \ |
| 521 | / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \ | 530 | / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \ |
| 522 | - XMALLOC_OVERRUN_CHECK_SIZE) | 531 | - XMALLOC_OVERRUN_CHECK_SIZE) |
| 523 | 532 | ||
| 524 | static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = | 533 | static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = |
| 525 | { '\x9a', '\x9b', '\xae', '\xaf', | 534 | { '\x9a', '\x9b', '\xae', '\xaf', |