static struct chunk *expand_heap(size_t n)
{
+ static int init;
struct chunk *w;
uintptr_t new;
lock(mal.brk_lock);
+ if (!init) {
+ mal.brk = __brk(0);
+#ifdef SHARED
+ mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
+#endif
+ mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
+ mal.heap = (void *)mal.brk;
+ init = 1;
+ }
+
if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
n = new - mal.brk;
return area;
}
+ w = MEM_TO_CHUNK(mal.heap);
+ w->psize = 0 | C_INUSE;
+
w = MEM_TO_CHUNK(new);
w->psize = n | C_INUSE;
w->csize = 0 | C_INUSE;
return 0;
}
-static int init_malloc(size_t n)
-{
- static volatile int init, waiters;
- int state;
- struct chunk *c;
-
- if (init == 2) return 0;
-
- while ((state=a_swap(&init, 1)) == 1)
- __wait(&init, &waiters, 1, 1);
- if (state) {
- a_store(&init, 2);
- return 0;
- }
-
- mal.brk = __brk(0);
-#ifdef SHARED
- mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
-#endif
- mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
-
- c = expand_heap(n);
-
- if (!c) {
- a_store(&init, 0);
- if (waiters) __wake(&init, 1, 1);
- return -1;
- }
-
- mal.heap = (void *)c;
- c->psize = 0 | C_INUSE;
- free(CHUNK_TO_MEM(c));
-
- a_store(&init, 2);
- if (waiters) __wake(&init, -1, 1);
- return 1;
-}
-
static int adjust_size(size_t *n)
{
/* Result of pointer difference must fit in ptrdiff_t. */
for (;;) {
uint64_t mask = mal.binmap & -(1ULL<<i);
if (!mask) {
- if (init_malloc(n) > 0) continue;
c = expand_heap(n);
if (!c) return 0;
if (alloc_rev(c)) {