Remove distracting code from dlmalloc

This commit is contained in:
Justine Tunney 2024-12-16 22:43:00 -08:00
parent af7bd80430
commit c8c81af0c7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 28 additions and 467 deletions

View file

@ -1,5 +1,6 @@
#include "libc/sysv/consts/auxv.h"
#include "libc/runtime/runtime.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/runtime/runtime.h"
/* ---------------------------- setting mparams -------------------------- */
@ -9,7 +10,6 @@
void dlmalloc_pre_fork(void) {
mstate h;
ACQUIRE_MALLOC_GLOBAL_LOCK();
for (unsigned i = ARRAYLEN(g_heaps); i--;)
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
ACQUIRE_LOCK(&h->mutex);
@ -20,7 +20,6 @@ void dlmalloc_post_fork_parent(void) {
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
RELEASE_LOCK(&h->mutex);
RELEASE_MALLOC_GLOBAL_LOCK();
}
void dlmalloc_post_fork_child(void) {
@ -28,7 +27,6 @@ void dlmalloc_post_fork_child(void) {
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
if ((h = atomic_load_explicit(&g_heaps[i], memory_order_acquire)))
(void)REFRESH_LOCK(&h->mutex);
(void)REFRESH_MALLOC_GLOBAL_LOCK();
}
#else
@ -40,32 +38,14 @@ void dlmalloc_post_fork_child(void) { (void)REFRESH_LOCK(&(gm)->mutex); }
/* Initialize mparams */
__attribute__((__constructor__(49))) int init_mparams(void) {
#ifdef NEED_GLOBAL_LOCK_INIT
if (malloc_global_mutex_status <= 0)
init_malloc_global_mutex();
#endif
// ACQUIRE_MALLOC_GLOBAL_LOCK();
if (mparams.magic == 0) {
size_t magic;
size_t psize;
size_t gsize;
#if defined(__COSMOPOLITAN__)
psize = getpagesize();
psize = __pagesize;
gsize = DEFAULT_GRANULARITY ? DEFAULT_GRANULARITY : psize;
#elif !defined(WIN32)
psize = malloc_getpagesize;
gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize);
#else /* WIN32 */
{
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
psize = system_info.dwPageSize;
gsize = ((DEFAULT_GRANULARITY != 0)?
DEFAULT_GRANULARITY : system_info.dwAllocationGranularity);
}
#endif /* WIN32 */
/* Sanity-check configuration:
size_t must be unsigned and as wide as pointer type.
@ -86,11 +66,7 @@ __attribute__((__constructor__(49))) int init_mparams(void) {
mparams.page_size = psize;
mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
#if MORECORE_CONTIGUOUS
mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT;
#else /* MORECORE_CONTIGUOUS */
mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT;
#endif /* MORECORE_CONTIGUOUS */
#if !ONLY_MSPACES
/* Set up lock for main malloc area */
@ -110,7 +86,7 @@ __attribute__((__constructor__(49))) int init_mparams(void) {
}
else
#endif /* USE_DEV_RANDOM */
magic = (size_t)(_rand64() ^ (size_t)0x55555555U);
magic = (size_t)(rdtsc() ^ (size_t)0x55555555U);
magic |= (size_t)8U; /* ensure nonzero */
magic &= ~(size_t)7U; /* improve chances of fault for bad values */
/* Until memory modes commonly available, use volatile-write */
@ -118,8 +94,6 @@ __attribute__((__constructor__(49))) int init_mparams(void) {
}
}
// RELEASE_MALLOC_GLOBAL_LOCK();
#if ONLY_MSPACES
threaded_dlmalloc();
#endif