Skip to content

Commit

Permalink
Remove distracting code from dlmalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Dec 17, 2024
1 parent af7bd80 commit c8c81af
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 469 deletions.
130 changes: 4 additions & 126 deletions third_party/dlmalloc/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@
#define USE_SPIN_LOCKS 1
#endif

#define HAVE_MMAP 1
#define HAVE_MREMAP 1
#define HAVE_MORECORE 0
#define USE_LOCKS 2
#define MORECORE_CONTIGUOUS 0
#define MALLOC_INSPECT_ALL 1
#define ABORT_ON_ASSERT_FAILURE 0
#define LOCK_AT_FORK 1
Expand Down Expand Up @@ -88,7 +85,7 @@

/* -------------------------- System allocation -------------------------- */

/* Get memory from system using MORECORE or MMAP */
/* Get memory from system */
static void* sys_alloc(mstate m, size_t nb) {
char* tbase = CMFAIL;
size_t tsize = 0;
Expand All @@ -113,90 +110,7 @@ static void* sys_alloc(mstate m, size_t nb) {
return 0;
}

/*
Try getting memory in any of three ways (in most-preferred to
least-preferred order):
1. A call to MORECORE that can normally contiguously extend memory.
(disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or
or main space is mmapped or a previous contiguous call failed)
2. A call to MMAP new space (disabled if not HAVE_MMAP).
Note that under the default settings, if MORECORE is unable to
fulfill a request, and HAVE_MMAP is true, then mmap is
used as a noncontiguous system allocator. This is a useful backup
strategy for systems with holes in address spaces -- in this case
sbrk cannot contiguously expand the heap, but mmap may be able to
find space.
3. A call to MORECORE that cannot usually contiguously extend memory.
(disabled if not HAVE_MORECORE)
In all cases, we need to request enough bytes from system to ensure
we can malloc nb bytes upon success, so pad with enough space for
top_foot, plus alignment-pad to make sure we don't lose bytes if
not on boundary, and round this up to a granularity unit.
*/

if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) {
char* br = CMFAIL;
size_t ssize = asize; /* sbrk call size */
msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top);
ACQUIRE_MALLOC_GLOBAL_LOCK();

if (ss == 0) { /* First time through or recovery */
char* base = (char*)CALL_MORECORE(0);
if (base != CMFAIL) {
size_t fp;
/* Adjust to end on a page boundary */
if (!is_page_aligned(base))
ssize += (page_align((size_t)base) - (size_t)base);
fp = m->footprint + ssize; /* recheck limits */
if (ssize > nb && ssize < HALF_MAX_SIZE_T &&
(m->footprint_limit == 0 ||
(fp > m->footprint && fp <= m->footprint_limit)) &&
(br = (char*)(CALL_MORECORE(ssize))) == base) {
tbase = base;
tsize = ssize;
}
}
}
else {
/* Subtract out existing available top space from MORECORE request. */
ssize = granularity_align(nb - m->topsize + SYS_ALLOC_PADDING);
/* Use mem here only if it did continuously extend old space */
if (ssize < HALF_MAX_SIZE_T &&
(br = (char*)(CALL_MORECORE(ssize))) == ss->base+ss->size) {
tbase = br;
tsize = ssize;
}
}

if (tbase == CMFAIL) { /* Cope with partial failure */
if (br != CMFAIL) { /* Try to use/extend the space we did get */
if (ssize < HALF_MAX_SIZE_T &&
ssize < nb + SYS_ALLOC_PADDING) {
size_t esize = granularity_align(nb + SYS_ALLOC_PADDING - ssize);
if (esize < HALF_MAX_SIZE_T) {
char* end = (char*)CALL_MORECORE(esize);
if (end != CMFAIL)
ssize += esize;
else { /* Can't use; try to release */
(void) CALL_MORECORE(-ssize);
br = CMFAIL;
}
}
}
}
if (br != CMFAIL) { /* Use the space we did get */
tbase = br;
tsize = ssize;
}
else
disable_contiguous(m); /* Don't try contiguous path in the future */
}

RELEASE_MALLOC_GLOBAL_LOCK();
}

