summaryrefslogtreecommitdiff
path: root/src/malloc/__simple_malloc.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/malloc/__simple_malloc.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
initial check-in, version 0.5.0v0.5.0
Diffstat (limited to 'src/malloc/__simple_malloc.c')
-rw-r--r--src/malloc/__simple_malloc.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/malloc/__simple_malloc.c b/src/malloc/__simple_malloc.c
new file mode 100644
index 00000000..49b74c8e
--- /dev/null
+++ b/src/malloc/__simple_malloc.c
@@ -0,0 +1,44 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <limits.h>
+#include <errno.h>
+#include "libc.h"
+
+uintptr_t __brk(uintptr_t);
+
+#define ALIGN 16
+
+void *__simple_malloc(size_t n)
+{
+ static uintptr_t cur, brk;
+ uintptr_t base, new;
+ static int lock;
+ size_t align=1;
+
+ if (n < SIZE_MAX - ALIGN)
+ while (align<n && align<ALIGN)
+ align += align;
+ n = n + align - 1 & -align;
+
+ LOCK(&lock);
+ if (!cur) cur = brk = __brk(0)+16;
+ if (n > SIZE_MAX - brk) goto fail;
+
+ base = cur + align-1 & -align;
+ if (base+n > brk) {
+ new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
+ if (__brk(new) != new) goto fail;
+ brk = new;
+ }
+ cur = base+n;
+ UNLOCK(&lock);
+
+ return (void *)base;
+
+fail:
+ UNLOCK(&lock);
+ errno = ENOMEM;
+ return 0;
+}
+
+weak_alias(__simple_malloc, malloc);