Skip to content

Commit 6d3c5e4

Browse files
committed
Add ARRAY_SIZE macro, and use it where possible.
1 parent d139c48 commit 6d3c5e4

28 files changed

Lines changed: 63 additions & 59 deletions

py/builtin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
128128

129129
STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
130130
mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
131-
return mp_obj_str_format(sizeof(args) / sizeof(args[0]), args);
131+
return mp_obj_str_format(ARRAY_SIZE(args), args);
132132
}
133133

134134
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);

py/builtintables.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ const mp_obj_dict_t mp_builtin_object_dict_obj = {
118118
.map = {
119119
.all_keys_are_qstrs = 1,
120120
.table_is_fixed_array = 1,
121-
.used = sizeof(mp_builtin_object_table) / sizeof(mp_map_elem_t),
122-
.alloc = sizeof(mp_builtin_object_table) / sizeof(mp_map_elem_t),
121+
.used = ARRAY_SIZE(mp_builtin_object_table),
122+
.alloc = ARRAY_SIZE(mp_builtin_object_table),
123123
.table = (mp_map_elem_t*)mp_builtin_object_table,
124124
},
125125
};
@@ -158,8 +158,8 @@ const mp_obj_dict_t mp_builtin_module_dict_obj = {
158158
.map = {
159159
.all_keys_are_qstrs = 1,
160160
.table_is_fixed_array = 1,
161-
.used = sizeof(mp_builtin_module_table) / sizeof(mp_map_elem_t),
162-
.alloc = sizeof(mp_builtin_module_table) / sizeof(mp_map_elem_t),
161+
.used = ARRAY_SIZE(mp_builtin_module_table),
162+
.alloc = ARRAY_SIZE(mp_builtin_module_table),
163163
.table = (mp_map_elem_t*)mp_builtin_module_table,
164164
},
165165
};

py/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ STATIC const mp_map_elem_t mp_constants_table[] = {
8686
STATIC const mp_map_t mp_constants_map = {
8787
.all_keys_are_qstrs = 1,
8888
.table_is_fixed_array = 1,
89-
.used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
90-
.alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
89+
.used = ARRAY_SIZE(mp_constants_table),
90+
.alloc = ARRAY_SIZE(mp_constants_table),
9191
.table = (mp_map_elem_t*)mp_constants_table,
9292
};
9393

py/emitinlinethumb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ STATIC uint get_arg_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t
141141
if (MP_PARSE_NODE_IS_ID(pn)) {
142142
qstr reg_qstr = MP_PARSE_NODE_LEAF_ARG(pn);
143143
const char *reg_str = qstr_str(reg_qstr);
144-
for (uint i = 0; i < sizeof(reg_name_table) / sizeof(reg_name_table[0]); i++) {
144+
for (uint i = 0; i < ARRAY_SIZE(reg_name_table); i++) {
145145
const reg_name_t *r = &reg_name_table[i];
146146
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')) {
147147
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
260260
asm_thumb_b_n(emit->as, label_num);
261261
} else if (op_str[0] == 'b' && op_len == 3) {
262262
uint cc = -1;
263-
for (uint i = 0; i < (sizeof cc_name_table) / (sizeof cc_name_table[0]); i++) {
263+
for (uint i = 0; i < ARRAY_SIZE(cc_name_table); i++) {
264264
if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) {
265265
cc = cc_name_table[i].cc;
266266
}

py/misc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ int m_get_total_bytes_allocated(void);
5252
int m_get_current_bytes_allocated(void);
5353
int m_get_peak_bytes_allocated(void);
5454

55+
/** array helpers ***********************************************/
56+
57+
// get the number of elements in a fixed-size array
58+
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
59+
5560
/** unichar / UTF-8 *********************************************/
5661

5762
typedef int unichar; // TODO

py/modarray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ STATIC const mp_obj_dict_t mp_module_array_globals = {
1414
.map = {
1515
.all_keys_are_qstrs = 1,
1616
.table_is_fixed_array = 1,
17-
.used = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
18-
.alloc = sizeof(mp_module_array_globals_table) / sizeof(mp_map_elem_t),
17+
.used = ARRAY_SIZE(mp_module_array_globals_table),
18+
.alloc = ARRAY_SIZE(mp_module_array_globals_table),
1919
.table = (mp_map_elem_t*)mp_module_array_globals_table,
2020
},
2121
};

py/modcmath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ STATIC const mp_obj_dict_t mp_module_cmath_globals = {
116116
.map = {
117117
.all_keys_are_qstrs = 1,
118118
.table_is_fixed_array = 1,
119-
.used = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
120-
.alloc = sizeof(mp_module_cmath_globals_table) / sizeof(mp_map_elem_t),
119+
.used = ARRAY_SIZE(mp_module_cmath_globals_table),
120+
.alloc = ARRAY_SIZE(mp_module_cmath_globals_table),
121121
.table = (mp_map_elem_t*)mp_module_cmath_globals_table,
122122
},
123123
};

py/modcollections.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ STATIC const mp_obj_dict_t mp_module_collections_globals = {
1616
.map = {
1717
.all_keys_are_qstrs = 1,
1818
.table_is_fixed_array = 1,
19-
.used = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
20-
.alloc = sizeof(mp_module_collections_globals_table) / sizeof(mp_map_elem_t),
19+
.used = ARRAY_SIZE(mp_module_collections_globals_table),
20+
.alloc = ARRAY_SIZE(mp_module_collections_globals_table),
2121
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
2222
},
2323
};

py/modio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ STATIC const mp_obj_dict_t mp_module_io_globals = {
1818
.map = {
1919
.all_keys_are_qstrs = 1,
2020
.table_is_fixed_array = 1,
21-
.used = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
22-
.alloc = sizeof(mp_module_io_globals_table) / sizeof(mp_map_elem_t),
21+
.used = ARRAY_SIZE(mp_module_io_globals_table),
22+
.alloc = ARRAY_SIZE(mp_module_io_globals_table),
2323
.table = (mp_map_elem_t*)mp_module_io_globals_table,
2424
},
2525
};

py/modmath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ STATIC const mp_obj_dict_t mp_module_math_globals = {
145145
.map = {
146146
.all_keys_are_qstrs = 1,
147147
.table_is_fixed_array = 1,
148-
.used = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
149-
.alloc = sizeof(mp_module_math_globals_table) / sizeof(mp_map_elem_t),
148+
.used = ARRAY_SIZE(mp_module_math_globals_table),
149+
.alloc = ARRAY_SIZE(mp_module_math_globals_table),
150150
.table = (mp_map_elem_t*)mp_module_math_globals_table,
151151
},
152152
};

0 commit comments

Comments
 (0)