aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorPaul Eggert2016-01-30 14:20:57 -0800
committerPaul Eggert2016-01-30 15:26:08 -0800
commita4817d834e7d125d31049dfb6fd0a0df4782bad0 (patch)
treeb2e63ff90a6307c20982e55fd779f670c28f6244 /src/gmalloc.c
parente4cd4a76a621927bc155a18353ef7fd09133887d (diff)
downloademacs-a4817d834e7d125d31049dfb6fd0a0df4782bad0.tar.gz
emacs-a4817d834e7d125d31049dfb6fd0a0df4782bad0.zip
Include <malloc.h> when advisable
This should help insulate us better from future glibc changes. It is good hygiene to include .h files for APIs that Emacs uses. Fix type clashes between Emacs and GNU <malloc.h> (Bug#22086). * configure.ac: Check for malloc.h. * src/alloc.c: Include <malloc.h> depending on HAVE_MALLOC_H, not on DOUG_LEA_MALLOC. * src/emacs.c, src/gmalloc.c (malloc_enable_thread): Remove decl (now in lisp.h). * src/gmalloc.c: Include stddef.h earlier, for ptrdiff_t. [emacs]: Include lisp.h. [HAVE_MALLOC_H]: Include <malloc.h>. (__MALLOC_HOOK_VOLATILE): New macro, if not already defined. (__after_morecore_hook, __malloc_initialize_hook, __morecore) (__default_morecore): [!HAVE_MALLOC_H]: New decls near non-inclusion of <malloc.h>. (calloc): Make it clear that the macro should not be used. Remove unused decl. (malloc_info): New macro, to avoid clash with glibc <malloc.h>. (__morecore, __default_morecore, __after_morecore_hook) (__malloc_extra_blocks, __malloc_initialize_hook, __free_hook) (__malloc_hook, __realloc_hook, __memalign_hook, memory_warnings): Remove later decls. (gmalloc_hook, gfree_hook, grealloc_hook): Rename from __malloc_hook, __free_hook, __realloc_hook to avoid type collision with glibc <malloc.h>. All uses changed. (gmalloc_hook): (__malloc_extra_blocks) [DOUG_LEA_MALLOC||HYBRID_MALLOC||SYSTEM_MALLOC]: Now static. (gmalloc_hook, __malloc_extra_blocks): Define even if [!HYBRID_MALLOC]. (__malloc_initialize_hook, __after_morecore_hook): Declare with types compatible with glibc. (__memalign_hook, hybrid_calloc) [HYBRID_MALLOC]: Remove. All uses removed. * src/lisp.h (__malloc_extra_blocks, malloc_enable_thread): New decls. * src/ralloc.c, src/vm-limit.c: Simplify includes and include <malloc.h> if available.
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c156
1 files changed, 67 insertions, 89 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index 4fd324686ba..4feff83a2d1 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -25,6 +25,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
25#define USE_PTHREAD 25#define USE_PTHREAD
26#endif 26#endif
27 27
28#include <stddef.h>
28#include <string.h> 29#include <string.h>
29#include <limits.h> 30#include <limits.h>
30#include <stdint.h> 31#include <stdint.h>
@@ -38,6 +39,26 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
38#include <w32heap.h> /* for sbrk */ 39#include <w32heap.h> /* for sbrk */
39#endif 40#endif
40 41
42#ifdef emacs
43# include "lisp.h"
44#endif
45
46#ifdef HAVE_MALLOC_H
47# if 4 < __GNUC__ + (2 <= __GNUC_MINOR__)
48# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
49# endif
50# include <malloc.h>
51#endif
52#ifndef __MALLOC_HOOK_VOLATILE
53# define __MALLOC_HOOK_VOLATILE volatile
54#endif
55#ifndef HAVE_MALLOC_H
56extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
57extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void);
58extern void *(*__morecore) (ptrdiff_t);
59extern void *__default_morecore (ptrdiff_t);
60#endif
61
41/* If HYBRID_MALLOC is defined, then temacs will use malloc, 62/* If HYBRID_MALLOC is defined, then temacs will use malloc,
42 realloc... as defined in this file (and renamed gmalloc, 63 realloc... as defined in this file (and renamed gmalloc,
43 grealloc... via the macros that follow). The dumped emacs, 64 grealloc... via the macros that follow). The dumped emacs,
@@ -58,9 +79,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
58#undef free 79#undef free
59#define malloc gmalloc 80#define malloc gmalloc
60#define realloc grealloc 81#define realloc grealloc
61#define calloc gcalloc 82#define calloc do_not_call_me /* Emacs never calls calloc. */
62#define aligned_alloc galigned_alloc 83#define aligned_alloc galigned_alloc
63#define free gfree 84#define free gfree
85#define malloc_info gmalloc_info
64 86
65#ifdef HYBRID_MALLOC 87#ifdef HYBRID_MALLOC
66# include "sheap.h" 88# include "sheap.h"
@@ -77,15 +99,6 @@ extern "C"
77{ 99{
78#endif 100#endif
79 101
80#include <stddef.h>
81
82/* Underlying allocation function; successive calls should
83 return contiguous pieces of memory. */
84extern void *(*__morecore) (ptrdiff_t size);
85
86/* Default value of `__morecore'. */
87extern void *__default_morecore (ptrdiff_t size);
88
89#ifdef HYBRID_MALLOC 102#ifdef HYBRID_MALLOC
90#define extern static 103#define extern static
91#endif 104#endif
@@ -95,9 +108,7 @@ extern void *malloc (size_t size) ATTRIBUTE_MALLOC_SIZE ((1));
95/* Re-allocate the previously allocated block 108/* Re-allocate the previously allocated block
96 in ptr, making the new block SIZE bytes long. */ 109 in ptr, making the new block SIZE bytes long. */
97extern void *realloc (void *ptr, size_t size) ATTRIBUTE_ALLOC_SIZE ((2)); 110extern void *realloc (void *ptr, size_t size) ATTRIBUTE_ALLOC_SIZE ((2));
98/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */ 111/* Free a block. */
99extern void *calloc (size_t nmemb, size_t size) ATTRIBUTE_MALLOC_SIZE ((1,2));
100/* Free a block allocated by `malloc', `realloc' or `calloc'. */
101extern void free (void *ptr); 112extern void free (void *ptr);
102 113
103/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ 114/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
@@ -107,11 +118,6 @@ extern void *memalign (size_t, size_t);
107extern int posix_memalign (void **, size_t, size_t); 118extern int posix_memalign (void **, size_t, size_t);
108#endif 119#endif
109 120
110#ifdef USE_PTHREAD
111/* Set up mutexes and make malloc etc. thread-safe. */
112extern void malloc_enable_thread (void);
113#endif
114
115/* The allocator divides the heap into blocks of fixed size; large 121/* The allocator divides the heap into blocks of fixed size; large
116 requests receive one or more whole blocks, and small requests 122 requests receive one or more whole blocks, and small requests
117 receive a fragment of a block. Fragment sizes are powers of two, 123 receive a fragment of a block. Fragment sizes are powers of two,
@@ -243,26 +249,11 @@ extern int _malloc_thread_enabled_p;
243#define UNLOCK_ALIGNED_BLOCKS() 249#define UNLOCK_ALIGNED_BLOCKS()
244#endif 250#endif
245 251
246/* If not NULL, this function is called after each time
247 `__morecore' is called to increase the data size. */
248extern void (*__after_morecore_hook) (void);
249
250/* Number of extra blocks to get each time we ask for more core.
251 This reduces the frequency of calling `(*__morecore)'. */
252extern size_t __malloc_extra_blocks;
253
254/* Nonzero if `malloc' has been called and done its initialization. */ 252/* Nonzero if `malloc' has been called and done its initialization. */
255extern int __malloc_initialized; 253extern int __malloc_initialized;
256/* Function called to initialize malloc data structures. */ 254/* Function called to initialize malloc data structures. */
257extern int __malloc_initialize (void); 255extern int __malloc_initialize (void);
258 256
259/* Hooks for debugging versions. */
260extern void (*__malloc_initialize_hook) (void);
261extern void (*__free_hook) (void *ptr);
262extern void *(*__malloc_hook) (size_t size);
263extern void *(*__realloc_hook) (void *ptr, size_t size);
264extern void *(*__memalign_hook) (size_t size, size_t alignment);
265
266#ifdef GC_MCHECK 257#ifdef GC_MCHECK
267 258
268/* Return values for `mprobe': these are the kinds of inconsistencies that 259/* Return values for `mprobe': these are the kinds of inconsistencies that
@@ -304,9 +295,6 @@ struct mstats
304/* Pick up the current statistics. */ 295/* Pick up the current statistics. */
305extern struct mstats mstats (void); 296extern struct mstats mstats (void);
306 297
307/* Call WARNFUN with a warning message when memory usage is high. */
308extern void memory_warnings (void *start, void (*warnfun) (const char *));
309
310#endif 298#endif
311 299
312#undef extern 300#undef extern
@@ -337,13 +325,11 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
337 325
338#include <errno.h> 326#include <errno.h>
339 327
340void *(*__morecore) (ptrdiff_t size) = __default_morecore; 328/* Debugging hook for 'malloc'. */
329static void *(*__MALLOC_HOOK_VOLATILE gmalloc_hook) (size_t);
341 330
342#ifndef HYBRID_MALLOC 331#ifndef HYBRID_MALLOC
343 332
344/* Debugging hook for `malloc'. */
345void *(*__malloc_hook) (size_t size);
346
347/* Pointer to the base of the first block. */ 333/* Pointer to the base of the first block. */
348char *_heapbase; 334char *_heapbase;
349 335
@@ -368,10 +354,9 @@ size_t _bytes_free;
368/* Are you experienced? */ 354/* Are you experienced? */
369int __malloc_initialized; 355int __malloc_initialized;
370 356
371size_t __malloc_extra_blocks; 357void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void);
372 358void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
373void (*__malloc_initialize_hook) (void); 359void *(*__morecore) (ptrdiff_t);
374void (*__after_morecore_hook) (void);
375 360
376#else 361#else
377 362
@@ -379,6 +364,13 @@ static struct list _fraghead[BLOCKLOG];
379 364
380#endif /* HYBRID_MALLOC */ 365#endif /* HYBRID_MALLOC */
381 366
367/* Number of extra blocks to get each time we ask for more core.
368 This reduces the frequency of calling `(*__morecore)'. */
369#if defined DOUG_LEA_MALLOC || defined HYBRID_MALLOC || defined SYSTEM_MALLOC
370static
371#endif
372size_t __malloc_extra_blocks;
373
382/* Number of info entries. */ 374/* Number of info entries. */
383static size_t heapsize; 375static size_t heapsize;
384 376
@@ -935,15 +927,15 @@ malloc (size_t size)
935 if (!__malloc_initialized && !__malloc_initialize ()) 927 if (!__malloc_initialized && !__malloc_initialize ())
936 return NULL; 928 return NULL;
937 929
938 /* Copy the value of __malloc_hook to an automatic variable in case 930 /* Copy the value of gmalloc_hook to an automatic variable in case
939 __malloc_hook is modified in another thread between its 931 gmalloc_hook is modified in another thread between its
940 NULL-check and the use. 932 NULL-check and the use.
941 933
942 Note: Strictly speaking, this is not a right solution. We should 934 Note: Strictly speaking, this is not a right solution. We should
943 use mutexes to access non-read-only variables that are shared 935 use mutexes to access non-read-only variables that are shared
944 among multiple threads. We just leave it for compatibility with 936 among multiple threads. We just leave it for compatibility with
945 glibc malloc (i.e., assignments to __malloc_hook) for now. */ 937 glibc malloc (i.e., assignments to gmalloc_hook) for now. */
946 hook = __malloc_hook; 938 hook = gmalloc_hook;
947 return (hook != NULL ? *hook : _malloc_internal) (size); 939 return (hook != NULL ? *hook : _malloc_internal) (size);
948} 940}
949 941
@@ -995,10 +987,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
995 The author may be reached (Email) at the address mike@ai.mit.edu, 987 The author may be reached (Email) at the address mike@ai.mit.edu,
996 or (US mail) as Mike Haertel c/o Free Software Foundation. */ 988 or (US mail) as Mike Haertel c/o Free Software Foundation. */
997 989
990/* Debugging hook for free. */
991static void (*__MALLOC_HOOK_VOLATILE gfree_hook) (void *);
998 992
999#ifndef HYBRID_MALLOC 993#ifndef HYBRID_MALLOC
1000/* Debugging hook for free. */
1001void (*__free_hook) (void *__ptr);
1002 994
1003/* List of blocks allocated by aligned_alloc. */ 995/* List of blocks allocated by aligned_alloc. */
1004struct alignlist *_aligned_blocks = NULL; 996struct alignlist *_aligned_blocks = NULL;
@@ -1251,7 +1243,7 @@ _free_internal_nolock (void *ptr)
1251} 1243}
1252 1244
1253/* Return memory to the heap. 1245/* Return memory to the heap.
1254 Like `free' but don't call a __free_hook if there is one. */ 1246 Like 'free' but don't call a hook if there is one. */
1255void 1247void
1256_free_internal (void *ptr) 1248_free_internal (void *ptr)
1257{ 1249{
@@ -1265,7 +1257,7 @@ _free_internal (void *ptr)
1265void 1257void
1266free (void *ptr) 1258free (void *ptr)
1267{ 1259{
1268 void (*hook) (void *) = __free_hook; 1260 void (*hook) (void *) = gfree_hook;
1269 1261
1270 if (hook != NULL) 1262 if (hook != NULL)
1271 (*hook) (ptr); 1263 (*hook) (ptr);
@@ -1309,10 +1301,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
1309#define min(a, b) ((a) < (b) ? (a) : (b)) 1301#define min(a, b) ((a) < (b) ? (a) : (b))
1310#endif 1302#endif
1311 1303
1312#ifndef HYBRID_MALLOC
1313/* Debugging hook for realloc. */ 1304/* Debugging hook for realloc. */
1314void *(*__realloc_hook) (void *ptr, size_t size); 1305static void *(*grealloc_hook) (void *, size_t);
1315#endif
1316 1306
1317/* Resize the given region to the new size, returning a pointer 1307/* Resize the given region to the new size, returning a pointer
1318 to the (possibly moved) region. This is optimized for speed; 1308 to the (possibly moved) region. This is optimized for speed;
@@ -1456,7 +1446,7 @@ realloc (void *ptr, size_t size)
1456 if (!__malloc_initialized && !__malloc_initialize ()) 1446 if (!__malloc_initialized && !__malloc_initialize ())
1457 return NULL; 1447 return NULL;
1458 1448
1459 hook = __realloc_hook; 1449 hook = grealloc_hook;
1460 return (hook != NULL ? *hook : _realloc_internal) (ptr, size); 1450 return (hook != NULL ? *hook : _realloc_internal) (ptr, size);
1461} 1451}
1462/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc. 1452/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
@@ -1479,6 +1469,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
1479 1469
1480/* Allocate an array of NMEMB elements each SIZE bytes long. 1470/* Allocate an array of NMEMB elements each SIZE bytes long.
1481 The entire array is initialized to zeros. */ 1471 The entire array is initialized to zeros. */
1472#ifndef calloc
1482void * 1473void *
1483calloc (size_t nmemb, size_t size) 1474calloc (size_t nmemb, size_t size)
1484{ 1475{
@@ -1496,6 +1487,7 @@ calloc (size_t nmemb, size_t size)
1496 return memset (result, 0, bytes); 1487 return memset (result, 0, bytes);
1497 return result; 1488 return result;
1498} 1489}
1490#endif
1499/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. 1491/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
1500This file is part of the GNU C Library. 1492This file is part of the GNU C Library.
1501 1493
@@ -1541,6 +1533,9 @@ __default_morecore (ptrdiff_t increment)
1541 return NULL; 1533 return NULL;
1542 return result; 1534 return result;
1543} 1535}
1536
1537void *(*__morecore) (ptrdiff_t) = __default_morecore;
1538
1544/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc. 1539/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
1545 1540
1546This library is free software; you can redistribute it and/or 1541This library is free software; you can redistribute it and/or
@@ -1556,19 +1551,11 @@ General Public License for more details.
1556You should have received a copy of the GNU General Public 1551You should have received a copy of the GNU General Public
1557License along with this library. If not, see <http://www.gnu.org/licenses/>. */ 1552License along with this library. If not, see <http://www.gnu.org/licenses/>. */
1558 1553
1559#ifndef HYBRID_MALLOC
1560void *(*__memalign_hook) (size_t size, size_t alignment);
1561#endif
1562
1563void * 1554void *
1564aligned_alloc (size_t alignment, size_t size) 1555aligned_alloc (size_t alignment, size_t size)
1565{ 1556{
1566 void *result; 1557 void *result;
1567 size_t adj, lastadj; 1558 size_t adj, lastadj;
1568 void *(*hook) (size_t, size_t) = __memalign_hook;
1569
1570 if (hook)
1571 return (*hook) (alignment, size);
1572 1559
1573 /* Allocate a block with enough extra space to pad the block with up to 1560 /* Allocate a block with enough extra space to pad the block with up to
1574 (ALIGNMENT - 1) bytes if necessary. */ 1561 (ALIGNMENT - 1) bytes if necessary. */
@@ -1731,7 +1718,6 @@ valloc (size_t size)
1731/* Declare system malloc and friends. */ 1718/* Declare system malloc and friends. */
1732extern void *malloc (size_t size); 1719extern void *malloc (size_t size);
1733extern void *realloc (void *ptr, size_t size); 1720extern void *realloc (void *ptr, size_t size);
1734extern void *calloc (size_t nmemb, size_t size);
1735extern void free (void *ptr); 1721extern void free (void *ptr);
1736#ifdef HAVE_ALIGNED_ALLOC 1722#ifdef HAVE_ALIGNED_ALLOC
1737extern void *aligned_alloc (size_t alignment, size_t size); 1723extern void *aligned_alloc (size_t alignment, size_t size);
@@ -1750,14 +1736,6 @@ hybrid_malloc (size_t size)
1750 return gmalloc (size); 1736 return gmalloc (size);
1751} 1737}
1752 1738
1753void *
1754hybrid_calloc (size_t nmemb, size_t size)
1755{
1756 if (DUMPED)
1757 return calloc (nmemb, size);
1758 return gcalloc (nmemb, size);
1759}
1760
1761void 1739void
1762hybrid_free (void *ptr) 1740hybrid_free (void *ptr)
1763{ 1741{
@@ -1947,9 +1925,9 @@ freehook (void *ptr)
1947 else 1925 else
1948 hdr = NULL; 1926 hdr = NULL;
1949 1927
1950 __free_hook = old_free_hook; 1928 gfree_hook = old_free_hook;
1951 free (hdr); 1929 free (hdr);
1952 __free_hook = freehook; 1930 gfree_hook = freehook;
1953} 1931}
1954 1932
1955static void * 1933static void *
@@ -1957,9 +1935,9 @@ mallochook (size_t size)
1957{ 1935{
1958 struct hdr *hdr; 1936 struct hdr *hdr;
1959 1937
1960 __malloc_hook = old_malloc_hook; 1938 gmalloc_hook = old_malloc_hook;
1961 hdr = malloc (sizeof *hdr + size + 1); 1939 hdr = malloc (sizeof *hdr + size + 1);
1962 __malloc_hook = mallochook; 1940 gmalloc_hook = mallochook;
1963 if (hdr == NULL) 1941 if (hdr == NULL)
1964 return NULL; 1942 return NULL;
1965 1943
@@ -1985,13 +1963,13 @@ reallochook (void *ptr, size_t size)
1985 memset ((char *) ptr + size, FREEFLOOD, osize - size); 1963 memset ((char *) ptr + size, FREEFLOOD, osize - size);
1986 } 1964 }
1987 1965
1988 __free_hook = old_free_hook; 1966 gfree_hook = old_free_hook;
1989 __malloc_hook = old_malloc_hook; 1967 gmalloc_hook = old_malloc_hook;
1990 __realloc_hook = old_realloc_hook; 1968 grealloc_hook = old_realloc_hook;
1991 hdr = realloc (hdr, sizeof *hdr + size + 1); 1969 hdr = realloc (hdr, sizeof *hdr + size + 1);
1992 __free_hook = freehook; 1970 gfree_hook = freehook;
1993 __malloc_hook = mallochook; 1971 gmalloc_hook = mallochook;
1994 __realloc_hook = reallochook; 1972 grealloc_hook = reallochook;
1995 if (hdr == NULL) 1973 if (hdr == NULL)
1996 return NULL; 1974 return NULL;
1997 1975
@@ -2048,12 +2026,12 @@ mcheck (void (*func) (enum mcheck_status))
2048 /* These hooks may not be safely inserted if malloc is already in use. */ 2026 /* These hooks may not be safely inserted if malloc is already in use. */
2049 if (!__malloc_initialized && !mcheck_used) 2027 if (!__malloc_initialized && !mcheck_used)
2050 { 2028 {
2051 old_free_hook = __free_hook; 2029 old_free_hook = gfree_hook;
2052 __free_hook = freehook; 2030 gfree_hook = freehook;
2053 old_malloc_hook = __malloc_hook; 2031 old_malloc_hook = gmalloc_hook;
2054 __malloc_hook = mallochook; 2032 gmalloc_hook = mallochook;
2055 old_realloc_hook = __realloc_hook; 2033 old_realloc_hook = grealloc_hook;
2056 __realloc_hook = reallochook; 2034 grealloc_hook = reallochook;
2057 mcheck_used = 1; 2035 mcheck_used = 1;
2058 } 2036 }
2059 2037