Commit a4817d83 authored by Paul Eggert's avatar Paul Eggert

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.
parent e4cd4a76
......@@ -1589,6 +1589,7 @@ fi
dnl checks for header files
AC_CHECK_HEADERS_ONCE(
malloc.h
sys/systeminfo.h
sys/sysinfo.h
coff.h pty.h
......
......@@ -59,6 +59,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include "dosfns.h" /* For dos_memory_info. */
#endif
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
#if (defined ENABLE_CHECKING \
&& defined HAVE_VALGRIND_VALGRIND_H \
&& !defined USE_VALGRIND)
......@@ -107,8 +111,6 @@ my_heap_start (void)
#ifdef DOUG_LEA_MALLOC
#include <malloc.h>
/* Specify maximum number of areas to mmap. It would be nice to use a
value that explicitly means "no limit". */
......
......@@ -99,7 +99,6 @@ typedef bool bool_bf;
#ifdef emacs
#define malloc hybrid_malloc
#define realloc hybrid_realloc
#define calloc hybrid_calloc
#define aligned_alloc hybrid_aligned_alloc
#define free hybrid_free
#endif
......
......@@ -134,8 +134,6 @@ bool might_dump;
extern void unexec_init_emacs_zone (void);
#endif
extern void malloc_enable_thread (void);
/* If true, Emacs should not attempt to use a window-specific code,
but instead should use the virtual terminal under which it was started. */
bool inhibit_window_system;
......
......@@ -25,6 +25,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#define USE_PTHREAD
#endif
#include <stddef.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
......@@ -38,6 +39,26 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#include <w32heap.h> /* for sbrk */
#endif
#ifdef emacs
# include "lisp.h"
#endif
#ifdef HAVE_MALLOC_H
# if 4 < __GNUC__ + (2 <= __GNUC_MINOR__)
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# endif
# include <malloc.h>
#endif
#ifndef __MALLOC_HOOK_VOLATILE
# define __MALLOC_HOOK_VOLATILE volatile
#endif
#ifndef HAVE_MALLOC_H
extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void);
extern void *(*__morecore) (ptrdiff_t);
extern void *__default_morecore (ptrdiff_t);
#endif
/* If HYBRID_MALLOC is defined, then temacs will use malloc,
realloc... as defined in this file (and renamed gmalloc,
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/>.
#undef free
#define malloc gmalloc
#define realloc grealloc
#define calloc gcalloc
#define calloc do_not_call_me /* Emacs never calls calloc. */
#define aligned_alloc galigned_alloc
#define free gfree
#define malloc_info gmalloc_info
#ifdef HYBRID_MALLOC
# include "sheap.h"
......@@ -77,15 +99,6 @@ extern "C"
{
#endif
#include <stddef.h>
/* Underlying allocation function; successive calls should
return contiguous pieces of memory. */
extern void *(*__morecore) (ptrdiff_t size);
/* Default value of `__morecore'. */
extern void *__default_morecore (ptrdiff_t size);
#ifdef HYBRID_MALLOC
#define extern static
#endif
......@@ -95,9 +108,7 @@ extern void *malloc (size_t size) ATTRIBUTE_MALLOC_SIZE ((1));
/* Re-allocate the previously allocated block
in ptr, making the new block SIZE bytes long. */
extern void *realloc (void *ptr, size_t size) ATTRIBUTE_ALLOC_SIZE ((2));
/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
extern void *calloc (size_t nmemb, size_t size) ATTRIBUTE_MALLOC_SIZE ((1,2));
/* Free a block allocated by `malloc', `realloc' or `calloc'. */
/* Free a block. */
extern void free (void *ptr);
/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
......@@ -107,11 +118,6 @@ extern void *memalign (size_t, size_t);
extern int posix_memalign (void **, size_t, size_t);
#endif
#ifdef USE_PTHREAD
/* Set up mutexes and make malloc etc. thread-safe. */
extern void malloc_enable_thread (void);
#endif
/* The allocator divides the heap into blocks of fixed size; large
requests receive one or more whole blocks, and small requests
receive a fragment of a block. Fragment sizes are powers of two,
......@@ -243,26 +249,11 @@ extern int _malloc_thread_enabled_p;
#define UNLOCK_ALIGNED_BLOCKS()
#endif
/* If not NULL, this function is called after each time
`__morecore' is called to increase the data size. */
extern void (*__after_morecore_hook) (void);
/* Number of extra blocks to get each time we ask for more core.
This reduces the frequency of calling `(*__morecore)'. */
extern size_t __malloc_extra_blocks;
/* Nonzero if `malloc' has been called and done its initialization. */
extern int __malloc_initialized;
/* Function called to initialize malloc data structures. */
extern int __malloc_initialize (void);
/* Hooks for debugging versions. */
extern void (*__malloc_initialize_hook) (void);
extern void (*__free_hook) (void *ptr);
extern void *(*__malloc_hook) (size_t size);
extern void *(*__realloc_hook) (void *ptr, size_t size);
extern void *(*__memalign_hook) (size_t size, size_t alignment);
#ifdef GC_MCHECK
/* Return values for `mprobe': these are the kinds of inconsistencies that
......@@ -304,9 +295,6 @@ struct mstats
/* Pick up the current statistics. */
extern struct mstats mstats (void);
/* Call WARNFUN with a warning message when memory usage is high. */
extern void memory_warnings (void *start, void (*warnfun) (const char *));
#endif
#undef extern
......@@ -337,13 +325,11 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#include <errno.h>
void *(*__morecore) (ptrdiff_t size) = __default_morecore;
/* Debugging hook for 'malloc'. */
static void *(*__MALLOC_HOOK_VOLATILE gmalloc_hook) (size_t);
#ifndef HYBRID_MALLOC
/* Debugging hook for `malloc'. */
void *(*__malloc_hook) (size_t size);
/* Pointer to the base of the first block. */
char *_heapbase;
......@@ -368,10 +354,9 @@ size_t _bytes_free;
/* Are you experienced? */
int __malloc_initialized;
size_t __malloc_extra_blocks;
void (*__malloc_initialize_hook) (void);
void (*__after_morecore_hook) (void);
void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void);
void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
void *(*__morecore) (ptrdiff_t);
#else
......@@ -379,6 +364,13 @@ static struct list _fraghead[BLOCKLOG];
#endif /* HYBRID_MALLOC */
/* Number of extra blocks to get each time we ask for more core.
This reduces the frequency of calling `(*__morecore)'. */
#if defined DOUG_LEA_MALLOC || defined HYBRID_MALLOC || defined SYSTEM_MALLOC
static
#endif
size_t __malloc_extra_blocks;
/* Number of info entries. */
static size_t heapsize;
......@@ -935,15 +927,15 @@ malloc (size_t size)
if (!__malloc_initialized && !__malloc_initialize ())
return NULL;
/* Copy the value of __malloc_hook to an automatic variable in case
__malloc_hook is modified in another thread between its
/* Copy the value of gmalloc_hook to an automatic variable in case
gmalloc_hook is modified in another thread between its
NULL-check and the use.
Note: Strictly speaking, this is not a right solution. We should
use mutexes to access non-read-only variables that are shared
among multiple threads. We just leave it for compatibility with
glibc malloc (i.e., assignments to __malloc_hook) for now. */
hook = __malloc_hook;
glibc malloc (i.e., assignments to gmalloc_hook) for now. */
hook = gmalloc_hook;
return (hook != NULL ? *hook : _malloc_internal) (size);
}
......@@ -995,10 +987,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
The author may be reached (Email) at the address mike@ai.mit.edu,
or (US mail) as Mike Haertel c/o Free Software Foundation. */
/* Debugging hook for free. */
static void (*__MALLOC_HOOK_VOLATILE gfree_hook) (void *);
#ifndef HYBRID_MALLOC
/* Debugging hook for free. */
void (*__free_hook) (void *__ptr);
/* List of blocks allocated by aligned_alloc. */
struct alignlist *_aligned_blocks = NULL;
......@@ -1251,7 +1243,7 @@ _free_internal_nolock (void *ptr)
}
/* Return memory to the heap.
Like `free' but don't call a __free_hook if there is one. */
Like 'free' but don't call a hook if there is one. */
void
_free_internal (void *ptr)
{
......@@ -1265,7 +1257,7 @@ _free_internal (void *ptr)
void
free (void *ptr)
{
void (*hook) (void *) = __free_hook;
void (*hook) (void *) = gfree_hook;
if (hook != NULL)
(*hook) (ptr);
......@@ -1309,10 +1301,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef HYBRID_MALLOC
/* Debugging hook for realloc. */
void *(*__realloc_hook) (void *ptr, size_t size);
#endif
static void *(*grealloc_hook) (void *, size_t);
/* Resize the given region to the new size, returning a pointer
to the (possibly moved) region. This is optimized for speed;
......@@ -1456,7 +1446,7 @@ realloc (void *ptr, size_t size)
if (!__malloc_initialized && !__malloc_initialize ())
return NULL;
hook = __realloc_hook;
hook = grealloc_hook;
return (hook != NULL ? *hook : _realloc_internal) (ptr, size);
}
/* 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/>.
/* Allocate an array of NMEMB elements each SIZE bytes long.
The entire array is initialized to zeros. */
#ifndef calloc
void *
calloc (size_t nmemb, size_t size)
{
......@@ -1496,6 +1487,7 @@ calloc (size_t nmemb, size_t size)
return memset (result, 0, bytes);
return result;
}
#endif
/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
This file is part of the GNU C Library.
......@@ -1541,6 +1533,9 @@ __default_morecore (ptrdiff_t increment)
return NULL;
return result;
}
void *(*__morecore) (ptrdiff_t) = __default_morecore;
/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
......@@ -1556,19 +1551,11 @@ General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
#ifndef HYBRID_MALLOC
void *(*__memalign_hook) (size_t size, size_t alignment);
#endif
void *
aligned_alloc (size_t alignment, size_t size)
{
void *result;
size_t adj, lastadj;
void *(*hook) (size_t, size_t) = __memalign_hook;
if (hook)
return (*hook) (alignment, size);
/* Allocate a block with enough extra space to pad the block with up to
(ALIGNMENT - 1) bytes if necessary. */
......@@ -1731,7 +1718,6 @@ valloc (size_t size)
/* Declare system malloc and friends. */
extern void *malloc (size_t size);
extern void *realloc (void *ptr, size_t size);
extern void *calloc (size_t nmemb, size_t size);
extern void free (void *ptr);
#ifdef HAVE_ALIGNED_ALLOC
extern void *aligned_alloc (size_t alignment, size_t size);
......@@ -1750,14 +1736,6 @@ hybrid_malloc (size_t size)
return gmalloc (size);
}
void *
hybrid_calloc (size_t nmemb, size_t size)
{
if (DUMPED)
return calloc (nmemb, size);
return gcalloc (nmemb, size);
}
void
hybrid_free (void *ptr)
{
......@@ -1947,9 +1925,9 @@ freehook (void *ptr)
else
hdr = NULL;
__free_hook = old_free_hook;
gfree_hook = old_free_hook;
free (hdr);
__free_hook = freehook;
gfree_hook = freehook;
}
static void *
......@@ -1957,9 +1935,9 @@ mallochook (size_t size)
{
struct hdr *hdr;
__malloc_hook = old_malloc_hook;
gmalloc_hook = old_malloc_hook;
hdr = malloc (sizeof *hdr + size + 1);
__malloc_hook = mallochook;
gmalloc_hook = mallochook;
if (hdr == NULL)
return NULL;
......@@ -1985,13 +1963,13 @@ reallochook (void *ptr, size_t size)
memset ((char *) ptr + size, FREEFLOOD, osize - size);
}
__free_hook = old_free_hook;
__malloc_hook = old_malloc_hook;
__realloc_hook = old_realloc_hook;
gfree_hook = old_free_hook;
gmalloc_hook = old_malloc_hook;
grealloc_hook = old_realloc_hook;
hdr = realloc (hdr, sizeof *hdr + size + 1);
__free_hook = freehook;
__malloc_hook = mallochook;
__realloc_hook = reallochook;
gfree_hook = freehook;
gmalloc_hook = mallochook;
grealloc_hook = reallochook;
if (hdr == NULL)
return NULL;
......@@ -2048,12 +2026,12 @@ mcheck (void (*func) (enum mcheck_status))
/* These hooks may not be safely inserted if malloc is already in use. */
if (!__malloc_initialized && !mcheck_used)
{
old_free_hook = __free_hook;
__free_hook = freehook;
old_malloc_hook = __malloc_hook;
__malloc_hook = mallochook;
old_realloc_hook = __realloc_hook;
__realloc_hook = reallochook;
old_free_hook = gfree_hook;
gfree_hook = freehook;
old_malloc_hook = gmalloc_hook;
gmalloc_hook = mallochook;
old_realloc_hook = grealloc_hook;
grealloc_hook = reallochook;
mcheck_used = 1;
}
......
......@@ -3769,6 +3769,12 @@ extern void check_cons_list (void);
INLINE void (check_cons_list) (void) { lisp_h_check_cons_list (); }
#endif
#if !defined DOUG_LEA_MALLOC && !defined HYBRID_MALLOC && !defined SYSTEM_MALLOC
/* Defined in gmalloc.c. */
extern size_t __malloc_extra_blocks;
#endif
extern void malloc_enable_thread (void);
#ifdef REL_ALLOC
/* Defined in ralloc.c. */
extern void *r_alloc (void **, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
......
......@@ -22,31 +22,15 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
rather than all of them. This means allowing for a possible
hole between the first bloc and the end of malloc storage. */
#ifdef emacs
#include <config.h>
#include "lisp.h" /* Needed for VALBITS. */
#include "blockinput.h"
#include <unistd.h>
#ifdef DOUG_LEA_MALLOC
#define M_TOP_PAD -2
extern int mallopt (int, int);
#else /* not DOUG_LEA_MALLOC */
#if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
extern size_t __malloc_extra_blocks;
#endif /* not SYSTEM_MALLOC and not HYBRID_MALLOC */
#endif /* not DOUG_LEA_MALLOC */
#else /* not emacs */
#include <stddef.h>
#include <malloc.h>
#endif /* not emacs */
#ifdef emacs
# include "lisp.h"
# include "blockinput.h"
# include <unistd.h>
#endif
#include "getpagesize.h"
......@@ -95,7 +79,9 @@ static int extra_bytes;
/* The hook `malloc' uses for the function which gets more space
from the system. */
#if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#else
extern void *(*__morecore) (ptrdiff_t);
#endif
......
......@@ -51,9 +51,16 @@ char data_start[1] = { 1 };
# endif
#endif
/* From gmalloc.c. */
extern void (* __after_morecore_hook) (void);
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
#ifndef __MALLOC_HOOK_VOLATILE
# define __MALLOC_HOOK_VOLATILE volatile
#endif
#ifndef HAVE_MALLOC_H
extern void *(*__morecore) (ptrdiff_t);
extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
#endif
/* From ralloc.c. */
#ifdef REL_ALLOC
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment