Skip to content

Commit b62c30b

Browse files
committed
Generalize malloc-via-gc-heap support, make it available to unix port.
1 parent 8d90a38 commit b62c30b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

py/malloc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ static int peak_bytes_allocated = 0;
1919
#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
2020
#endif
2121

22+
#if MICROPY_ENABLE_GC
23+
#include "gc.h"
24+
25+
// We redirect standard alloc functions to GC heap - just for the rest of
26+
// this module. In the rest of micropython source, system malloc can be
27+
// freely accessed - for interfacing with system and 3rd-party libs for
28+
// example. On the other hand, some (e.g. bare-metal) ports may use GC
29+
// heap as system heap, so, to avoid warnings, we do undef's first.
30+
#undef malloc
31+
#undef free
32+
#undef realloc
33+
#define malloc gc_alloc
34+
#define free gc_free
35+
#define realloc gc_realloc
36+
#endif // MICROPY_ENABLE_GC
37+
2238
void *m_malloc(int num_bytes) {
2339
if (num_bytes == 0) {
2440
return NULL;

stm/malloc0.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ void *realloc(void *ptr, size_t n) {
2929

3030
#endif
3131

32-
void *malloc(size_t n) {
33-
return gc_alloc(n);
34-
}
35-
36-
void free(void *ptr) {
37-
gc_free(ptr);
38-
}
39-
40-
void *realloc(void *ptr, size_t n) {
41-
return gc_realloc(ptr, n);
42-
}
43-
4432
void __assert_func(void) {
4533
printf("\nASSERT FAIL!");
4634
for (;;) {

0 commit comments

Comments
 (0)