Add ARRAY_SIZE macro, and use it where possible.
diff --git a/py/builtin.c b/py/builtin.c
index 58a86ac..250a355 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -128,7 +128,7 @@
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 e6461ad..ab60183 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -118,8 +118,8 @@
.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 @@
.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 ee735a8..532e29a 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -86,8 +86,8 @@
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 1ed5526..8b0e6af 100644
--- a/py/emitinlinethumb.c
+++ b/py/emitinlinethumb.c
@@ -141,7 +141,7 @@
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 = ®_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 @@
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 002a97f..bf31f75 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -52,6 +52,11 @@
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 d8ff715..ff668f5 100644
--- a/py/modarray.c
+++ b/py/modarray.c
@@ -14,8 +14,8 @@
.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 3f561e7..80dc0c8 100644
--- a/py/modcmath.c
+++ b/py/modcmath.c
@@ -116,8 +116,8 @@
.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 5278a52..12d8f48 100644
--- a/py/modcollections.c
+++ b/py/modcollections.c
@@ -16,8 +16,8 @@
.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 ac5c7df..d07dc6a 100644
--- a/py/modio.c
+++ b/py/modio.c
@@ -18,8 +18,8 @@
.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 09bb4ce..2c5b0e6 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -145,8 +145,8 @@
.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 ed24596..b8ddbfe 100644
--- a/py/modmicropython.c
+++ b/py/modmicropython.c
@@ -39,8 +39,8 @@
.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 81afd94..2a3c3c0 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -98,8 +98,8 @@
.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 41ade1f..ab067e0 100644
--- a/py/modsys.c
+++ b/py/modsys.c
@@ -49,8 +49,8 @@
.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,
},
};