Skip to content

Commit 0e7bfc8

Browse files
jimmodpgeorge
authored andcommitted
all: Use mp_obj_malloc everywhere it's applicable.
This replaces occurences of foo_t *foo = m_new_obj(foo_t); foo->base.type = &foo_type; with foo_t *foo = mp_obj_malloc(foo_t, &foo_type); Excludes any places where base is a sub-field or when new0/memset is used. Signed-off-by: Jim Mussared <[email protected]>
1 parent 6a3bc0e commit 0e7bfc8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+145
-294
lines changed

extmod/machine_i2c.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,7 @@ STATIC void mp_machine_soft_i2c_init(mp_obj_base_t *self_in, size_t n_args, cons
668668

669669
STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
670670
// create new soft I2C object
671-
machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t);
672-
self->base.type = &mp_machine_soft_i2c_type;
671+
machine_i2c_obj_t *self = mp_obj_malloc(machine_i2c_obj_t, &mp_machine_soft_i2c_type);
673672
mp_map_t kw_args;
674673
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
675674
mp_machine_soft_i2c_init(&self->base, n_args, args, &kw_args);

extmod/machine_signal.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t
108108
}
109109
}
110110

111-
machine_signal_t *o = m_new_obj(machine_signal_t);
112-
o->base.type = type;
111+
machine_signal_t *o = mp_obj_malloc(machine_signal_t, type);
113112
o->pin = pin;
114113
o->invert = invert;
115114
return MP_OBJ_FROM_PTR(o);

extmod/machine_spi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
175175
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
176176

177177
// create new object
178-
mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
179-
self->base.type = &mp_machine_soft_spi_type;
178+
mp_machine_soft_spi_obj_t *self = mp_obj_malloc(mp_machine_soft_spi_obj_t, &mp_machine_soft_spi_type);
180179

181180
// set parameters
182181
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);

extmod/modbluetooth.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ STATIC mp_obj_t bluetooth_uuid_make_new(const mp_obj_type_t *type, size_t n_args
9797

9898
mp_arg_check_num(n_args, n_kw, 1, 1, false);
9999

100-
mp_obj_bluetooth_uuid_t *self = m_new_obj(mp_obj_bluetooth_uuid_t);
101-
self->base.type = &mp_type_bluetooth_uuid;
100+
mp_obj_bluetooth_uuid_t *self = mp_obj_malloc(mp_obj_bluetooth_uuid_t, &mp_type_bluetooth_uuid);
102101

103102
if (mp_obj_is_int(all_args[0])) {
104103
self->type = MP_BLUETOOTH_UUID_TYPE_16;

extmod/modbtree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ void __dbpanic(DB *db) {
6767
}
6868

6969
STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) {
70-
mp_obj_btree_t *o = m_new_obj(mp_obj_btree_t);
71-
o->base.type = &btree_type;
70+
mp_obj_btree_t *o = mp_obj_malloc(mp_obj_btree_t, &btree_type);
7271
o->stream = stream;
7372
o->db = db;
7473
o->start_key = mp_const_none;

extmod/modframebuf.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, u
266266
STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
267267
mp_arg_check_num(n_args, n_kw, 4, 5, false);
268268

269-
mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
270-
o->base.type = type;
269+
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, type);
271270
o->buf_obj = args[0];
272271

273272
mp_buffer_info_t bufinfo;
@@ -628,8 +627,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
628627

629628
// this factory function is provided for backwards compatibility with old FrameBuffer1 class
630629
STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args) {
631-
mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
632-
o->base.type = &mp_type_framebuf;
630+
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, &mp_type_framebuf);
633631

634632
mp_buffer_info_t bufinfo;
635633
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);

extmod/moduasyncio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ STATIC int task_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) {
8787
STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
8888
(void)args;
8989
mp_arg_check_num(n_args, n_kw, 0, 0, false);
90-
mp_obj_task_queue_t *self = m_new_obj(mp_obj_task_queue_t);
91-
self->base.type = type;
90+
mp_obj_task_queue_t *self = mp_obj_malloc(mp_obj_task_queue_t, type);
9291
self->heap = (mp_obj_task_t *)mp_pairheap_new(task_lt);
9392
return MP_OBJ_FROM_PTR(self);
9493
}

extmod/moducryptolib.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ STATIC mp_obj_t ucryptolib_aes_make_new(const mp_obj_type_t *type, size_t n_args
228228
mp_raise_ValueError(MP_ERROR_TEXT("mode"));
229229
}
230230

231-
mp_obj_aes_t *o = m_new_obj_var(mp_obj_aes_t, struct ctr_params, !!is_ctr_mode(block_mode));
232-
o->base.type = type;
231+
mp_obj_aes_t *o = mp_obj_malloc_var(mp_obj_aes_t, struct ctr_params, !!is_ctr_mode(block_mode), type);
233232

