summaryrefslogtreecommitdiff
path: root/src/alloc.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2011-09-30 10:07:40 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2011-09-30 10:07:40 -0700
commitf701dc2abbcfb64c0c4d02ac899dccb03ee2b246 (patch)
tree6d2cfa91e42aeaa89763a4c938fe7347431d5cca /src/alloc.c
parentcbc5ee224e558a2ee11c7abab293b2f2084852cd (diff)
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 <http://lists.gnu.org/archive/html/emacs-devel/2011-09/msg00815.html>. * 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.
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c47
1 files changed, 45 insertions, 2 deletions
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',