summaryrefslogtreecommitdiff
path: root/src/gmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c80
1 files changed, 38 insertions, 42 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index 2bda95ebd3d..f063af60cf1 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -1,5 +1,5 @@
/* Declarations for `malloc' and friends.
- Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2017 Free
+ Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2022 Free
Software Foundation, Inc.
Written May 1989 by Mike Haertel.
@@ -36,9 +36,7 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>.
#include <pthread.h>
#endif
-#ifdef emacs
-# include "lisp.h"
-#endif
+#include "lisp.h"
#ifdef HAVE_MALLOC_H
# if GNUC_PREREQ (4, 2, 0)
@@ -76,7 +74,6 @@ extern void *(*__morecore) (ptrdiff_t);
#ifdef HYBRID_MALLOC
# include "sheap.h"
-# define DUMPED bss_sbrk_did_unexec
#endif
#ifdef __cplusplus
@@ -182,7 +179,7 @@ struct list
};
/* Free list headers for each fragment size. */
-extern struct list _fraghead[];
+static struct list _fraghead[BLOCKLOG];
/* List of blocks allocated with aligned_alloc and friends. */
struct alignlist
@@ -339,9 +336,6 @@ size_t _heapindex;
/* Limit of valid info table indices. */
size_t _heaplimit;
-/* Free lists for each fragment size. */
-struct list _fraghead[BLOCKLOG];
-
/* Instrumentation. */
size_t _chunks_used;
size_t _bytes_used;
@@ -351,10 +345,6 @@ size_t _bytes_free;
/* Are you experienced? */
int __malloc_initialized;
-#else
-
-static struct list _fraghead[BLOCKLOG];
-
#endif /* HYBRID_MALLOC */
/* Number of extra blocks to get each time we ask for more core.
@@ -919,7 +909,7 @@ malloc (size_t size)
among multiple threads. We just leave it for compatibility with
glibc malloc (i.e., assignments to gmalloc_hook) for now. */
hook = gmalloc_hook;
- return (hook != NULL ? *hook : _malloc_internal) (size);
+ return (hook ? hook : _malloc_internal) (size);
}
#if !(defined (_LIBC) || defined (HYBRID_MALLOC))
@@ -1430,7 +1420,7 @@ realloc (void *ptr, size_t size)
return NULL;
hook = grealloc_hook;
- return (hook != NULL ? *hook : _realloc_internal) (ptr, size);
+ return (hook ? hook : _realloc_internal) (ptr, size);
}
/* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
@@ -1502,17 +1492,18 @@ extern void *__sbrk (ptrdiff_t increment);
static void *
gdefault_morecore (ptrdiff_t increment)
{
- void *result;
#ifdef HYBRID_MALLOC
- if (!DUMPED)
+ if (!definitely_will_not_unexec_p ())
{
return bss_sbrk (increment);
}
#endif
- result = (void *) __sbrk (increment);
- if (result == (void *) -1)
- return NULL;
- return result;
+#ifdef HAVE_SBRK
+ void *result = (void *) __sbrk (increment);
+ if (result != (void *) -1)
+ return result;
+#endif
+ return NULL;
}
void *(*__morecore) (ptrdiff_t) = gdefault_morecore;
@@ -1699,16 +1690,6 @@ valloc (size_t size)
#undef free
#ifdef HYBRID_MALLOC
-/* 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);
-#elif defined HAVE_POSIX_MEMALIGN
-extern int posix_memalign (void **memptr, size_t alignment, size_t size);
-#endif
/* Assuming PTR was allocated via the hybrid malloc, return true if
PTR was allocated via gmalloc, not the system malloc. Also, return
@@ -1719,6 +1700,8 @@ extern int posix_memalign (void **memptr, size_t alignment, size_t size);
static bool
allocated_via_gmalloc (void *ptr)
{
+ if (!__malloc_initialized)
+ return false;
size_t block = BLOCK (ptr);
size_t blockmax = _heaplimit - 1;
return block <= blockmax && _heapinfo[block].busy.type != 0;
@@ -1730,7 +1713,7 @@ allocated_via_gmalloc (void *ptr)
void *
hybrid_malloc (size_t size)
{
- if (DUMPED)
+ if (definitely_will_not_unexec_p ())
return malloc (size);
return gmalloc (size);
}
@@ -1738,13 +1721,13 @@ hybrid_malloc (size_t size)
void *
hybrid_calloc (size_t nmemb, size_t size)
{
- if (DUMPED)
+ if (definitely_will_not_unexec_p ())
return calloc (nmemb, size);
return gcalloc (nmemb, size);
}
-void
-hybrid_free (void *ptr)
+static void
+hybrid_free_1 (void *ptr)
{
if (allocated_via_gmalloc (ptr))
gfree (ptr);
@@ -1752,11 +1735,29 @@ hybrid_free (void *ptr)
free (ptr);
}
+void
+hybrid_free (void *ptr)
+{
+ /* Stolen from Gnulib, to make sure we preserve errno. */
+#if defined __GNUC__ && !defined __clang__
+ int err[2];
+ err[0] = errno;
+ err[1] = errno;
+ errno = 0;
+ hybrid_free_1 (ptr);
+ errno = err[errno == 0];
+#else
+ int err = errno;
+ hybrid_free_1 (ptr);
+ errno = err;
+#endif
+}
+
#if defined HAVE_ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN
void *
hybrid_aligned_alloc (size_t alignment, size_t size)
{
- if (!DUMPED)
+ if (!definitely_will_not_unexec_p ())
return galigned_alloc (alignment, size);
/* The following is copied from alloc.c */
#ifdef HAVE_ALIGNED_ALLOC
@@ -1779,7 +1780,7 @@ hybrid_realloc (void *ptr, size_t size)
return hybrid_malloc (size);
if (!allocated_via_gmalloc (ptr))
return realloc (ptr, size);
- if (!DUMPED)
+ if (!definitely_will_not_unexec_p ())
return grealloc (ptr, size);
/* The dumped emacs is trying to realloc storage allocated before
@@ -2012,12 +2013,7 @@ mabort (enum mcheck_status status)
__libc_fatal (msg);
#else
fprintf (stderr, "mcheck: %s\n", msg);
- fflush (stderr);
-# ifdef emacs
emacs_abort ();
-# else
- abort ();
-# endif
#endif
}