summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c2
-rw-r--r--py/builtintables.c8
-rw-r--r--py/compile.c4
-rw-r--r--py/emitinlinethumb.c4
-rw-r--r--py/misc.h5
-rw-r--r--py/modarray.c4
-rw-r--r--py/modcmath.c4
-rw-r--r--py/modcollections.c4
-rw-r--r--py/modio.c4
-rw-r--r--py/modmath.c4
-rw-r--r--py/modmicropython.c4
-rw-r--r--py/modstruct.c4
-rw-r--r--py/modsys.c4
13 files changed, 30 insertions, 25 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 58a86ac9b5..250a3558e8 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -128,7 +128,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
- return mp_obj_str_format(sizeof(args) / sizeof(args[0]), args);
+ return mp_obj_str_format(ARRAY_SIZE(args), args);
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
diff --git a/py/builtintables.c b/py/builtintables.c
index e6461ad80b..ab60183c66 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -118,8 +118,8 @@ const mp_obj_dict_t mp_builtin_object_dict_obj = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_builtin_object_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_builtin_object_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_builtin_object_table),
+ .alloc = ARRAY_SIZE(mp_builtin_object_table),
.table = (mp_map_elem_t*)mp_builtin_object_table,
},
};
@@ -158,8 +158,8 @@ const mp_obj_dict_t mp_builtin_module_dict_obj = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_builtin_module_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_builtin_module_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_builtin_module_table),
+ .alloc = ARRAY_SIZE(mp_builtin_module_table),
.table = (mp_map_elem_t*)mp_builtin_module_table,
},
};
diff --git a/py/compile.c b/py/compile.c
index ee735a872f..532e29a233 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -86,8 +86,8 @@ STATIC const mp_map_elem_t mp_constants_table[] = {
STATIC const mp_map_t mp_constants_map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_constants_table),
+ .alloc = ARRAY_SIZE(mp_constants_table),
.table = (mp_map_elem_t*)mp_constants_table,
};
diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c
index 1ed5526631..8b0e6af5e5 100644
--- a/py/emitinlinethumb.c
+++ b/py/emitinlinethumb.c
@@ -141,7 +141,7 @@ STATIC uint get_arg_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t
if (MP_PARSE_NODE_IS_ID(pn)) {
qstr reg_qstr = MP_PARSE_NODE_LEAF_ARG(pn);
const char *reg_str = qstr_str(reg_qstr);
- for (uint i = 0; i < sizeof(reg_name_table) / sizeof(reg_name_table[0]); i++) {
+ for (uint i = 0; i < ARRAY_SIZE(reg_name_table); i++) {
const reg_name_t *r = &reg_name_table[i];
if (reg_str[0] == r->name[0] && reg_str[1] == r->name[1] && reg_str[2] == r->name[2] && (reg_str[2] == '\0' || reg_str[3] == '\0')) {
if (r->reg > max_reg) {
@@ -260,7 +260,7 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, m
asm_thumb_b_n(emit->as, label_num);
} else if (op_str[0] == 'b' && op_len == 3) {
uint cc = -1;
- for (uint i = 0; i < (sizeof cc_name_table) / (sizeof cc_name_table[0]); i++) {
+ for (uint i = 0; i < ARRAY_SIZE(cc_name_table); i++) {
if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) {
cc = cc_name_table[i].cc;
}
diff --git a/py/misc.h b/py/misc.h
index 002a97ffec..bf31f75354 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -52,6 +52,11 @@ int m_get_total_bytes_allocated(void);
int m_get_current_bytes_allocated(void);
int m_get_peak_bytes_allocated(void);
+/** array helpers ***********************************************/
+
+// get the number of elements in a fixed-size array
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
/** unichar / UTF-8 *********************************************/
typedef int unichar; // TODO
diff --git a/py/modarray.c b/py/modarray.c
index d8ff7158ff..ff668f5b87 100644
--- a/py/modarray.c
+++ b/py/modarray.c
@@ -14,8 +14,8 @@ STATIC const mp_obj_dict_t mp_module_array_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_array_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_array_globals_table),
.table = (mp_map_elem_t*)mp_module_array_globals_table,
},
};
diff --git a/py/modcmath.c b/py/modcmath.c
index 3f561e7f30..80dc0c8860 100644
--- a/py/modcmath.c
+++ b/py/modcmath.c
@@ -116,8 +116,8 @@ STATIC const mp_obj_dict_t mp_module_cmath_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_cmath_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_cmath_globals_table),
.table = (mp_map_elem_t*)mp_module_cmath_globals_table,
},
};
diff --git a/py/modcollections.c b/py/modcollections.c
index 5278a526fb..12d8f486c6 100644
--- a/py/modcollections.c
+++ b/py/modcollections.c
@@ -16,8 +16,8 @@ STATIC const mp_obj_dict_t mp_module_collections_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_collections_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_collections_globals_table),
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
},
};
diff --git a/py/modio.c b/py/modio.c
index ac5c7df9de..d07dc6a982 100644
--- a/py/modio.c
+++ b/py/modio.c
@@ -18,8 +18,8 @@ STATIC const mp_obj_dict_t mp_module_io_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_io_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_io_globals_table),
.table = (mp_map_elem_t*)mp_module_io_globals_table,
},
};
diff --git a/py/modmath.c b/py/modmath.c
index 09bb4ce3aa..2c5b0e6faf 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -145,8 +145,8 @@ STATIC const mp_obj_dict_t mp_module_math_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_math_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_math_globals_table),
.table = (mp_map_elem_t*)mp_module_math_globals_table,
},
};
diff --git a/py/modmicropython.c b/py/modmicropython.c
index ed245969cf..b8ddbfe2a1 100644
--- a/py/modmicropython.c
+++ b/py/modmicropython.c
@@ -39,8 +39,8 @@ STATIC const mp_obj_dict_t mp_module_micropython_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_micropython_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_micropython_globals_table),
.table = (mp_map_elem_t*)mp_module_micropython_globals_table,
},
};
diff --git a/py/modstruct.c b/py/modstruct.c
index 81afd94d16..2a3c3c0ad6 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -98,8 +98,8 @@ STATIC const mp_obj_dict_t mp_module_struct_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_struct_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_struct_globals_table),
.table = (mp_map_elem_t*)mp_module_struct_globals_table,
},
};
diff --git a/py/modsys.c b/py/modsys.c
index 41ade1f275..ab067e0662 100644
--- a/py/modsys.c
+++ b/py/modsys.c
@@ -49,8 +49,8 @@ STATIC const mp_obj_dict_t mp_module_sys_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
- .used = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
- .alloc = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
+ .used = ARRAY_SIZE(mp_module_sys_globals_table),
+ .alloc = ARRAY_SIZE(mp_module_sys_globals_table),
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
},
};