8000 Change memory allocation API to require size for free and realloc. · micropython/micropython@732407f · GitHub
[go: up one dir, main page]

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

+57
-52
lines changed

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( 9E88 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 1241 +
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

py/objclass.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mp_obj_t class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
3737
memcpy(args2, args, n_args * sizeof(mp_obj_t));
3838
args2[n_args] = o;
3939
init_ret = rt_call_function_n(init_fn->value, n_args + 1, args2);
40-
m_free(args2);
40+
m_del(mp_obj_t, args2, n_args + 1);
4141
}
4242
if (init_ret != mp_const_none) {
4343
nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "__init__() should return None, not '%s'", mp_obj_get_type_str(init_ret)));

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
5858
}
5959

6060
mp_obj_t res = ((mp_fun_var_t)self->fun)(n_args, args_ordered);
61-
m_free(args_ordered);
61+
m_del(mp_obj_t, args_ordered, n_args);
6262

6363
return res;
6464
}

py/objlist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
5757
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
5858
mp_obj_list_t *self = self_in;
5959
if (self->len >= self->alloc) {
60+
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
6061
self->alloc *= 2;
61-
self->items = m_renew(mp_obj_t, self->items, self->alloc);
6262
}
6363
self->items[self->len++] = arg;
6464
return mp_const_none; // return None, as per CPython

py/objstr.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
3636
if (MP_OBJ_IS_TYPE(rhs_in, &str_type)) {
3737
// add 2 strings
3838
const char *rhs_str = qstr_str(((mp_obj_str_t*)rhs_in)->qstr);
39-
char *val = m_new(char, strlen(lhs_str) + strlen(rhs_str) + 1);
39+
int alloc_len = strlen(lhs_str) + strlen(rhs_str) + 1;
40+
char *val = m_new(char, alloc_len);
4041
stpcpy(stpcpy(val, lhs_str), rhs_str);
41-
return mp_obj_new_str(qstr_from_str_take(val));
42+
return mp_obj_new_str(qstr_from_str_take(val, alloc_len));
4243
}
4344
break;
4445
}
@@ -78,7 +79,7 @@ mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
7879
}
7980
strcat(joined_str, s2);
8081
}
81-
return mp_obj_new_str(qstr_from_str_take(joined_str));
82+
return mp_obj_new_str(qstr_from_str_take(joined_str, required_len + 1));
8283

8384
bad_arg:
8485
nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "?str.join expecting a list of str's"));
@@ -115,7 +116,7 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
115116
}
116117
}
117118

118-
return mp_obj_new_str(qstr_from_str_take(vstr->buf));
119+
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
119120
}
120121

121122
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);

