summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-02-16 11:41:28 +1100
committerDamien George <damien@micropython.org>2024-02-19 23:40:54 +1100
commit24234937747e6d7fd920d21358fb26b826398e1a (patch)
tree7f6662dc0889b5a1c051fc1b12cb7c8077584fd3
parent9242e3d16d34dd15f1cd868681be45432b443c5a (diff)
py/obj: Change sizeof to offsetof in mp_obj_malloc_var macro.
Following b6a977848407a4ced45d118cf926bd915cc89dfb, to properly calculate the size of the variable-length allocation. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--extmod/modcryptolib.c6
-rw-r--r--extmod/modhashlib.c12
-rw-r--r--extmod/modre.c2
-rw-r--r--ports/nrf/boards/MICROBIT/modules/microbitimage.c2
-rw-r--r--ports/unix/modffi.c4
-rw-r--r--py/modio.c2
-rw-r--r--py/obj.h2
-rw-r--r--py/objattrtuple.c2
-rw-r--r--py/objclosure.c2
-rw-r--r--py/objfun.c2
-rw-r--r--py/objgenerator.c4
-rw-r--r--py/objmap.c2
-rw-r--r--py/objnamedtuple.c2
-rw-r--r--py/objtuple.c2
-rw-r--r--py/objtype.c2
-rw-r--r--py/objzip.c2
16 files changed, 25 insertions, 25 deletions
diff --git a/extmod/modcryptolib.c b/extmod/modcryptolib.c
index b33d8533f..66511b0f7 100644
--- a/extmod/modcryptolib.c
+++ b/extmod/modcryptolib.c
@@ -87,6 +87,7 @@ typedef struct _mp_obj_aes_t {
#define AES_KEYTYPE_ENC 1
#define AES_KEYTYPE_DEC 2
uint8_t key_type : 2;
+ struct ctr_params ctr_params[]; // optional
} mp_obj_aes_t;
static inline bool is_ctr_mode(int block_mode) {
@@ -98,8 +99,7 @@ static inline bool is_ctr_mode(int block_mode) {
}
static inline struct ctr_params *ctr_params_from_aes(mp_obj_aes_t *o) {
- // ctr_params follows aes object struct
- return (struct ctr_params *)&o[1];
+ return &o->ctr_params[0];
}
#if MICROPY_SSL_AXTLS
@@ -228,7 +228,7 @@ STATIC mp_obj_t cryptolib_aes_make_new(const mp_obj_type_t *type, size_t n_args,
mp_raise_ValueError(MP_ERROR_TEXT("mode"));
}
- mp_obj_aes_t *o = mp_obj_malloc_var(mp_obj_aes_t, struct ctr_params, !!is_ctr_mode(block_mode), type);
+ mp_obj_aes_t *o = mp_obj_malloc_var(mp_obj_aes_t, ctr_params, struct ctr_params, !!is_ctr_mode(block_mode), type);
o->block_mode = block_mode;
o->key_type = AES_KEYTYPE_NONE;
diff --git a/extmod/modhashlib.c b/extmod/modhashlib.c
index 263144a5c..dbebd7866 100644
--- a/extmod/modhashlib.c
+++ b/extmod/modhashlib.c
@@ -83,7 +83,7 @@ STATIC mp_obj_t hashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_sha256_context), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(mbedtls_sha256_context), type);
o->final = false;
mbedtls_sha256_init((mbedtls_sha256_context *)&o->state);
mbedtls_sha256_starts_ret((mbedtls_sha256_context *)&o->state, 0);
@@ -118,7 +118,7 @@ STATIC mp_obj_t hashlib_sha256_digest(mp_obj_t self_in) {
STATIC mp_obj_t hashlib_sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(CRYAL_SHA256_CTX), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(CRYAL_SHA256_CTX), type);
o->final = false;
sha256_init((CRYAL_SHA256_CTX *)o->state);
if (n_args == 1) {
@@ -172,7 +172,7 @@ STATIC mp_obj_t hashlib_sha1_update(mp_obj_t self_in, mp_obj_t arg);
#if MICROPY_SSL_AXTLS
STATIC mp_obj_t hashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(SHA1_CTX), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(SHA1_CTX), type);
o->final = false;
SHA1_Init((SHA1_CTX *)o->state);
if (n_args == 1) {
@@ -211,7 +211,7 @@ STATIC mp_obj_t hashlib_sha1_digest(mp_obj_t self_in) {
STATIC mp_obj_t hashlib_sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_sha1_context), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(mbedtls_sha1_context), type);
o->final = false;
mbedtls_sha1_init((mbedtls_sha1_context *)o->state);
mbedtls_sha1_starts_ret((mbedtls_sha1_context *)o->state);
@@ -266,7 +266,7 @@ STATIC mp_obj_t hashlib_md5_update(mp_obj_t self_in, mp_obj_t arg);
#if MICROPY_SSL_AXTLS
STATIC mp_obj_t hashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(MD5_CTX), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(MD5_CTX), type);
o->final = false;
MD5_Init((MD5_CTX *)o->state);
if (n_args == 1) {
@@ -305,7 +305,7 @@ STATIC mp_obj_t hashlib_md5_digest(mp_obj_t self_in) {
STATIC mp_obj_t hashlib_md5_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
- mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, char, sizeof(mbedtls_md5_context), type);
+ mp_obj_hash_t *o = mp_obj_malloc_var(mp_obj_hash_t, state, char, sizeof(mbedtls_md5_context), type);
o->final = false;
mbedtls_md5_init((mbedtls_md5_context *)o->state);
mbedtls_md5_starts_ret((mbedtls_md5_context *)o->state);
diff --git a/extmod/modre.c b/extmod/modre.c
index c24c07d09..9d5d71787 100644
--- a/extmod/modre.c
+++ b/extmod/modre.c
@@ -428,7 +428,7 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
if (size == -1) {
goto error;
}
- mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, (mp_obj_type_t *)&re_type);
+ mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, re.insts, char, size, (mp_obj_type_t *)&re_type);
#if MICROPY_PY_RE_DEBUG
int flags = 0;
if (n_args > 1) {
diff --git a/ports/nrf/boards/MICROBIT/modules/microbitimage.c b/ports/nrf/boards/MICROBIT/modules/microbitimage.c
index 08ef4babc..b3505c20a 100644
--- a/ports/nrf/boards/MICROBIT/modules/microbitimage.c
+++ b/ports/nrf/boards/MICROBIT/modules/microbitimage.c
@@ -113,7 +113,7 @@ mp_int_t imageHeight(microbit_image_obj_t * p_image) {
}
STATIC greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) {
- greyscale_t *result = mp_obj_malloc_var(greyscale_t, uint8_t, (w*h+1)>>1, &microbit_image_type);
+ greyscale_t *result = mp_obj_malloc_var(greyscale_t, byte_data, uint8_t, (w*h+1)>>1, &microbit_image_type);
result->five = 0;
result->width = w;
result->height = h;
diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c
index bc585f864..897fa07ac 100644
--- a/ports/unix/modffi.c
+++ b/ports/unix/modffi.c
@@ -227,7 +227,7 @@ STATIC mp_obj_t make_func(mp_obj_t rettype_in, void *func, mp_obj_t argtypes_in)
const char *argtypes = mp_obj_str_get_str(argtypes_in);
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(argtypes_in));
- mp_obj_ffifunc_t *o = mp_obj_malloc_var(mp_obj_ffifunc_t, ffi_type *, nparams, &ffifunc_type);
+ mp_obj_ffifunc_t *o = mp_obj_malloc_var(mp_obj_ffifunc_t, params, ffi_type *, nparams, &ffifunc_type);
o->func = func;
o->rettype = *rettype;
@@ -334,7 +334,7 @@ STATIC mp_obj_t mod_ffi_callback(size_t n_args, const mp_obj_t *pos_args, mp_map
const char *rettype = mp_obj_str_get_str(rettype_in);
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in));
- mp_obj_fficallback_t *o = mp_obj_malloc_var(mp_obj_fficallback_t, ffi_type *, nparams, &fficallback_type);
+ mp_obj_fficallback_t *o = mp_obj_malloc_var(mp_obj_fficallback_t, params, ffi_type *, nparams, &fficallback_type);
o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func);
diff --git a/py/modio.c b/py/modio.c
index 39317c52d..37eff3c41 100644
--- a/py/modio.c
+++ b/py/modio.c
@@ -119,7 +119,7 @@ typedef struct _mp_obj_bufwriter_t {
STATIC mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 2, false);
size_t alloc = mp_obj_get_int(args[1]);
- mp_obj_bufwriter_t *o = mp_obj_malloc_var(mp_obj_bufwriter_t, byte, alloc, type);
+ mp_obj_bufwriter_t *o = mp_obj_malloc_var(mp_obj_bufwriter_t, buf, byte, alloc, type);
o->stream = args[0];
o->alloc = alloc;
o->len = 0;
diff --git a/py/obj.h b/py/obj.h
index c7b7db0c3..5d49873ff 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -913,7 +913,7 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
// Helper versions of m_new_obj when you need to immediately set base.type.
// Implementing this as a call rather than inline saves 8 bytes per usage.
#define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
-#define mp_obj_malloc_var(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
+#define mp_obj_malloc_var(struct_type, var_field, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(offsetof(struct_type, var_field) + sizeof(var_type) * (var_num), obj_type))
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
// These macros are derived from more primitive ones and are used to
diff --git a/py/objattrtuple.c b/py/objattrtuple.c
index fbe04bedb..1ec949989 100644
--- a/py/objattrtuple.c
+++ b/py/objattrtuple.c
@@ -71,7 +71,7 @@ STATIC void mp_obj_attrtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
mp_obj_t mp_obj_new_attrtuple(const qstr *fields, size_t n, const mp_obj_t *items) {
- mp_obj_tuple_t *o = mp_obj_malloc_var(mp_obj_tuple_t, mp_obj_t, n + 1, &mp_type_attrtuple);
+ mp_obj_tuple_t *o = mp_obj_malloc_var(mp_obj_tuple_t, items, mp_obj_t, n + 1, &mp_type_attrtuple);
o->len = n;
for (size_t i = 0; i < n; i++) {
o->items[i] = items[i];
diff --git a/py/objclosure.c b/py/objclosure.c
index 6059d1810..88109cc91 100644
--- a/py/objclosure.c
+++ b/py/objclosure.c
@@ -105,7 +105,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
);
mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed_over, const mp_obj_t *closed) {
- mp_obj_closure_t *o = mp_obj_malloc_var(mp_obj_closure_t, mp_obj_t, n_closed_over, &mp_type_closure);
+ mp_obj_closure_t *o = mp_obj_malloc_var(mp_obj_closure_t, closed, mp_obj_t, n_closed_over, &mp_type_closure);
o->fun = fun;
o->n_closed = n_closed_over;
memcpy(o->closed, closed, n_closed_over * sizeof(mp_obj_t));
diff --git a/py/objfun.c b/py/objfun.c
index e2136968b..930d0ccdf 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -384,7 +384,7 @@ mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_
def_kw_args = def_args[1];
n_extra_args += 1;
}
- mp_obj_fun_bc_t *o = mp_obj_malloc_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args, &mp_type_fun_bc);
+ mp_obj_fun_bc_t *o = mp_obj_malloc_var(mp_obj_fun_bc_t, extra_args, mp_obj_t, n_extra_args, &mp_type_fun_bc);
o->bytecode = code;
o->context = context;
o->child_table = child_table;
diff --git a/py/objgenerator.c b/py/objgenerator.c
index ecd1f583e..7786b1fc8 100644
--- a/py/objgenerator.c
+++ b/py/objgenerator.c
@@ -59,7 +59,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons
MP_BC_PRELUDE_SIG_DECODE(ip);
// allocate the generator object, with room for local stack and exception stack
- mp_obj_gen_instance_t *o = mp_obj_malloc_var(mp_obj_gen_instance_t, byte,
+ mp_obj_gen_instance_t *o = mp_obj_malloc_var(mp_obj_gen_instance_t, code_state.state, byte,
n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t),
&mp_type_gen_instance);
@@ -114,7 +114,7 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k
MP_BC_PRELUDE_SIG_DECODE(ip);
// Allocate the generator object, with room for local stack (exception stack not needed).
- mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, byte, n_state * sizeof(mp_obj_t), &mp_type_gen_instance);
+ mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, code_state.state, byte, n_state * sizeof(mp_obj_t), &mp_type_gen_instance);
// Parse the input arguments and set up the code state
o->pend_exc = mp_const_none;
diff --git a/py/objmap.c b/py/objmap.c
index e7e594cd2..98ff19bb7 100644
--- a/py/objmap.c
+++ b/py/objmap.c
@@ -38,7 +38,7 @@ typedef struct _mp_obj_map_t {
STATIC mp_obj_t map_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, MP_OBJ_FUN_ARGS_MAX, false);
- mp_obj_map_t *o = mp_obj_malloc_var(mp_obj_map_t, mp_obj_t, n_args - 1, type);
+ mp_obj_map_t *o = mp_obj_malloc_var(mp_obj_map_t, iters, mp_obj_t, n_args - 1, type);
o->n_iters = n_args - 1;
o->fun = args[0];
for (size_t i = 0; i < n_args - 1; i++) {
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c
index e586f0894..0adf83fe9 100644
--- a/py/objnamedtuple.c
+++ b/py/objnamedtuple.c
@@ -110,7 +110,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
// Create a namedtuple with explicit malloc. Calling mp_obj_new_tuple
// with num_fields=0 returns a read-only object.
- mp_obj_tuple_t *tuple = mp_obj_malloc_var(mp_obj_tuple_t, mp_obj_t, num_fields, type_in);
+ mp_obj_tuple_t *tuple = mp_obj_malloc_var(mp_obj_tuple_t, items, mp_obj_t, num_fields, type_in);
tuple->len = num_fields;
// Copy the positional args into the first slots of the namedtuple
diff --git a/py/objtuple.c b/py/objtuple.c
index 969c488ee..cb1d7cd38 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -244,7 +244,7 @@ mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items) {
if (n == 0) {
return mp_const_empty_tuple;
}
- mp_obj_tuple_t *o = mp_obj_malloc_var(mp_obj_tuple_t, mp_obj_t, n, &mp_type_tuple);
+ mp_obj_tuple_t *o = mp_obj_malloc_var(mp_obj_tuple_t, items, mp_obj_t, n, &mp_type_tuple);
o->len = n;
if (items) {
for (size_t i = 0; i < n; i++) {
diff --git a/py/objtype.c b/py/objtype.c
index d21c2a4b5..694db3008 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -99,7 +99,7 @@ STATIC
mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_type_t **native_base) {
size_t num_native_bases = instance_count_native_bases(class, native_base);
assert(num_native_bases < 2);
- mp_obj_instance_t *o = mp_obj_malloc_var(mp_obj_instance_t, mp_obj_t, num_native_bases, class);
+ mp_obj_instance_t *o = mp_obj_malloc_var(mp_obj_instance_t, subobj, mp_obj_t, num_native_bases, class);
mp_map_init(&o->members, 0);
// Initialise the native base-class slot (should be 1 at most) with a valid
// object. It doesn't matter which object, so long as it can be uniquely
diff --git a/py/objzip.c b/py/objzip.c
index 3c3138180..7b4ae7548 100644
--- a/py/objzip.c
+++ b/py/objzip.c
@@ -39,7 +39,7 @@ typedef struct _mp_obj_zip_t {
STATIC mp_obj_t zip_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
- mp_obj_zip_t *o = mp_obj_malloc_var(mp_obj_zip_t, mp_obj_t, n_args, type);
+ mp_obj_zip_t *o = mp_obj_malloc_var(mp_obj_zip_t, iters, mp_obj_t, n_args, type);
o->n_iters = n_args;
for (size_t i = 0; i < n_args; i++) {
o->iters[i] = mp_getiter(args[i], NULL);