Skip to content

Commit b42fbfc

Browse files
committed
Better delayed-initialization for malloc et al.
1 parent 0104f11 commit b42fbfc

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

acorn/src/malloc_wrapper.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ static void free_wrap(void * p) {
3131

3232
void init_malloc_wrapper() {
3333
malloc_sem = xSemaphoreCreateRecursiveMutex();
34-
old_malloc = malloc;
35-
old_realloc = realloc;
36-
old_free = free;
37-
malloc = malloc_wrap;
38-
realloc = realloc_wrap;
39-
free = free_wrap;
34+
old_malloc = malloc_ptr;
35+
old_realloc = realloc_ptr;
36+
old_free = free_ptr;
37+
malloc_ptr = malloc_wrap;
38+
realloc_ptr = realloc_wrap;
39+
free_ptr = free_wrap;
4040
}
4141

libc/include/malloc.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
BEGIN_DECL
99

10-
// Due to chicken-and-egg issues, we may need to call this from other modules.
11-
// Only call it from global constructors however.
12-
void _uc_sdk_ensure_malloc_exists();
13-
1410
typedef void * (*malloc_t)(size_t size);
1511
typedef void (*free_t)(void * ptr);
1612
typedef void * (*realloc_t)(void * ptr, size_t size);
@@ -19,9 +15,13 @@ void * base_malloc(size_t size);
1915
void base_free(void * ptr);
2016
void * base_realloc(void * ptr, size_t size);
2117

22-
extern malloc_t malloc;
23-
extern free_t free;
24-
extern realloc_t realloc;
18+
void * malloc(size_t size);
19+
void free(void * ptr);
20+
void * realloc(void * ptr, size_t size);
21+
22+
extern malloc_t malloc_ptr;
23+
extern free_t free_ptr;
24+
extern realloc_t realloc_ptr;
2525

2626
static __inline__ void * calloc(size_t nmemb, size_t size) {
2727
uint8_t * r = (uint8_t *)malloc(nmemb * size);

libc/src/initfini.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ void exit(int return_code) {
8181
}
8282

8383
void libc_init() {
84-
__sinit(_impure_ptr);
8584
__libc_init_array();
85+
__sinit(_impure_ptr);
8686
}

libc/src/malloc.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,18 @@ void base_free(void * ptr) {
199199
cur->prev->next = cur->next;
200200
}
201201

202-
malloc_t malloc = base_malloc;
203-
free_t free = base_free;
204-
realloc_t realloc = base_realloc;
202+
malloc_t malloc_ptr = NULL;
203+
free_t free_ptr = NULL;
204+
realloc_t realloc_ptr = NULL;
205205

206-
// Due to chicken-and-egg issues, we may need to call this from other modules.
207-
// Only call it from global constructors however.
208-
void _uc_sdk_ensure_malloc_exists() {
209-
malloc = base_malloc;
210-
free = base_free;
211-
realloc = base_realloc;
206+
void * malloc(size_t size) {
207+
return malloc_ptr ? malloc_ptr(size) : base_malloc(size);
208+
}
209+
210+
void free(void * ptr) {
211+
free_ptr ? free_ptr(ptr) : base_free(ptr);
212+
}
213+
214+
void * realloc(void * ptr, size_t size) {
215+
return realloc_ptr ? realloc_ptr(size) : base_realloc(ptr, size);
212216
}

os/src/fio.c

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ __attribute__((constructor)) static void fio_init() {
4141
fio_fds[0].fdread = stdin_read;
4242
fio_fds[1].fdwrite = stdout_write;
4343
fio_fds[2].fdwrite = stdout_write;
44-
_uc_sdk_ensure_malloc_exists();
4544
fio_sem = xSemaphoreCreateMutex();
4645
}
4746

os/src/sbrk.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void * sbrk(ptrdiff_t incr) {
2121
prev_heap_end = heap_end ? heap_end : (void *) &__heap_start;
2222

2323
/* Align to always be on 8-byte boundaries */
24-
next_heap_end = (void *)((((uintptr_t)heap_end + incr) + 7) & ~7);
24+
next_heap_end = (void *)((((uintptr_t)prev_heap_end + incr) + 7) & ~7);
2525

2626
/* Check if this allocation would exceed the end of the ram - would probably get into the stack first however */
2727
if (next_heap_end > stack_min) {

0 commit comments

Comments
 (0)