diff options
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r-- | src/gmalloc.c | 167 |
1 files changed, 9 insertions, 158 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c index fe6a0412a9b..18b993f78d0 100644 --- a/src/gmalloc.c +++ b/src/gmalloc.c @@ -21,7 +21,7 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>. #include <config.h> -#if defined HAVE_PTHREAD && !defined HYBRID_MALLOC +#if defined HAVE_PTHREAD #define USE_PTHREAD #endif @@ -57,13 +57,6 @@ extern void *(*__morecore) (ptrdiff_t); extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void); #endif /* !defined HAVE_MALLOC_H || glibc >= 2.24 */ -/* 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, - however, will use the system malloc, realloc.... In other source - files, malloc, realloc... are renamed hybrid_malloc, - hybrid_realloc... via macros in lisp.h. hybrid_malloc and - friends are wrapper functions defined later in this file. */ #undef malloc #undef realloc #undef calloc @@ -76,19 +69,11 @@ extern void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void); #define free gfree #define malloc_info gmalloc_info -#ifdef HYBRID_MALLOC -# include "sheap.h" -#endif - #ifdef __cplusplus extern "C" { #endif -#ifdef HYBRID_MALLOC -#define extern static -#endif - /* Allocate SIZE bytes of memory. */ extern void *malloc (size_t size) ATTRIBUTE_MALLOC_SIZE ((1)); /* Re-allocate the previously allocated block @@ -327,8 +312,6 @@ void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void); void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void); void *(*__morecore) (ptrdiff_t); -#ifndef HYBRID_MALLOC - /* Pointer to the base of the first block. */ char *_heapbase; @@ -350,11 +333,9 @@ size_t _bytes_free; /* Are you experienced? */ int __malloc_initialized; -#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 +#if defined DOUG_LEA_MALLOC || defined SYSTEM_MALLOC static #endif size_t __malloc_extra_blocks; @@ -917,7 +898,7 @@ malloc (size_t size) return (hook ? hook : _malloc_internal) (size); } -#if !(defined (_LIBC) || defined (HYBRID_MALLOC)) +#if !(defined (_LIBC)) /* On some ANSI C systems, some libc functions call _malloc, _free and _realloc. Make them use the GNU functions. */ @@ -969,11 +950,8 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>. /* Debugging hook for free. */ static void (*__MALLOC_HOOK_VOLATILE gfree_hook) (void *); -#ifndef HYBRID_MALLOC - /* List of blocks allocated by aligned_alloc. */ struct alignlist *_aligned_blocks = NULL; -#endif /* Return memory to the heap. Like `_free_internal' but don't lock mutex. */ @@ -1244,7 +1222,6 @@ free (void *ptr) _free_internal (ptr); } -#ifndef HYBRID_MALLOC /* Define the `cfree' alias for `free'. */ #ifdef weak_alias weak_alias (free, cfree) @@ -1255,7 +1232,6 @@ cfree (void *ptr) free (ptr); } #endif -#endif /* Change the size of a block allocated by `malloc'. Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2025 Free Software Foundation, Inc. @@ -1495,12 +1471,6 @@ extern void *__sbrk (ptrdiff_t increment); static void * gdefault_morecore (ptrdiff_t increment) { -#ifdef HYBRID_MALLOC - if (!definitely_will_not_unexec_p ()) - { - return bss_sbrk (increment); - } -#endif #ifdef HAVE_SBRK void *result = (void *) __sbrk (increment); if (result != (void *) -1) @@ -1610,7 +1580,6 @@ aligned_alloc (size_t alignment, size_t size) } /* Note that memalign and posix_memalign are not used in Emacs. */ -#ifndef HYBRID_MALLOC /* An obsolete alias for aligned_alloc, for any old libraries that use this alias. */ @@ -1620,8 +1589,6 @@ memalign (size_t alignment, size_t size) return aligned_alloc (alignment, size); } -/* If HYBRID_MALLOC is defined, we may want to use the system - posix_memalign below. */ int posix_memalign (void **memptr, size_t alignment, size_t size) { @@ -1640,7 +1607,6 @@ posix_memalign (void **memptr, size_t alignment, size_t size) return 0; } -#endif /* Allocate memory on a page boundary. Copyright (C) 1990-1993, 1995-1996, 1999, 2002-2007, 2013-2025 Free @@ -1662,18 +1628,16 @@ License along with this library. If not, see <https://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. */ -#ifndef HYBRID_MALLOC - -# ifndef HAVE_MALLOC_H +#ifndef HAVE_MALLOC_H /* Allocate SIZE bytes on a page boundary. */ extern void *valloc (size_t); -# endif +#endif -# if defined _SC_PAGESIZE || !defined HAVE_GETPAGESIZE -# include "getpagesize.h" -# elif !defined getpagesize +#if defined _SC_PAGESIZE || !defined HAVE_GETPAGESIZE +# include "getpagesize.h" +#elif !defined getpagesize extern int getpagesize (void); -# endif +#endif static size_t pagesize; @@ -1685,7 +1649,6 @@ valloc (size_t size) return aligned_alloc (pagesize, size); } -#endif /* HYBRID_MALLOC */ #undef malloc #undef realloc @@ -1693,116 +1656,6 @@ valloc (size_t size) #undef aligned_alloc #undef free -#ifdef HYBRID_MALLOC - -/* Assuming PTR was allocated via the hybrid malloc, return true if - PTR was allocated via gmalloc, not the system malloc. Also, return - true if _heaplimit is zero; this can happen temporarily when - gmalloc calls itself for internal use, and in that case PTR is - already known to be allocated via gmalloc. */ - -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; -} - -/* See the comments near the beginning of this file for explanations - of the following functions. */ - -void * -hybrid_malloc (size_t size) -{ - if (definitely_will_not_unexec_p ()) - return malloc (size); - return gmalloc (size); -} - -void * -hybrid_calloc (size_t nmemb, size_t size) -{ - if (definitely_will_not_unexec_p ()) - return calloc (nmemb, size); - return gcalloc (nmemb, size); -} - -static void -hybrid_free_1 (void *ptr) -{ - if (allocated_via_gmalloc (ptr)) - gfree (ptr); - else - 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 (!definitely_will_not_unexec_p ()) - return galigned_alloc (alignment, size); - /* The following is copied from alloc.c */ -#ifdef HAVE_ALIGNED_ALLOC - return aligned_alloc (alignment, size); -#else /* HAVE_POSIX_MEMALIGN */ - void *p; - return posix_memalign (&p, alignment, size) == 0 ? p : 0; -#endif -} -#endif - -void * -hybrid_realloc (void *ptr, size_t size) -{ - void *result; - int type; - size_t block, oldsize; - - if (!ptr) - return hybrid_malloc (size); - if (!allocated_via_gmalloc (ptr)) - return realloc (ptr, size); - if (!definitely_will_not_unexec_p ()) - return grealloc (ptr, size); - - /* The dumped emacs is trying to realloc storage allocated before - dumping via gmalloc. Allocate new space and copy the data. Do - not bother with gfree (ptr), as that would just waste time. */ - block = BLOCK (ptr); - type = _heapinfo[block].busy.type; - oldsize = - type < 0 ? _heapinfo[block].busy.info.size * BLOCKSIZE - : (size_t) 1 << type; - result = malloc (size); - if (result) - return memcpy (result, ptr, min (oldsize, size)); - return result; -} - -#else /* ! HYBRID_MALLOC */ - void * malloc (size_t size) { @@ -1833,8 +1686,6 @@ realloc (void *ptr, size_t size) return grealloc (ptr, size); } -#endif /* HYBRID_MALLOC */ - #ifdef GC_MCHECK /* Standard debugging hooks for `malloc'. |