py/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ typedef struct _parser_t {
9494

9595
static void push_rule(parser_t *parser, const rule_t *rule, int arg_i) {
9696
if (parser->rule_stack_top >= parser->rule_stack_alloc) {
97+
parser->rule_stack = m_renew(rule_stack_t, parser->rule_stack, parser->rule_stack_alloc, parser->rule_stack_alloc * 2);
9798
parser->rule_stack_alloc *= 2;
98-
parser->rule_stack = m_renew(rule_stack_t, parser->rule_stack, parser->rule_stack_alloc);
9999
}
100100
parser->rule_stack[parser->rule_stack_top].rule_id = rule->rule_id;
101101
parser->rule_stack[parser->rule_stack_top].arg_i = arg_i;

py/qstr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void qstr_init(void) {
1616

1717
static qstr qstr_add(const char *str) {
1818
if (qstrs_len >= qstrs_alloc) {
19+
qstrs = m_renew(const char*, qstrs, qstrs_alloc, qstrs_alloc * 2);
1920
qstrs_alloc *= 2;
20-
qstrs = m_renew(const char*, qstrs, qstrs_alloc);
2121
}
2222
qstrs[qstrs_len++] = str;
2323
return qstrs_len - 1;
@@ -32,10 +32,10 @@ qstr qstr_from_str_static(const char *str) {
3232
return qstr_add(str);
3333
}
3434

35-
qstr qstr_from_str_take(char *str) {
35+
qstr qstr_from_str_take(char *str, int alloc_len) {
3636
for (int i = 0; i < qstrs_len; i++) {
3737
if (strcmp(qstrs[i], str) == 0) {
38-
m_free(str);
38+
m_del(char, str, alloc_len);
3939
return i;
4040
}
4141
}

py/scope.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
6969

7070
// make sure we have enough memory
7171
if (scope->id_info_len >= scope->id_info_alloc) {
72+
scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc * 2);
7273
scope->id_info_alloc *= 2;
73-
scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc);
7474
}
7575

7676
id_info_t *id_info;

py/vstr.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ void vstr_init(vstr_t *vstr) {
1111
vstr->len = 0;
1212
vstr->buf = m_new(char, vstr->alloc);
1313
if (vstr->buf == NULL) {
14-
m_free(vstr);
1514
vstr->had_error = true;
1615
return;
1716
}
@@ -20,7 +19,7 @@ void vstr_init(vstr_t *vstr) {
2019
}
2120

2221
void vstr_clear(vstr_t *vstr) {
23-
m_free(vstr->buf);
22+
m_del(char, vstr->buf, vstr->alloc);
2423
vstr->buf = NULL;
2524
}
2625

@@ -35,8 +34,8 @@ vstr_t *vstr_new(void) {
3534

3635
void vstr_free(vstr_t *vstr) {
3736
if (vstr != NULL) {
38-
m_free(vstr->buf);
39-
m_free(vstr);
37+
m_del(char, vstr->buf, vstr->alloc);
38+
m_del_obj(vstr_t, vstr);
4039
}
4140
}
4241

@@ -67,7 +66,7 @@ int vstr_len(vstr_t *vstr) {
6766
bool vstr_ensure_extra(vstr_t *vstr, int size) {
6867
if (vstr->len + size + 1 > vstr->alloc) {
6968
int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
70-
char *new_buf = m_renew(char, vstr->buf, new_alloc);
69+
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
7170
if (new_buf == NULL) {
7271
vstr->had_error = true;
7372
return false;

stm/lexerstm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ unichar str_buf_next_char(mp_lexer_str_buf_t *sb) {
1717

1818
void str_buf_free(mp_lexer_str_buf_t *sb) {
1919
if (sb->free) {
20-
m_free((char*)sb->src_beg);
20+
m_del(char, (char*)sb->src_beg, 0 /* don't know allocated size of src */);
2121
}
2222
}
2323

stm/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) {
706706
UINT n_out;
707707
f_read(&self->fp, buf, n, &n_out);
708708
buf[n_out] = 0;
709-
return mp_obj_new_str(qstr_from_str_take(buf));
709+
return mp_obj_new_str(qstr_from_str_take(buf, n + 1));
710710
}
711711

712712
mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) {

stm/pybwlan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ mp_obj_t pyb_wlan_http_get(mp_obj_t host_name, mp_obj_t host_path) {
191191
vstr_add_strn(vstr, buf, ret);
192192
}
193193

194-
mp_ret = mp_obj_new_str(qstr_from_str_take(vstr_str(vstr)));
194+
mp_ret = mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
195195
}
196196

197197
closesocket(sd);

unix/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdint.h>
22
#include <stdio.h>
33
#include <string.h>
4+
#include <malloc.h>
45

56
#include "nlr.h"
67
#include "misc.h"
@@ -44,8 +45,8 @@ static void do_repl(void) {
4445
break;
4546
}
4647
char *line3 = str_join(line, '\n', line2);
47-
m_free(line);
48-
m_free(line2);
48+
free(line);
49+
free(line2);
4950
line = line3;
5051
}
5152
}

0 commit comments

Comments
 (0)
0