aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/musl/src/malloc
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-07-01 15:52:54 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-07-01 15:52:54 -0700
commitc89dd15e1be4959800dc7092d7dd4375253db7bc (patch)
treeca184ae53592efa21e67128a5f891d642d7f1118 /lib/libc/musl/src/malloc
parent5466e87fce581f2ef90ac23bb80b1dbc05836fc6 (diff)
parent2360f8c490f3ec684ed64ff28e8c1fade249070b (diff)
downloadzig-c89dd15e1be4959800dc7092d7dd4375253db7bc.tar.gz
zig-c89dd15e1be4959800dc7092d7dd4375253db7bc.zip
Merge remote-tracking branch 'origin/master' into llvm14
Diffstat (limited to 'lib/libc/musl/src/malloc')
-rw-r--r--lib/libc/musl/src/malloc/free.c2
-rw-r--r--lib/libc/musl/src/malloc/mallocng/aligned_alloc.c3
-rw-r--r--lib/libc/musl/src/malloc/mallocng/free.c12
-rw-r--r--lib/libc/musl/src/malloc/oldmalloc/malloc.c6
4 files changed, 19 insertions, 4 deletions
diff --git a/lib/libc/musl/src/malloc/free.c b/lib/libc/musl/src/malloc/free.c
index f17a952cb4..3944f7b28f 100644
--- a/lib/libc/musl/src/malloc/free.c
+++ b/lib/libc/musl/src/malloc/free.c
@@ -2,5 +2,5 @@
void free(void *p)
{
- return __libc_free(p);
+ __libc_free(p);
}
diff --git a/lib/libc/musl/src/malloc/mallocng/aligned_alloc.c b/lib/libc/musl/src/malloc/mallocng/aligned_alloc.c
index 3411689600..e0862a83ae 100644
--- a/lib/libc/musl/src/malloc/mallocng/aligned_alloc.c
+++ b/lib/libc/musl/src/malloc/mallocng/aligned_alloc.c
@@ -22,6 +22,9 @@ void *aligned_alloc(size_t align, size_t len)
if (align <= UNIT) align = UNIT;
unsigned char *p = malloc(len + align - UNIT);
+ if (!p)
+ return 0;
+
struct meta *g = get_meta(p);
int idx = get_slot_index(p);
size_t stride = get_stride(g);
diff --git a/lib/libc/musl/src/malloc/mallocng/free.c b/lib/libc/musl/src/malloc/mallocng/free.c
index 40745f97da..418a085c18 100644
--- a/lib/libc/musl/src/malloc/mallocng/free.c
+++ b/lib/libc/musl/src/malloc/mallocng/free.c
@@ -119,7 +119,11 @@ void free(void *p)
if (((uintptr_t)(start-1) ^ (uintptr_t)end) >= 2*PGSZ && g->last_idx) {
unsigned char *base = start + (-(uintptr_t)start & (PGSZ-1));
size_t len = (end-base) & -PGSZ;
- if (len) madvise(base, len, MADV_FREE);
+ if (len) {
+ int e = errno;
+ madvise(base, len, MADV_FREE);
+ errno = e;
+ }
}
// atomic free without locking if this is neither first or last slot
@@ -139,5 +143,9 @@ void free(void *p)
wrlock();
struct mapinfo mi = nontrivial_free(g, idx);
unlock();
- if (mi.len) munmap(mi.base, mi.len);
+ if (mi.len) {
+ int e = errno;
+ munmap(mi.base, mi.len);
+ errno = e;
+ }
}
diff --git a/lib/libc/musl/src/malloc/oldmalloc/malloc.c b/lib/libc/musl/src/malloc/oldmalloc/malloc.c
index 53f5f959ec..25d00d44de 100644
--- a/lib/libc/musl/src/malloc/oldmalloc/malloc.c
+++ b/lib/libc/musl/src/malloc/oldmalloc/malloc.c
@@ -11,7 +11,7 @@
#include "malloc_impl.h"
#include "fork_impl.h"
-#define malloc __libc_malloc
+#define malloc __libc_malloc_impl
#define realloc __libc_realloc
#define free __libc_free
@@ -481,12 +481,14 @@ void __bin_chunk(struct chunk *self)
if (size > RECLAIM && (size^(size-osize)) > size-osize) {
uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
+ int e = errno;
#if 1
__madvise((void *)a, b-a, MADV_DONTNEED);
#else
__mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
#endif
+ errno = e;
}
unlock_bin(i);
@@ -499,7 +501,9 @@ static void unmap_chunk(struct chunk *self)
size_t len = CHUNK_SIZE(self) + extra;
/* Crash on double free */
if (extra & 1) a_crash();
+ int e = errno;
__munmap(base, len);
+ errno = e;
}
void free(void *p)