mips: add vdso support
[musl.git] / src / malloc / lite_malloc.c
bloba7e4a9f7b511bdbf2db4d5018e5088c19ed38570
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <limits.h>
4 #include <errno.h>
5 #include "libc.h"
7 #define ALIGN 16
9 void *__expand_heap(size_t *);
11 static void *__simple_malloc(size_t n)
13 static char *cur, *end;
14 static volatile int lock[2];
15 size_t align=1, pad;
16 void *p;
18 if (!n) n++;
19 while (align<n && align<ALIGN)
20 align += align;
22 LOCK(lock);
24 pad = -(uintptr_t)cur & align-1;
26 if (n <= SIZE_MAX/2 + ALIGN) n += pad;
28 if (n > end-cur) {
29 size_t m = n;
30 char *new = __expand_heap(&m);
31 if (!new) {
32 UNLOCK(lock);
33 return 0;
35 if (new != end) {
36 cur = new;
37 n -= pad;
38 pad = 0;
40 end = new + m;
43 p = cur + pad;
44 cur += n;
45 UNLOCK(lock);
46 return p;
49 weak_alias(__simple_malloc, malloc);
50 weak_alias(__simple_malloc, __malloc0);