]> git.proxmox.com Git - wasi-libc.git/commitdiff
Use __heap_base by dlmalloc (#114)
authorvms <michail.vms@gmail.com>
Tue, 5 Nov 2019 20:52:51 +0000 (23:52 +0300)
committerDan Gohman <sunfish@mozilla.com>
Tue, 5 Nov 2019 20:52:51 +0000 (12:52 -0800)
* add explicit allocator initialization

* move init to a better place

* fix warnings

* add __wasilibc_try_init_allocator

* move initialization to dlmalloc()

* fix typos

* fix identations

* follow style guide

* review changes

dlmalloc/src/malloc.c
expected/wasm32-wasi/undefined-symbols.txt

index 29ce55698a1e641ed05a8741e71e26ad704b85b7..56988432b55b7394b1c99b58c0b4df0e0a9aa9dd 100644 (file)
@@ -4560,6 +4560,11 @@ static void* tmalloc_small(mstate m, size_t nb) {
 
 #if !ONLY_MSPACES
 
+#if __wasilibc_unmodified_upstream // Forward declaration of try_init_allocator.
+#else
+static void try_init_allocator(void);
+#endif
+
 void* dlmalloc(size_t bytes) {
   /*
      Basic algorithm:
@@ -4588,6 +4593,13 @@ void* dlmalloc(size_t bytes) {
   ensure_initialization(); /* initialize in sys_alloc if not using locks */
 #endif
 
+#if __wasilibc_unmodified_upstream // Try to initialize the allocator.
+#else
+  if (!is_initialized(gm)) {
+    try_init_allocator();
+  }
+#endif
+
   if (!PREACTION(gm)) {
     void* mem;
     size_t nb;
@@ -5197,6 +5209,42 @@ static void internal_inspect_all(mstate m,
 }
 #endif /* MALLOC_INSPECT_ALL */
 
+#ifdef __wasilibc_unmodified_upstream // Define a function that initializes the initial state of dlmalloc
+#else
+/* ------------------ Exported try_init_allocator -------------------- */
+
+/* Symbol marking the end of data, bss and explicit stack, provided by wasm-ld. */
+extern unsigned char __heap_base;
+
+/* Initialize the initial state of dlmalloc to be able to use free memory between __heap_base and initial. */
+static void try_init_allocator(void) {
+  /* Check that it is a first-time initialization. */
+  assert(!is_initialized(gm));
+
+  char *base = (char *)&__heap_base;
+  /* Calls sbrk(0) that returns the initial memory position. */
+  char *init = (char *)CALL_MORECORE(0);
+  int initial_heap_size = init - base;
+
+  /* Check that initial heap is long enough to serve a minimal allocation request. */
+  if (initial_heap_size <= MIN_CHUNK_SIZE + TOP_FOOT_SIZE + MALLOC_ALIGNMENT) {
+    return;
+  }
+
+  /* Initialize mstate. */
+  ensure_initialization();
+
+  /* Initialize the dlmalloc internal state. */
+  gm->least_addr = base;
+  gm->seg.base = base;
+  gm->seg.size = initial_heap_size;
+  gm->magic = mparams.magic;
+  gm->release_checks = MAX_RELEASE_CHECK_RATE;
+  init_bins(gm);
+  init_top(gm, (mchunkptr)base, initial_heap_size - TOP_FOOT_SIZE);
+}
+#endif
+
 /* ------------------ Exported realloc, memalign, etc -------------------- */
 
 #if !ONLY_MSPACES
index 8715e541d86ffb5467eafa66e2f73d736c56961d..47e31069034132e16b0c930206c63ce677ed4d9a 100644 (file)
@@ -10,6 +10,7 @@ __floatsitf
 __floatunsitf
 __getf2
 __gttf2
+__heap_base
 __letf2
 __lttf2
 __netf2