if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */
if (tbase == CMFAIL) { /* Try MMAP */
char* mp = dlmalloc_requires_more_vespene_gas(asize);
if (mp != CMFAIL) {
tbase = mp;
Expand All @@ -205,24 +119,6 @@ static void* sys_alloc(mstate m, size_t nb) {
}
}

if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */
if (asize < HALF_MAX_SIZE_T) {
char* br = CMFAIL;
char* end = CMFAIL;
ACQUIRE_MALLOC_GLOBAL_LOCK();
br = (char*)(CALL_MORECORE(asize));
end = (char*)(CALL_MORECORE(0));
RELEASE_MALLOC_GLOBAL_LOCK();
if (br != CMFAIL && end != CMFAIL && br < end) {
size_t ssize = end - br;
if (ssize > nb + TOP_FOOT_SIZE) {
tbase = br;
tsize = ssize;
}
}
}
}

if (tbase != CMFAIL) {

if ((m->footprint += tsize) > m->max_footprint)
Expand Down Expand Up @@ -362,8 +258,7 @@ static int sys_trim(mstate m, size_t pad) {

if (!is_extern_segment(sp)) {
if (is_mmapped_segment(sp)) {
if (HAVE_MMAP &&
sp->size >= extra &&
if (sp->size >= extra &&
!has_segment_link(m, sp)) { /* can't shrink if pinned */
size_t newsize = sp->size - extra;
(void)newsize; /* placate people compiling -Wunused-variable */
Expand All @@ -374,22 +269,6 @@ static int sys_trim(mstate m, size_t pad) {
}
}
}
else if (HAVE_MORECORE) {
if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */
extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit;
ACQUIRE_MALLOC_GLOBAL_LOCK();
{
/* Make sure end of memory is where we last set it. */
char* old_br = (char*)(CALL_MORECORE(0));
if (old_br == sp->base + sp->size) {
char* rel_br = (char*)(CALL_MORECORE(-extra));
char* new_br = (char*)(CALL_MORECORE(0));
if (rel_br != CMFAIL && new_br < old_br)
released = old_br - new_br;
}
}
RELEASE_MALLOC_GLOBAL_LOCK();
}
}

if (released != 0) {
Expand All @@ -401,8 +280,7 @@ static int sys_trim(mstate m, size_t pad) {
}

/* Unmap any unused mmapped segments */
if (HAVE_MMAP)
released += release_unused_segments(m);
released += release_unused_segments(m);

/* On failure, disable autotrim to avoid repeated failed future calls */
if (released == 0 && m->topsize > m->trim_check)
Expand Down
32 changes: 3 additions & 29 deletions third_party/dlmalloc/init.inc
Original file line number Diff line number Diff line change
@@ -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 -------------------------- */
Expand All @@ -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);
Expand All @@ -20,15 +20,13 @@ 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) {
mstate h;
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
Expand All @@ -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.
Expand All @@ -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 */
Expand All @@ -110,16 +86,14 @@ __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 */
(*(volatile size_t *)(&(mparams.magic))) = magic;
}
}

// RELEASE_MALLOC_GLOBAL_LOCK();

#if ONLY_MSPACES
threaded_dlmalloc();
#endif
Expand Down
6 changes: 0 additions & 6 deletions third_party/dlmalloc/locks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ static int malloc_unlk(MLOCK_T *lk) {
#define INITIAL_LOCK(lk) malloc_inlk(lk)
#define REFRESH_LOCK(lk) malloc_wipe(lk)
#define DESTROY_LOCK(lk) malloc_kilk(lk)
#define INITIAL_MALLOC_GLOBAL_LOCK() INITIAL_LOCK(&malloc_global_mutex);
#define REFRESH_MALLOC_GLOBAL_LOCK() REFRESH_LOCK(&malloc_global_mutex);
#define ACQUIRE_MALLOC_GLOBAL_LOCK() ACQUIRE_LOCK(&malloc_global_mutex);
#define RELEASE_MALLOC_GLOBAL_LOCK() RELEASE_LOCK(&malloc_global_mutex);

static MLOCK_T malloc_global_mutex;

#define USE_LOCK_BIT (2U)

Expand Down
Loading

0 comments on commit c8c81af

Please sign in to comment.