8000 Add ARRAY_SIZE macro, and use it where possible. · lurch/micropython@6d3c5e4 · GitHub
[go: up one dir, main page]

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

+63
-59
lines changed
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
};

py/modmicropython.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ STATIC const mp_obj_dict_t mp_module_micropython_globals = {
3939
.map = {
4040
.all_keys_are_qstrs = 1,
4141
.table_is_fixed_array = 1,
42-
.used = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
43-
.alloc = sizeof(mp_module_micropython_globals_table) / sizeof(mp_map_elem_t),
42+
.used = ARRAY_SIZE(mp_module_micropython_globals_table),
43+
.alloc = ARRAY_SIZE(mp_module_micropython_globals_table),
4444
.table = (mp_map_elem_t*)mp_module_micropython_globals_table,
4545
},
4646
};

py/modstruct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ STATIC const mp_obj_dict_t mp_module_struct_globals = {
9898
.map = {
9999
.all_keys_are_qstrs = 1,
100100
.table_is_fixed_array = 1,
101-
.used = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
102-
.alloc = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
101+
.used = ARRAY_SIZE(mp_module_struct_globals_table),
102+
.alloc = ARRAY_SIZE(mp_module_struct_globals_table),
103103
.table = (mp_map_elem_t*)mp_module_struct_globals_table,
104104
},
105105
};

py/modsys.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ STATIC const mp_obj_dict_t mp_module_sys_globals = {
4949
.map = {
5050
.all_keys_are_qstrs = 1,
5151
.table_is_fixed_array = 1,
52-
.used = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
53-
.alloc = sizeof(mp_module_sys_globals_table) / sizeof(mp_map_elem_t),
52+
.used = ARRAY_SIZE(mp_module_sys_globals_table),
53+
.alloc = ARRAY_SIZE(mp_module_sys_globals_table),
5454
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
5555
},
5656
};

stmhal/dac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ STATIC const mp_arg_parse_t pyb_dac_write_timed_accepted_args[] = {
167167
{ MP_QSTR_freq, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_INT, {.u_int = 0} },
168168
{ MP_QSTR_mode, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = DMA_NORMAL} },
169169
};
170-
#define PYB_DAC_WRITE_TIMED_NUM_ARGS (sizeof(pyb_dac_write_timed_accepted_args) / sizeof(pyb_dac_write_timed_accepted_args[0]))
170+
#define PYB_DAC_WRITE_TIMED_NUM_ARGS ARRAY_SIZE(pyb_dac_write_timed_accepted_args)
171171

