| OLD | NEW |
| 1 #include <stdlib.h> | 1 #include <stdlib.h> |
| 2 #include <stdint.h> | 2 #include <stdint.h> |
| 3 #include <limits.h> | 3 #include <limits.h> |
| 4 #include <errno.h> | 4 #include <errno.h> |
| 5 #include "libc.h" | 5 #include "libc.h" |
| 6 | 6 |
| 7 #define ALIGN 16 | 7 #define ALIGN 16 |
| 8 | 8 |
| 9 void *__expand_heap(size_t *); | 9 void* __expand_heap(size_t*); |
| 10 | 10 |
| 11 static void *__simple_malloc(size_t n) | 11 static void* __simple_malloc(size_t n) { |
| 12 { | 12 static char *cur, *end; |
| 13 » static char *cur, *end; | 13 static volatile int lock[2]; |
| 14 » static volatile int lock[2]; | 14 size_t align = 1, pad; |
| 15 » size_t align=1, pad; | 15 void* p; |
| 16 » void *p; | |
| 17 | 16 |
| 18 » if (!n) n++; | 17 if (!n) |
| 19 » while (align<n && align<ALIGN) | 18 n++; |
| 20 » » align += align; | 19 while (align < n && align < ALIGN) |
| 20 align += align; |
| 21 | 21 |
| 22 » LOCK(lock); | 22 LOCK(lock); |
| 23 | 23 |
| 24 » pad = -(uintptr_t)cur & align-1; | 24 pad = -(uintptr_t)cur & align - 1; |
| 25 | 25 |
| 26 » if (n <= SIZE_MAX/2 + ALIGN) n += pad; | 26 if (n <= SIZE_MAX / 2 + ALIGN) |
| 27 n += pad; |
| 27 | 28 |
| 28 » if (n > end-cur) { | 29 if (n > end - cur) { |
| 29 » » size_t m = n; | 30 size_t m = n; |
| 30 » » char *new = __expand_heap(&m); | 31 char* new = __expand_heap(&m); |
| 31 » » if (!new) { | 32 if (!new) { |
| 32 » » » UNLOCK(lock); | 33 UNLOCK(lock); |
| 33 » » » return 0; | 34 return 0; |
| 34 » » } | 35 } |
| 35 » » if (new != end) { | 36 if (new != end) { |
| 36 » » » cur = new; | 37 cur = new; |
| 37 » » » n -= pad; | 38 n -= pad; |
| 38 » » » pad = 0; | 39 pad = 0; |
| 39 » » } | 40 } |
| 40 » » end = new + m; | 41 end = new + m; |
| 41 » } | 42 } |
| 42 | 43 |
| 43 » p = cur + pad; | 44 p = cur + pad; |
| 44 » cur += n; | 45 cur += n; |
| 45 » UNLOCK(lock); | 46 UNLOCK(lock); |
| 46 » return p; | 47 return p; |
| 47 } | 48 } |
| 48 | 49 |
| 49 weak_alias(__simple_malloc, malloc); | 50 weak_alias(__simple_malloc, malloc); |
| 50 weak_alias(__simple_malloc, __malloc0); | 51 weak_alias(__simple_malloc, __malloc0); |
| OLD | NEW |