Skip to content

Commit 18bef25

Browse files
committed
objlist: Add support for statically allocated lists.
Similar to similar support for lists.
1 parent f130ca1 commit 18bef25

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);
454454
mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
455455

456456
// list
457+
struct _mp_obj_list_t;
458+
void mp_obj_list_init(struct _mp_obj_list_t *o, uint n);
457459
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
458460
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
459461
void mp_obj_list_set_len(mp_obj_t self_in, uint len);

py/objlist.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
#include "obj.h"
99
#include "runtime0.h"
1010
#include "runtime.h"
11-
12-
typedef struct _mp_obj_list_t {
13-
mp_obj_base_t base;
14-
machine_uint_t alloc;
15-
machine_uint_t len;
16-
mp_obj_t *items;
17-
} mp_obj_list_t;
11+
#include "objlist.h"
1812

1913
STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
2014
STATIC mp_obj_list_t *list_new(uint n);
@@ -363,12 +357,16 @@ const mp_obj_type_t mp_type_list = {
363357
.locals_dict = (mp_obj_t)&list_locals_dict,
364358
};
365359

366-
STATIC mp_obj_list_t *list_new(uint n) {
367-
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
360+
void mp_obj_list_init(mp_obj_list_t *o, uint n) {
368361
o->base.type = &mp_type_list;
369362
o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
370363
o->len = n;
371364
o->items = m_new(mp_obj_t, o->alloc);
365+
}
366+
367+
STATIC mp_obj_list_t *list_new(uint n) {
368+
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
369+
mp_obj_list_init(o, n);
372370
return o;
373371
}
374372

py/objlist.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
typedef struct _mp_obj_list_t {
2+
mp_obj_base_t base;
3+
machine_uint_t alloc;
4+
machine_uint_t len;
5+
mp_obj_t *items;
6+
} mp_obj_list_t;

0 commit comments

Comments
 (0)