|
55 | 55 | // example. On the other hand, some (e.g. bare-metal) ports may use GC
|
56 | 56 | // heap as system heap, so, to avoid warnings, we do undef's first.
|
57 | 57 | // CIRCUITPY-CHANGE: Add selective collect support to malloc to optimize GC for large buffers
|
58 |
| -#undef malloc |
59 | 58 | #undef free
|
60 | 59 | #undef realloc
|
61 |
| -#if MICROPY_ENABLE_SELECTIVE_COLLECT |
62 |
| -#define malloc(b) gc_alloc((b), GC_ALLOC_FLAG_DO_NOT_COLLECT) |
63 |
| -#define malloc_with_collect(b) gc_alloc((b), 0) |
64 |
| -#define malloc_without_collect(b) gc_alloc((b), GC_ALLOC_FLAG_DO_NOT_COLLECT) |
65 |
| -#else |
66 |
| -#define malloc(b) gc_alloc((b), 0) |
67 |
| -#define malloc_with_collect(b) gc_alloc((b), 0) |
68 |
| -#endif |
69 |
| -#define malloc_with_finaliser(b) gc_alloc((b), GC_ALLOC_FLAG_HAS_FINALISER) |
70 | 60 | #define free gc_free
|
71 | 61 | #define realloc(ptr, n) gc_realloc(ptr, n, true)
|
72 | 62 | #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
|
@@ -99,15 +89,18 @@ static void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
|
99 | 89 | void *m_malloc_helper(size_t num_bytes, uint8_t flags) {
|
100 | 90 | void *ptr;
|
101 | 91 | #if MICROPY_ENABLE_GC
|
| 92 | + uint8_t gc_flags = 0; |
102 | 93 | #if MICROPY_ENABLE_SELECTIVE_COLLECT
|
103 | 94 | if ((flags & M_MALLOC_COLLECT) == 0) {
|
104 |
| - ptr = malloc_without_collect(num_bytes); |
105 |
| - } else { |
106 |
| - ptr = malloc_with_collect(num_bytes); |
| 95 | + gc_flags |= GC_ALLOC_FLAG_DO_NOT_COLLECT; |
| 96 | + } |
| 97 | + #endif |
| 98 | + #if MICROPY_ENABLE_FINALISER |
| 99 | + if ((flags & M_MALLOC_WITH_FINALISER) != 0) { |
| 100 | + gc_flags |= GC_ALLOC_FLAG_HAS_FINALISER; |
107 | 101 | }
|
108 |
| - #else |
109 |
| - ptr = malloc_with_collect(num_bytes); |
110 | 102 | #endif
|
| 103 | + ptr = gc_alloc(num_bytes, gc_flags); |
111 | 104 | #else
|
112 | 105 | ptr = malloc(num_bytes);
|
113 | 106 | #endif
|
|
0 commit comments