From f701dc2abbcfb64c0c4d02ac899dccb03ee2b246 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 30 Sep 2011 10:07:40 -0700 Subject: Remove dependency on glibc malloc internals. * alloc.c (XMALLOC_OVERRUN_CHECK_OVERHEAD, XMALLOC_OVERRUN_CHECK_SIZE): Move back here from lisp.h, but with their new implementations. (XMALLOC_BASE_ALIGNMENT, COMMON_MULTIPLE, XMALLOC_HEADER_ALIGNMENT) (XMALLOC_OVERRUN_SIZE_SIZE): Move these new lisp.h macros here. * charset.c (charset_table_init): New static var. (syms_of_charset): Use it instead of xmalloc. This removes a dependency on glibc malloc internals. See Eli Zaretskii's comment in . * lisp.h (XMALLOC_OVERRUN_CHECK_OVERHEAD, XMALLOC_OVERRUN_CHECK_SIZE): Move back to alloc.c. (XMALLOC_BASE_ALIGNMENT, COMMON_MULTIPLE, XMALLOC_HEADER_ALIGNMENT) (XMALLOC_OVERRUN_SIZE_SIZE): Move to alloc.c. --- src/alloc.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'src/alloc.c') diff --git a/src/alloc.c b/src/alloc.c index 4c5094b8f48..ead5c8a8ca4 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -482,10 +482,53 @@ buffer_memory_full (EMACS_INT nbytes) xsignal (Qnil, Vmemory_signal_data); } -#ifdef XMALLOC_OVERRUN_CHECK + +#ifndef XMALLOC_OVERRUN_CHECK +#define XMALLOC_OVERRUN_CHECK_OVERHEAD 0 +#else /* Check for overrun in malloc'ed buffers by wrapping a header and trailer - around each block. */ + around each block. + + The header consists of XMALLOC_OVERRUN_CHECK_SIZE fixed bytes + followed by XMALLOC_OVERRUN_SIZE_SIZE bytes containing the original + block size in little-endian order. The trailer consists of + XMALLOC_OVERRUN_CHECK_SIZE fixed bytes. + + The header is used to detect whether this block has been allocated + through these functions, as some low-level libc functions may + bypass the malloc hooks. */ + +#define XMALLOC_OVERRUN_CHECK_SIZE 16 +#define XMALLOC_OVERRUN_CHECK_OVERHEAD \ + (2 * XMALLOC_OVERRUN_CHECK_SIZE + XMALLOC_OVERRUN_SIZE_SIZE) + +/* Define XMALLOC_OVERRUN_SIZE_SIZE so that (1) it's large enough to + hold a size_t value and (2) the header size is a multiple of the + alignment that Emacs needs for C types and for USE_LSB_TAG. */ +#define XMALLOC_BASE_ALIGNMENT \ + offsetof ( \ + struct { \ + union { long double d; intmax_t i; void *p; } u; \ + char c; \ + }, \ + c) +#ifdef USE_LSB_TAG +/* A common multiple of the positive integers A and B. Ideally this + would be the least common multiple, but there's no way to do that + as a constant expression in C, so do the best that we can easily do. */ +# define COMMON_MULTIPLE(a, b) \ + ((a) % (b) == 0 ? (a) : (b) % (a) == 0 ? (b) : (a) * (b)) +# define XMALLOC_HEADER_ALIGNMENT \ + COMMON_MULTIPLE (1 << GCTYPEBITS, XMALLOC_BASE_ALIGNMENT) +#else +# define XMALLOC_HEADER_ALIGNMENT XMALLOC_BASE_ALIGNMENT +#endif +#define XMALLOC_OVERRUN_SIZE_SIZE \ + (((XMALLOC_OVERRUN_CHECK_SIZE + sizeof (size_t) \ + + XMALLOC_HEADER_ALIGNMENT - 1) \ + / XMALLOC_HEADER_ALIGNMENT * XMALLOC_HEADER_ALIGNMENT) \ + - XMALLOC_OVERRUN_CHECK_SIZE) static char const xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE] = { '\x9a', '\x9b', '\xae', '\xaf', -- cgit v1.2.1