172172
mp_obj_t pyb_dac_write_timed(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
173173
pyb_dac_obj_t *self = args[0];

stmhal/extint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ STATIC const mp_arg_parse_t pyb_extint_make_new_accepted_args[] = {
245245
{ MP_QSTR_pull, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_INT, {.u_int = 0} },
246246
{ MP_QSTR_callback, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
247247
};
248-
#define PYB_EXTINT_MAKE_NEW_NUM_ARGS (sizeof(pyb_extint_make_new_accepted_args) / sizeof(pyb_extint_make_new_accepted_args[0]))
248+
#define PYB_EXTINT_MAKE_NEW_NUM_ARGS ARRAY_SIZE(pyb_extint_make_new_accepted_args)
249249

250250
STATIC mp_obj_t extint_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
251251
// type_in == extint_obj_type

stmhal/i2c.c

Lines changed: 5 additions & 6 deletions
< 10000 /tr>
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ STATIC const pyb_i2c_obj_t pyb_i2c_obj[] = {
157157
#endif
158158
{{&pyb_i2c_type}, &I2CHandle2}
159159
};
160-
#define PYB_NUM_I2C (sizeof(pyb_i2c_obj) / sizeof(pyb_i2c_obj[0]))
161160

162161
STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
163162
pyb_i2c_obj_t *self = self_in;
@@ -183,7 +182,7 @@ STATIC const mp_arg_parse_t pyb_i2c_init_accepted_args[] = {
183182
{ MP_QSTR_baudrate, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 400000} },
184183
{ MP_QSTR_gencall, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_BOOL, {.u_bool = false} },
185184
};
186-
#define PYB_I2C_INIT_NUM_ARGS (sizeof(pyb_i2c_init_accepted_args) / sizeof(pyb_i2c_init_accepted_args[0]))
185+
#define PYB_I2C_INIT_NUM_ARGS ARRAY_SIZE(pyb_i2c_init_accepted_args)
187186

188187
STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
189188
// parse args
@@ -222,7 +221,7 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
222221
machine_int_t i2c_id = mp_obj_get_int(args[0]) - 1;
223222

224223
// check i2c number
225-
if (!(0 <= i2c_id && i2c_id < PYB_NUM_I2C && pyb_i2c_obj[i2c_id].i2c != NULL)) {
224+
if (!(0 <= i2c_id && i2c_id < ARRAY_SIZE(pyb_i2c_obj) && pyb_i2c_obj[i2c_id].i2c != NULL)) {
226225
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C bus %d does not exist", i2c_id + 1));
227226
}
228227

@@ -301,7 +300,7 @@ STATIC const mp_arg_parse_t pyb_i2c_send_accepted_args[] = {
301300
{ MP_QSTR_addr, MP_ARG_PARSE_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
302301
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
303302
};
304-
#define PYB_I2C_SEND_NUM_ARGS (sizeof(pyb_i2c_send_accepted_args) / sizeof(pyb_i2c_send_accepted_args[0]))
303+
#define PYB_I2C_SEND_NUM_ARGS ARRAY_SIZE(pyb_i2c_send_accepted_args)
305304

306305
STATIC mp_obj_t pyb_i2c_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
307306
pyb_i2c_obj_t *self = args[0];
@@ -341,7 +340,7 @@ STATIC const mp_arg_parse_t pyb_i2c_recv_accepted_args[] = {
341340
{ MP_QSTR_addr, MP_ARG_PARSE_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
342341
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
343342
};
344-
#define PYB_I2C_RECV_NUM_ARGS (sizeof(pyb_i2c_recv_accepted_args) / sizeof(pyb_i2c_recv_accepted_args[0]))
343+
#define PYB_I2C_RECV_NUM_ARGS ARRAY_SIZE(pyb_i2c_recv_accepted_args)
345344

346345
STATIC mp_obj_t pyb_i2c_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
347346
pyb_i2c_obj_t *self = args[0];
@@ -386,7 +385,7 @@ STATIC const mp_arg_parse_t pyb_i2c_mem_read_accepted_args[] = {
386385
{ MP_QSTR_memaddr, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_INT, {.u_int = 0} },
387386
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
388387
};
389-
#define PYB_I2C_MEM_READ_NUM_ARGS (sizeof(pyb_i2c_mem_read_accepted_args) / sizeof(pyb_i2c_mem_read_accepted_args[0]))
388+
#define PYB_I2C_MEM_READ_NUM_ARGS ARRAY_SIZE(pyb_i2c_mem_read_accepted_args)
390389

391390
STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
392391
pyb_i2c_obj_t *self = args[0];

stmhal/led.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ STATIC const pyb_led_obj_t pyb_led_obj[] = {
3030
#endif
3131
#endif
3232
};
33-
#define NUM_LEDS (sizeof(pyb_led_obj) / sizeof(pyb_led_obj[0]))
33+
#define NUM_LEDS ARRAY_SIZE(pyb_led_obj)
3434

3535
void led_init(void) {
3636
/* GPIO structure */

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ int main(void) {
279279
MP_OBJ_NEW_SMALL_INT(115200),
280280
};
281281
pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type,
282-
sizeof(args) / sizeof(args[0]),
282+
ARRAY_SIZE(args),
283283
0, args);
284284
}
285285
#else

stmhal/modos.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ STATIC const mp_obj_dict_t os_module_globals = {
167167
.map = {
168168
.all_keys_are_qstrs = 1,
169169
.table_is_fixed_array = 1,
170-
.used = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
171-
.alloc = sizeof(os_module_globals_table) / sizeof(mp_map_elem_t),
170+
.used = ARRAY_SIZE(os_module_globals_table),
171+
.alloc = ARRAY_SIZE(os_module_globals_table),
172172
.table = (mp_map_elem_t*)os_module_globals_table,
173173
},
174174
};

stmhal/modpyb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ STATIC const mp_obj_dict_t pyb_module_globals = {
324324
.map = {
325325
.all_keys_are_qstrs = 1,
326326
.table_is_fixed_array = 1,
327-
.used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
328-
.alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
327+
.used = ARRAY_SIZE(pyb_module_globals_table),
328+
.alloc = ARRAY_SIZE(pyb_module_globals_table),
329329
.table = (mp_map_elem_t*)pyb_module_globals_table,
330330
},
331331
};

stmhal/modstm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ STATIC const mp_obj_dict_t stm_module_globals = {
105105
.map = {
106106
.all_keys_are_qstrs = 1,
107107
.table_is_fixed_array = 1,
108-
.used = sizeof(stm_module_globals_table) / sizeof(mp_map_elem_t),
109-
.alloc = sizeof(stm_module_globals_table) / sizeof(mp_map_elem_t),
108+
.used = ARRAY_SIZE(stm_module_globals_table),
109+
.alloc = ARRAY_SIZE(stm_module_globals_table),
110110
.table = (mp_map_elem_t*)stm_module_globals_table,
111111
},
112112
};

stmhal/modtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ STATIC const mp_obj_dict_t time_module_globals = {
3333
.map = {
3434
.all_keys_are_qstrs = 1,
3535
.table_is_fixed_array = 1,
36-
.used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
37-
.alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
36+
.used = ARRAY_SIZE(time_module_globals_table),
37+
.alloc = ARRAY_SIZE(time_module_globals_table),
3838
.table = (mp_map_elem_t*)time_module_globals_table,
3939
},
4040
};

stmhal/spi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ STATIC const pyb_spi_obj_t pyb_spi_obj[] = {
158158
{{&pyb_spi_type}, NULL},
159159
#endif
160160
};
161-
#define PYB_NUM_SPI (sizeof(pyb_spi_obj) / sizeof(pyb_spi_obj[0]))
161+
#define PYB_NUM_SPI ARRAY_SIZE(pyb_spi_obj)
162162

163163
STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
164164
pyb_spi_obj_t *self = self_in;
@@ -206,7 +206,7 @@ STATIC const mp_arg_parse_t pyb_spi_init_accepted_args[] = {
206206
{ MP_QSTR_ti, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_BOOL, {.u_bool = false} },
207207
{ MP_QSTR_crc, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_OBJ, {.u_obj = mp_const_none} },
208208
};
209-
#define PYB_SPI_INIT_NUM_ARGS (sizeof(pyb_spi_init_accepted_args) / sizeof(pyb_spi_init_accepted_args[0]))
209+
#define PYB_SPI_INIT_NUM_ARGS ARRAY_SIZE(pyb_spi_init_accepted_args)
210210

211211
STATIC mp_obj_t pyb_spi_init_helper(const pyb_spi_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
212212
// parse args
@@ -299,7 +299,7 @@ STATIC const mp_arg_parse_t pyb_spi_send_accepted_args[] = {
299299
{ MP_QSTR_send, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
300300
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
301301
};
302-
#define PYB_SPI_SEND_NUM_ARGS (sizeof(pyb_spi_send_accepted_args) / sizeof(pyb_spi_send_accepted_args[0]))
302+
#define PYB_SPI_SEND_NUM_ARGS ARRAY_SIZE(pyb_spi_send_accepted_args)
303303

304304
STATIC mp_obj_t pyb_spi_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
305305
// TODO assumes transmission size is 8-bits wide
@@ -331,7 +331,7 @@ STATIC const mp_arg_parse_t pyb_spi_recv_accepted_args[] = {
331331
{ MP_QSTR_recv, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
332332
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
333333
};
334-
#define PYB_SPI_RECV_NUM_ARGS (sizeof(pyb_spi_recv_accepted_args) / sizeof(pyb_spi_recv_accepted_args[0]))
334+
#define PYB_SPI_RECV_NUM_ARGS ARRAY_SIZE(pyb_spi_recv_accepted_args)
335335

336336
STATIC mp_obj_t pyb_spi_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
337337
// TODO assumes transmission size is 8-bits wide
@@ -368,7 +368,7 @@ STATIC const mp_arg_parse_t pyb_spi_send_recv_accepted_args[] = {
368368
{ MP_QSTR_recv, MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
369369
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
370370
};
371-
#define PYB_SPI_SEND_RECV_NUM_ARGS (sizeof(pyb_spi_send_recv_accepted_args) / sizeof(pyb_spi_send_recv_accepted_args[0]))
371+
#define PYB_SPI_SEND_RECV_NUM_ARGS ARRAY_SIZE(pyb_spi_send_recv_accepted_args)
372372

373373
STATIC mp_obj_t pyb_spi_send_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
374374
// TODO assumes transmission size is 8-bits wide

stmhal/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static uint32_t tim3_counter = 0;
5757

5858
// Used to do callbacks to Python code on interrupt
5959
STATIC pyb_timer_obj_t *pyb_timer_obj_all[14];
60-
#define PYB_TIMER_OBJ_ALL_NUM (sizeof(pyb_timer_obj_all) / sizeof(pyb_timer_obj_all[0]))
60+
#define PYB_TIMER_OBJ_ALL_NUM ARRAY_SIZE(pyb_timer_obj_all)
6161

6262
void timer_init0(void) {
6363
tim3_counter = 0;
@@ -180,7 +180,7 @@ STATIC const mp_arg_parse_t pyb_timer_init_accepted_args[] = {
180180
{ MP_QSTR_mode, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = TIM_COUNTERMODE_UP} },
181181
{ MP_QSTR_div, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = TIM_CLOCKDIVISION_DIV1} },
182182
};
183-
#define PYB_TIMER_INIT_NUM_ARGS (sizeof(pyb_timer_init_accepted_args) / sizeof(pyb_timer_init_accepted_args[0]))
183+
#define PYB_TIMER_INIT_NUM_ARGS ARRAY_SIZE(pyb_timer_init_accepted_args)
184184

185185
STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
186186
// parse args

stmhal/uart.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ STATIC const mp_arg_parse_t pyb_uart_init_accepted_args[] = {
231231
{ MP_QSTR_stop, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 1} },
232232
{ MP_QSTR_parity, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_OBJ, {.u_obj = mp_const_none} },
233233
};
234-
#define PYB_UART_INIT_NUM_ARGS (sizeof(pyb_uart_init_accepted_args) / sizeof(pyb_uart_init_accepted_args[0]))
234+
#define PYB_UART_INIT_NUM_ARGS ARRAY_SIZE(pyb_uart_init_accepted_args)
235235

236236
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
237237
// parse args
@@ -331,7 +331,7 @@ STATIC const mp_arg_parse_t pyb_uart_send_accepted_args[] = {
331331
{ MP_QSTR_send, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
332332
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
333333
};
334-
#define PYB_UART_SEND_NUM_ARGS (sizeof(pyb_uart_send_accepted_args) / sizeof(pyb_uart_send_accepted_args[0]))
334+
#define PYB_UART_SEND_NUM_ARGS ARRAY_SIZE(pyb_uart_send_accepted_args)
335335

336336
STATIC mp_obj_t pyb_uart_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
337337
// TODO assumes transmission size is 8-bits wide
@@ -363,7 +363,7 @@ STATIC const mp_arg_parse_t pyb_uart_recv_accepted_args[] = {
363363
{ MP_QSTR_recv, MP_ARG_PARSE_REQUIRED | MP_ARG_PARSE_OBJ, {.u_obj = MP_OBJ_NULL} },
364364
{ MP_QSTR_timeout, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 5000} },
365365
};
366-
#define PYB_UART_RECV_NUM_ARGS (sizeof(pyb_uart_recv_accepted_args) / sizeof(pyb_uart_recv_accepted_args[0]))
366+
#define PYB_UART_RECV_NUM_ARGS ARRAY_SIZE(pyb_uart_recv_accepted_args)
367367

368368
STATIC mp_obj_t pyb_uart_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
369369
// TODO assumes transmission size is 8-bits wide

0 commit comments

Comments
 (0)
0