mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-14 13:46:11 +00:00
Actually Portable Executable now supports Android. Cosmo's old mmap code required a 47 bit address space. The new implementation is very agnostic and supports both smaller address spaces (e.g. embedded) and even modern 56-bit PML5T paging for x86 which finally came true on Zen4 Threadripper Cosmopolitan no longer requires UNIX systems to observe the Windows 64kb granularity; i.e. sysconf(_SC_PAGE_SIZE) will now report the host native page size. This fixes a longstanding POSIX conformance issue, concerning file mappings that overlap the end of file. Other aspects of conformance have been improved too, such as the subtleties of address assignment and and the various subtleties surrounding MAP_FIXED and MAP_FIXED_NOREPLACE On Windows, mappings larger than 100 megabytes won't be broken down into thousands of independent 64kb mappings. Support for MAP_STACK is removed by this change; please use NewCosmoStack() instead. Stack overflow avoidance is now being implemented using the POSIX thread APIs. Please use GetStackBottom() and GetStackAddr(), instead of the old error-prone GetStackAddr() and HaveStackMemory() APIs which are removed.
149 lines
4.4 KiB
C++
149 lines
4.4 KiB
C++
#include "libc/sysv/consts/auxv.h"
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
/* ---------------------------- setting mparams -------------------------- */
|
|
|
|
#if LOCK_AT_FORK
|
|
#if ONLY_MSPACES
|
|
|
|
static void dlmalloc_pre_fork(void) {
|
|
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
|
ACQUIRE_LOCK(&g_heaps[i].m.mutex);
|
|
}
|
|
|
|
static void dlmalloc_post_fork_parent(void) {
|
|
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
|
RELEASE_LOCK(&g_heaps[i].m.mutex);
|
|
}
|
|
|
|
static void dlmalloc_post_fork_child(void) {
|
|
for (unsigned i = 0; i < ARRAYLEN(g_heaps); ++i)
|
|
(void)INITIAL_LOCK(&g_heaps[i].m.mutex);
|
|
}
|
|
|
|
#else
|
|
static void dlmalloc_pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); }
|
|
static void dlmalloc_post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); }
|
|
static void dlmalloc_post_fork_child(void) { (void)INITIAL_LOCK(&(gm)->mutex); }
|
|
#endif /* ONLY_MSPACES */
|
|
#endif /* LOCK_AT_FORK */
|
|
|
|
/* 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 = getauxval(AT_PAGESZ);
|
|
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.
|
|
ints must be at least 4 bytes.
|
|
alignment must be at least 8.
|
|
Alignment, min chunk size, and page size must all be powers of 2.
|
|
*/
|
|
if ((sizeof(size_t) != sizeof(char*)) ||
|
|
(MAX_SIZE_T < MIN_CHUNK_SIZE) ||
|
|
(sizeof(int) < 4) ||
|
|
(MALLOC_ALIGNMENT < (size_t)8U) ||
|
|
((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-SIZE_T_ONE)) != 0) ||
|
|
((MCHUNK_SIZE & (MCHUNK_SIZE-SIZE_T_ONE)) != 0) ||
|
|
((gsize & (gsize-SIZE_T_ONE)) != 0) ||
|
|
((psize & (psize-SIZE_T_ONE)) != 0))
|
|
ABORT;
|
|
mparams.granularity = gsize;
|
|
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 */
|
|
gm->mflags = mparams.default_mflags;
|
|
(void)INITIAL_LOCK(&gm->mutex);
|
|
#endif
|
|
|
|
#if LOCK_AT_FORK
|
|
pthread_atfork(&dlmalloc_pre_fork,
|
|
&dlmalloc_post_fork_parent,
|
|
&dlmalloc_post_fork_child);
|
|
#endif
|
|
|
|
{
|
|
#if USE_DEV_RANDOM
|
|
int fd;
|
|
unsigned char buf[sizeof(size_t)];
|
|
/* Try to use /dev/urandom, else fall back on using time */
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 &&
|
|
read(fd, buf, sizeof(buf)) == sizeof(buf)) {
|
|
magic = *((size_t *) buf);
|
|
close(fd);
|
|
}
|
|
else
|
|
#endif /* USE_DEV_RANDOM */
|
|
magic = (size_t)(_rand64() ^ (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
|
|
|
|
__runlevel = RUNLEVEL_MALLOC;
|
|
return 1;
|
|
}
|
|
|
|
/* support for mallopt */
|
|
static int change_mparam(int param_number, int value) {
|
|
size_t val;
|
|
ensure_initialization();
|
|
val = (value == -1)? MAX_SIZE_T : (size_t)value;
|
|
switch(param_number) {
|
|
case M_TRIM_THRESHOLD:
|
|
mparams.trim_threshold = val;
|
|
return 1;
|
|
case M_GRANULARITY:
|
|
if (val >= mparams.page_size && ((val & (val-1)) == 0)) {
|
|
mparams.granularity = val;
|
|
return 1;
|
|
}
|
|
else
|
|
return 0;
|
|
case M_MMAP_THRESHOLD:
|
|
mparams.mmap_threshold = val;
|
|
return 1;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|