234233
o->block_mode = block_mode;
235234
o->key_type = AES_KEYTYPE_NONE;

extmod/moductypes.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ STATIC NORETURN void syntax_error(void) {
9595

9696
STATIC mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
9797
mp_arg_check_num(n_args, n_kw, 2, 3, false);
98-
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
99-
o->base.type = type;
98+
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, type);
10099
o->addr = (void *)(uintptr_t)mp_obj_int_get_truncated(args[0]);
101100
o->desc = args[1];
102101
o->flags = LAYOUT_NATIVE;
@@ -463,8 +462,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
463462

464463
switch (agg_type) {
465464
case STRUCT: {
466-
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
467-
o->base.type = &uctypes_struct_type;
465+
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
468466
o->desc = sub->items[1];
469467
o->addr = self->addr + offset;
470468
o->flags = self->flags;
@@ -479,8 +477,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
479477
MP_FALLTHROUGH
480478
}
481479
case PTR: {
482-
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
483-
o->base.type = &uctypes_struct_type;
480+
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
484481
o->desc = MP_OBJ_FROM_PTR(sub);
485482
o->addr = self->addr + offset;
486483
o->flags = self->flags;
@@ -552,8 +549,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
552549
} else if (value == MP_OBJ_SENTINEL) {
553550
mp_uint_t dummy = 0;
554551
mp_uint_t size = uctypes_struct_size(t->items[2], self->flags, &dummy);
555-
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
556-
o->base.type = &uctypes_struct_type;
552+
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
557553
o->desc = t->items[2];
558554
o->addr = self->addr + size * index;
559555
o->flags = self->flags;
@@ -570,8 +566,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
570566
} else {
571567
mp_uint_t dummy = 0;
572568
mp_uint_t size = uctypes_struct_size(t->items[1], self->flags, &dummy);
573-
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
574-
o->base.type = &uctypes_struct_type;
569+
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
575570
o->desc = t->items[1];
576571
o->addr = p + size * index;
577572
o->flags = self->flags;

extmod/moduhashlib.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ STATIC mp_obj_t uhashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg);
8383

8484
STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
8585
mp_arg_check_num(n_args, n_kw, 0, 1, false);
86-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_sha256_context));
87-
o->base.type = type;
86+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_sha256_context), type);
8887
o->final = false;
8988
mbedtls_sha256_init((mbedtls_sha256_context *)&o->state);
9089
mbedtls_sha256_starts_ret((mbedtls_sha256_context *)&o->state, 0);
@@ -119,8 +118,7 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
119118

120119
STATIC mp_obj_t uhashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
121120
mp_arg_check_num(n_args, n_kw, 0, 1, false);
122-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(CRYAL_SHA256_CTX));
123-
o->base.type = type;
121+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(CRYAL_SHA256_CTX), type);
124122
o->final = false;
125123
sha256_init((CRYAL_SHA256_CTX *)o->state);
126124
if (n_args == 1) {
@@ -173,8 +171,7 @@ STATIC mp_obj_t uhashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg);
173171
#if MICROPY_SSL_AXTLS
174172
STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
175173
mp_arg_check_num(n_args, n_kw, 0, 1, false);
176-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX));
177-
o->base.type = type;
174+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(SHA1_CTX), type);
178175
o->final = false;
179176
SHA1_Init((SHA1_CTX *)o->state);
180177
if (n_args == 1) {
@@ -213,8 +210,7 @@ STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) {
213210

214211
STATIC mp_obj_t uhashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
215212
mp_arg_check_num(n_args, n_kw, 0, 1, false);
216-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_sha1_context));
217-
o->base.type = type;
213+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_sha1_context), type);
218214
o->final = false;
219215
mbedtls_sha1_init((mbedtls_sha1_context *)o->state);
220216
mbedtls_sha1_starts_ret((mbedtls_sha1_context *)o->state);
@@ -268,8 +264,7 @@ STATIC mp_obj_t uhashlib_md5_update(mp_obj_t self_in, mp_obj_t arg);
268264
#if MICROPY_SSL_AXTLS
269265
STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
270266
mp_arg_check_num(n_args, n_kw, 0, 1, false);
271-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(MD5_CTX));
272-
o->base.type = type;
267+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(MD5_CTX), type);
273268
o->final = false;
274269
MD5_Init((MD5_CTX *)o->state);
275270
if (n_args == 1) {
@@ -308,8 +303,7 @@ STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) {
308303

309304
STATIC mp_obj_t uhashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
310305
mp_arg_check_num(n_args, n_kw, 0, 1, false);
311-
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_md5_context));
312-
o->base.type = type;
306+
mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_md5_context), type);
313307
o->final = false;
314308
mbedtls_md5_init((mbedtls_md5_context *)o->state);
315309
mbedtls_md5_starts_ret((mbedtls_md5_context *)o->state);

0 commit comments

Comments
 (0)