Skip to content

Commit 732407f

Browse files
committed
Change memory allocation API to require size for free and realloc.
1 parent a1c8e57 commit 732407f

22 files changed

Lines changed: 57 additions & 52 deletions

py/asmthumb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ asm_thumb_t *asm_thumb_new(uint max_num_labels) {
4444

4545
void asm_thumb_free(asm_thumb_t *as, bool free_code) {
4646
if (free_code) {
47-
m_free(as->code_base);
47+
m_del(byte, as->code_base, as->code_size);
4848
}
4949
/*
5050
if (as->label != NULL) {
@@ -58,7 +58,7 @@ void asm_thumb_free(asm_thumb_t *as, bool free_code) {
5858
g_array_free(as->label, true);
5959
}
6060
*/
61-
m_free(as);
61+
m_del_obj(asm_thumb_t, as);
6262
}
6363

6464
void asm_thumb_start_pass(asm_thumb_t *as, int pass) {

py/asmx64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void asm_x64_free(asm_x64_t* as, bool free_code) {
128128
g_array_free(as->label, true);
129129
}
130130
*/
131-
m_free(as);
131+
m_del_obj(asm_x64_t, as);
132132
}
133133

134134
void asm_x64_start_pass(asm_x64_t *as, int pass) {

py/builtin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
158158
char *str = m_new(char, 2);
159159
str[0] = ord;
160160
str[1] = '\0';
161-
return mp_obj_new_str(qstr_from_str_take(str));
161+
return mp_obj_new_str(qstr_from_str_take(str, 2));
162162
} else {
163163
nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "chr() arg not in range(0x110000)"));
164164
}

py/compile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
11841184
}
11851185
strcat(str, qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
11861186
}
1187-
*q2 = qstr_from_str_take(str);
1187+
*q2 = qstr_from_str_take(str, len + 1);
11881188
EMIT(import_name, *q2);
11891189
if (is_as) {
11901190
for (int i = 1; i < n; i++) {
@@ -2127,7 +2127,7 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
21272127
strcat(cat_str, str);
21282128
}
21292129

2130-
EMIT(load_const_str, qstr_from_str_take(cat_str), string_kind == MP_PARSE_NODE_BYTES);
2130+
EMIT(load_const_str, qstr_from_str_take(cat_str, n_bytes + 1), string_kind == MP_PARSE_NODE_BYTES);
21312131
}
21322132

21332133
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
@@ -3105,7 +3105,7 @@ bool mp_compile(mp_parse_node_t pn, bool is_repl) {
31053105
}
31063106
}
31073107

3108-
m_free(comp);
3108+
m_del_obj(compiler_t, comp);
31093109

31103110
return !comp->had_error;
31113111
}

py/emitpass1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ emit_t *emit_pass1_new(qstr qstr___class__) {
2626
}
2727

2828
void emit_pass1_free(emit_t *emit) {
29-
m_free(emit);
29+
m_del_obj(emit_t, emit);
3030
}
3131

3232
static void emit_pass1_dummy(emit_t *emit) {

py/lexer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ static void next_char(mp_lexer_t *lex) {
183183

184184
void indent_push(mp_lexer_t *lex, uint indent) {
185185
if (lex->num_indent_level >= lex->alloc_indent_level) {
186+
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level * 2);
186187
lex->alloc_indent_level *= 2;
187-
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level);
188188
}
189189
lex->indent_level[lex->num_indent_level++] = indent;
190190
}
@@ -639,7 +639,7 @@ void mp_lexer_free(mp_lexer_t *lex) {
639639
lex->stream_close(lex->stream_data);
640640
}
641641
vstr_clear(&lex->vstr);
642-
m_free(lex);
642+
m_del_obj(mp_lexer_t, lex);
643643
}
644644
}
645645

py/lexerunix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ unichar str_buf_next_char(str_buf_t *sb) {
2424
void str_buf_free(str_buf_t *sb) {
2525
if (sb) {
2626
if (sb->free) {
27-
m_free((char*)sb->src_beg);
27+
m_del(char, (char*)sb->src_beg, 0 /* unknown size of src_beg */);
2828
}
29-
m_free(sb);
29+
m_del_obj(str_buf_t, sb);
3030
}
3131
}
3232

@@ -52,7 +52,7 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
5252
close(fd);
5353
if (read_size != size) {
5454
printf("error reading file %s\n", filename);
55-
m_free(data);
55+
m_del(char, data, size);
5656
return NULL;
5757
}
5858

py/malloc.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55

66
static int total_bytes_allocated = 0;
77

8-
void m_free(void *ptr) {
9-
if (ptr != NULL) {
10-
free(ptr);
11-
}
12-
}
13-
148
void *m_malloc(int num_bytes) {
159
if (num_bytes == 0) {
1610
return NULL;
@@ -37,20 +31,26 @@ void *m_malloc0(int num_bytes) {
3731
return ptr;
3832
}
3933

40-
void *m_realloc(void *ptr, int num_bytes) {
41-
if (num_bytes == 0) {
34+
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
35+
if (new_num_bytes == 0) {
4236
free(ptr);
4337
return NULL;
4438
}
45-
ptr = realloc(ptr, num_bytes);
39+
ptr = realloc(ptr, new_num_bytes);
4640
if (ptr == NULL) {
47-
printf("could not allocate memory, reallocating %d bytes\n", num_bytes);
41+
printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
4842
return NULL;
4943
}
50-
total_bytes_allocated += num_bytes;
44+
total_bytes_allocated += new_num_bytes;
5145
return ptr;
5246
}
5347

48+
void m_free(void *ptr, int num_bytes) {
49+
if (ptr != NULL) {
50+
free(ptr);
51+
}
52+
}
53+
5454
int m_get_total_bytes_allocated(void) {
5555
return total_bytes_allocated;
5656
}

py/map.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
6464
mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value;
6565
}
6666
}
67-
m_free(old_table);
67+
m_del(mp_map_elem_t, old_table, old_alloc);
6868
// restart the search for the new element
6969
pos = hash % map->alloc;
7070
} else {
@@ -124,7 +124,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
124124
mp_set_lookup(set, old_table[i], true);
125125
}
126126
}
127-
m_free(old_table);
127+
m_del(mp_obj_t, old_table, old_alloc);
128128
// restart the search for the new element
129129
pos = hash % set->alloc;
130130
} else {

py/misc.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ typedef unsigned int uint;
1616

1717
/** memomry allocation ******************************************/
1818

19+
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
20+
1921
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
2022
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
21-
#define m_renew(type, ptr, num) ((type*)(m_realloc((ptr), sizeof(type) * (num))))
2223
#define m_new_obj(type) (m_new(type, 1))
2324
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
25+
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
26+
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
27+
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
2428

25-
void m_free(void *ptr);
2629
void *m_malloc(int num_bytes);
2730
void *m_malloc0(int num_bytes);
28-
void *m_realloc(void *ptr, int num_bytes);
31+
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
32+
void m_free(void *ptr, int num_bytes);
2933

3034
int m_get_total_bytes_allocated(void);
3135

@@ -96,7 +100,7 @@ typedef unsigned int qstr;
96100

97101
void qstr_init(void);
98102
qstr qstr_from_str_static(const char *str);
99-
qstr qstr_from_str_take(char *str);
103+
qstr qstr_from_str_take(char *str, int alloc_len);
100104
qstr qstr_from_strn_copy(const char *str, int len);
101105
const char* qstr_str(qstr qstr);
102106

0 commit comments

Comments
 (0)