Skip to content

Commit d891452

Browse files
committed
py: Add MICROPY_MALLOC_USES_ALLOCATED_SIZE to allow simpler malloc API.
1 parent e104acd commit d891452

6 files changed

Lines changed: 35 additions & 2 deletions

File tree

py/malloc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ void *m_malloc0(size_t num_bytes) {
109109
return ptr;
110110
}
111111

112+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
112113
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
114+
#else
115+
void *m_realloc(void *ptr, size_t new_num_bytes) {
116+
#endif
113117
void *new_ptr = realloc(ptr, new_num_bytes);
114118
if (new_ptr == NULL && new_num_bytes != 0) {
115119
return m_malloc_fail(new_num_bytes);
@@ -129,7 +133,11 @@ void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
129133
return new_ptr;
130134
}
131135

136+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
132137
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
138+
#else
139+
void *m_realloc_maybe(void *ptr, size_t new_num_bytes) {
140+
#endif
133141
void *new_ptr = realloc(ptr, new_num_bytes);
134142
#if MICROPY_MEM_STATS
135143
// At first thought, "Total bytes allocated" should only grow,
@@ -149,7 +157,11 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
149157
return new_ptr;
150158
}
151159

160+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
152161
void m_free(void *ptr, size_t num_bytes) {
162+
#else
163+
void m_free(void *ptr) {
164+
#endif
153165
free(ptr);
154166
#if MICROPY_MEM_STATS
155167
MP_STATE_MEM(current_bytes_allocated) -= num_bytes;

py/misc.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,32 @@ typedef unsigned int uint;
6161
#else
6262
#define m_new_obj_with_finaliser(type) m_new_obj(type)
6363
#endif
64+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
6465
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
6566
#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
6667
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
67-
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
6868
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
69+
#else
70+
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (new_num))))
71+
#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (new_num))))
72+
#define m_del(type, ptr, num) ((void)(num), m_free(ptr))
73+
#define m_del_var(obj_type, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
74+
#endif
75+
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
6976

7077
void *m_malloc(size_t num_bytes);
7178
void *m_malloc_maybe(size_t num_bytes);
7279
void *m_malloc_with_finaliser(size_t num_bytes);
7380
void *m_malloc0(size_t num_bytes);
81+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
7482
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
7583
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
7684
void m_free(void *ptr, size_t num_bytes);
85+
#else
86+
void *m_realloc(void *ptr, size_t new_num_bytes);
87+
void *m_realloc_maybe(void *ptr, size_t new_num_bytes);
88+
void m_free(void *ptr);
89+
#endif
7790
void *m_malloc_fail(size_t num_bytes);
7891

7992
#if MICROPY_MEM_STATS

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
#define MICROPY_MODULE_DICT_SIZE (1)
113113
#endif
114114

115+
// Whether realloc/free should be passed allocated memory region size
116+
// You must enable this if MICROPY_MEM_STATS is enabled
117+
#ifndef MICROPY_MALLOC_USES_ALLOCATED_SIZE
118+
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (0)
119+
#endif
120+
115121
// Number of bytes used to store qstr length
116122
// Dictates hard limit on maximum Python identifier length, but 1 byte
117123
// (limit of 255 bytes in an identifier) should be enough for everyone

py/objarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
373373
if (len_adj > 0) {
374374
if (len_adj > o->free) {
375375
// TODO: alloc policy; at the moment we go conservative
376-
o->items = m_realloc(o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
376+
o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
377377
o->free = 0;
378378
}
379379
mp_seq_replace_slice_grow_inplace(o->items, o->len,

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define MICROPY_ENABLE_GC (1)
4444
#define MICROPY_ENABLE_FINALISER (1)
4545
#define MICROPY_STACK_CHECK (1)
46+
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
4647
#define MICROPY_MEM_STATS (1)
4748
#define MICROPY_DEBUG_PRINTERS (1)
4849
#define MICROPY_HELPER_REPL (1)

windows/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define MICROPY_ENABLE_GC (1)
4040
#define MICROPY_ENABLE_FINALISER (1)
4141
#define MICROPY_STACK_CHECK (1)
42+
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
4243
#define MICROPY_MEM_STATS (1)
4344
#define MICROPY_DEBUG_PRINTERS (1)
4445
#define MICROPY_HELPER_REPL (1)

0 commit comments

Comments
 (0)