Skip to content

Commit 4ecc0e7

Browse files
authored
Merge pull request #10308 from tannewt/fix_finalisers
Fix finaliser in malloc helper
2 parents 054c0c1 + fba4e68 commit 4ecc0e7

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

py/malloc.c

+8-15
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,8 @@
5555
// example. On the other hand, some (e.g. bare-metal) ports may use GC
5656
// heap as system heap, so, to avoid warnings, we do undef's first.
5757
// CIRCUITPY-CHANGE: Add selective collect support to malloc to optimize GC for large buffers
58-
#undef malloc
5958
#undef free
6059
#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)
7060
#define free gc_free
7161
#define realloc(ptr, n) gc_realloc(ptr, n, true)
7262
#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) {
9989
void *m_malloc_helper(size_t num_bytes, uint8_t flags) {
10090
void *ptr;
10191
#if MICROPY_ENABLE_GC
92+
uint8_t gc_flags = 0;
10293
#if MICROPY_ENABLE_SELECTIVE_COLLECT
10394
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;
107101
}
108-
#else
109-
ptr = malloc_with_collect(num_bytes);
110102
#endif
103+
ptr = gc_alloc(num_bytes, gc_flags);
111104
#else
112105
ptr = malloc(num_bytes);
113106
#endif

0 commit comments

Comments
 (0)