all: Remove the "STATIC" macro and just use "static" instead.
The STATIC macro was introduced a very long time ago in commit
d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was
to have the option to define it to nothing so that all static functions
become global functions and therefore visible to certain debug tools, so
one could do function size comparison and other things.
This STATIC feature is rarely (if ever) used. And with the use of LTO and
heavy inline optimisation, analysing the size of individual functions when
they are not static is not a good representation of the size of code when
fully optimised.
So the macro does not have much use and it's simpler to just remove it.
Then you know exactly what it's doing. For example, newcomers don't have
to learn what the STATIC macro is and why it exists. Reading the code is
also less "loud" with a lowercase static.
One other minor point in favour of removing it, is that it stops bugs with
`STATIC inline`, which should always be `static inline`.
Methodology for this commit was:
1) git ls-files | egrep '\.[ch]$' | \
xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/"
2) Do some manual cleanup in the diff by searching for the word STATIC in
comments and changing those back.
3) "git-grep STATIC docs/", manually fixed those cases.
4) "rg -t python STATIC", manually fixed codegen lines that used STATIC.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
diff --git a/ports/esp32/modespnow.c b/ports/esp32/modespnow.c
index 77dc970..e7e51ee 100644
--- a/ports/esp32/modespnow.c
+++ b/ports/esp32/modespnow.c
@@ -149,7 +149,7 @@
// Allocate and initialise the ESPNow module as a singleton.
// Returns the initialised espnow_singleton.
-STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args,
+static mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args,
size_t n_kw, const mp_obj_t *all_args) {
// The espnow_singleton must be defined in MICROPY_PORT_ROOT_POINTERS
@@ -180,9 +180,9 @@
}
// Forward declare the send and recv ESPNow callbacks
-STATIC void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
+static void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
-STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len);
+static void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len);
// ESPNow.init(): Initialise the data buffers and ESP-NOW functions.
// Initialise the Espressif ESPNOW software stack, register callbacks and
@@ -205,7 +205,7 @@
// ESPNow.deinit(): De-initialise the ESPNOW software stack, disable callbacks
// and deallocate the recv data buffers.
// Note: this function is called from main.c:mp_task() to cleanup before soft
-// reset, so cannot be declared STATIC and must guard against self == NULL;.
+// reset, so cannot be declared static and must guard against self == NULL;.
mp_obj_t espnow_deinit(mp_obj_t _) {
esp_espnow_obj_t *self = _get_singleton();
if (self != NULL && self->recv_buffer != NULL) {
@@ -220,7 +220,7 @@
return mp_const_none;
}
-STATIC mp_obj_t espnow_active(size_t n_args, const mp_obj_t *args) {
+static mp_obj_t espnow_active(size_t n_args, const mp_obj_t *args) {
esp_espnow_obj_t *self = _get_singleton();
if (n_args > 1) {
if (mp_obj_is_true(args[1])) {
@@ -231,13 +231,13 @@
}
return self->recv_buffer != NULL ? mp_const_true : mp_const_false;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_active_obj, 1, 2, espnow_active);
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_active_obj, 1, 2, espnow_active);
// ESPNow.config(['param'|param=value, ..])
// Get or set configuration values. Supported config params:
// buffer: size of buffer for rx packets (default=514 bytes)
// timeout: Default read timeout (default=300,000 milliseconds)
-STATIC mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+static mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
esp_espnow_obj_t *self = _get_singleton();
enum { ARG_get, ARG_rxbuf, ARG_timeout_ms, ARG_rate };
static const mp_arg_t allowed_args[] = {
@@ -278,11 +278,11 @@
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_config_obj, 1, espnow_config);
+static MP_DEFINE_CONST_FUN_OBJ_KW(espnow_config_obj, 1, espnow_config);
// ESPNow.irq(recv_cb)
// Set callback function to be invoked when a message is received.
-STATIC mp_obj_t espnow_irq(size_t n_args, const mp_obj_t *args) {
+static mp_obj_t espnow_irq(size_t n_args, const mp_obj_t *args) {
esp_espnow_obj_t *self = _get_singleton();
mp_obj_t recv_cb = args[1];
if (recv_cb != mp_const_none && !mp_obj_is_callable(recv_cb)) {
@@ -292,12 +292,12 @@
self->recv_cb_arg = (n_args > 2) ? args[2] : mp_const_none;
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_irq_obj, 2, 3, espnow_irq);
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_irq_obj, 2, 3, espnow_irq);
// ESPnow.stats(): Provide some useful stats.
// Returns a tuple of:
// (tx_pkts, tx_responses, tx_failures, rx_pkts, dropped_rx_pkts)
-STATIC mp_obj_t espnow_stats(mp_obj_t _) {
+static mp_obj_t espnow_stats(mp_obj_t _) {
const esp_espnow_obj_t *self = _get_singleton();
return NEW_TUPLE(
mp_obj_new_int(self->tx_packets),
@@ -306,7 +306,7 @@
mp_obj_new_int(self->rx_packets),
mp_obj_new_int(self->dropped_rx_pkts));
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_stats_obj, espnow_stats);
+static MP_DEFINE_CONST_FUN_OBJ_1(espnow_stats_obj, espnow_stats);
#if MICROPY_PY_ESPNOW_RSSI
// ### Maintaining the peer table and reading RSSI values
@@ -401,7 +401,7 @@
// loaded into the 3rd and 4th elements.
// Default timeout is set with ESPNow.config(timeout=milliseconds).
// Return (None, None) on timeout.
-STATIC mp_obj_t espnow_recvinto(size_t n_args, const mp_obj_t *args) {
+static mp_obj_t espnow_recvinto(size_t n_args, const mp_obj_t *args) {
esp_espnow_obj_t *self = _get_singleton_initialised();
mp_int_t timeout_ms = ((n_args > 2 && args[2] != mp_const_none)
@@ -456,15 +456,15 @@
return MP_OBJ_NEW_SMALL_INT(msg_len);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_recvinto_obj, 2, 3, espnow_recvinto);
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_recvinto_obj, 2, 3, espnow_recvinto);
// Test if data is available to read from the buffers
-STATIC mp_obj_t espnow_any(const mp_obj_t _) {
+static mp_obj_t espnow_any(const mp_obj_t _) {
esp_espnow_obj_t *self = _get_singleton_initialised();
return ringbuf_avail(self->recv_buffer) ? mp_const_true : mp_const_false;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_any_obj, espnow_any);
+static MP_DEFINE_CONST_FUN_OBJ_1(espnow_any_obj, espnow_any);
// Used by espnow_send() for sends() with sync==True.
// Wait till all pending sent packet responses have been received.
@@ -494,7 +494,7 @@
// True if sync==True and message is received successfully by all recipients
// False if sync==True and message is not received by at least one recipient
// Raises: EAGAIN if the internal espnow buffers are full.
-STATIC mp_obj_t espnow_send(size_t n_args, const mp_obj_t *args) {
+static mp_obj_t espnow_send(size_t n_args, const mp_obj_t *args) {
esp_espnow_obj_t *self = _get_singleton_initialised();
// Check the various combinations of input arguments
const uint8_t *peer = (n_args > 2) ? _get_peer(args[1]) : NULL;
@@ -532,7 +532,7 @@
// Return False if sync and any peers did not respond.
return mp_obj_new_bool(!(sync && self->tx_failures != saved_failures));
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_send_obj, 2, 4, espnow_send);
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_send_obj, 2, 4, espnow_send);
// ### The ESP_Now send and recv callback routines
//
@@ -540,7 +540,7 @@
// Callback triggered when a sent packet is acknowledged by the peer (or not).
// Just count the number of responses and number of failures.
// These are used in the send() logic.
-STATIC void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) {
+static void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) {
esp_espnow_obj_t *self = _get_singleton();
self->tx_responses++;
if (status != ESP_NOW_SEND_SUCCESS) {
@@ -553,7 +553,7 @@
// ESPNow packet.
// If the buffer is full, drop the message and increment the dropped count.
// Schedules the user callback if one has been registered (ESPNow.config()).
-STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len) {
+static void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len) {
esp_espnow_obj_t *self = _get_singleton();
ringbuf_t *buf = self->recv_buffer;
// TODO: Test this works with ">".
@@ -584,17 +584,17 @@
// Set the ESP-NOW Primary Master Key (pmk) (for encrypted communications).
// Raise OSError if ESP-NOW functions are not initialised.
// Raise ValueError if key is not a bytes-like object exactly 16 bytes long.
-STATIC mp_obj_t espnow_set_pmk(mp_obj_t _, mp_obj_t key) {
+static mp_obj_t espnow_set_pmk(mp_obj_t _, mp_obj_t key) {
check_esp_err(esp_now_set_pmk(_get_bytes_len(key, ESP_NOW_KEY_LEN)));
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_set_pmk_obj, espnow_set_pmk);
+static MP_DEFINE_CONST_FUN_OBJ_2(espnow_set_pmk_obj, espnow_set_pmk);
// Common code for add_peer() and mod_peer() to process the args and kw_args:
// Raise ValueError if the LMK is not a bytes-like object of exactly 16 bytes.
// Raise TypeError if invalid keyword args or too many positional args.
// Return true if all args parsed correctly.
-STATIC bool _update_peer_info(
+static bool _update_peer_info(
esp_now_peer_info_t *peer, size_t n_args,
const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -630,7 +630,7 @@
// Update the cached peer count in self->peer_count;
// The peer_count ignores broadcast and multicast addresses and is used for the
// send() logic and is updated from add_peer(), mod_peer() and del_peer().
-STATIC void _update_peer_count() {
+static void _update_peer_count() {
esp_espnow_obj_t *self = _get_singleton_initialised();
esp_now_peer_info_t peer = {0};
@@ -654,7 +654,7 @@
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
// Raise TypeError if invalid keyword args or too many positional args.
// Return None.
-STATIC mp_obj_t espnow_add_peer(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+static mp_obj_t espnow_add_peer(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
esp_now_peer_info_t peer = {0};
memcpy(peer.peer_addr, _get_peer(args[1]), ESP_NOW_ETH_ALEN);
_update_peer_info(&peer, n_args - 2, args + 2, kw_args);
@@ -664,13 +664,13 @@
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_add_peer_obj, 2, espnow_add_peer);
+static MP_DEFINE_CONST_FUN_OBJ_KW(espnow_add_peer_obj, 2, espnow_add_peer);
// ESPNow.del_peer(peer_mac): Unregister peer_mac.
// Raise OSError if ESPNow.init() has not been called.
// Raise ValueError if peer is not a bytes-like objects or wrong length.
// Return None.
-STATIC mp_obj_t espnow_del_peer(mp_obj_t _, mp_obj_t peer) {
+static mp_obj_t espnow_del_peer(mp_obj_t _, mp_obj_t peer) {
uint8_t peer_addr[ESP_NOW_ETH_ALEN];
memcpy(peer_addr, _get_peer(peer), ESP_NOW_ETH_ALEN);
@@ -679,7 +679,7 @@
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_del_peer_obj, espnow_del_peer);
+static MP_DEFINE_CONST_FUN_OBJ_2(espnow_del_peer_obj, espnow_del_peer);
// Convert a peer_info struct to python tuple
// Used by espnow_get_peer() and espnow_get_peers()
@@ -697,7 +697,7 @@
// Return a tuple of tuples:
// ((peer_addr, lmk, channel, ifidx, encrypt),
// (peer_addr, lmk, channel, ifidx, encrypt), ...)
-STATIC mp_obj_t espnow_get_peers(mp_obj_t _) {
+static mp_obj_t espnow_get_peers(mp_obj_t _) {
esp_espnow_obj_t *self = _get_singleton_initialised();
// Build and initialise the peer info tuple.
@@ -711,14 +711,14 @@
return peerinfo_tuple;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_obj, espnow_get_peers);
+static MP_DEFINE_CONST_FUN_OBJ_1(espnow_get_peers_obj, espnow_get_peers);
#if MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
// ESPNow.get_peer(peer_mac): Get the peer info for peer_mac as a tuple.
// Raise OSError if ESPNow.init() has not been called.
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
// Return a tuple of (peer_addr, lmk, channel, ifidx, encrypt).
-STATIC mp_obj_t espnow_get_peer(mp_obj_t _, mp_obj_t arg1) {
+static mp_obj_t espnow_get_peer(mp_obj_t _, mp_obj_t arg1) {
esp_now_peer_info_t peer = {0};
memcpy(peer.peer_addr, _get_peer(arg1), ESP_NOW_ETH_ALEN);
@@ -726,7 +726,7 @@
return _peer_info_to_tuple(&peer);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_get_peer_obj, espnow_get_peer);
+static MP_DEFINE_CONST_FUN_OBJ_2(espnow_get_peer_obj, espnow_get_peer);
// ESPNow.mod_peer(peer_mac, [lmk, [channel, [ifidx, [encrypt]]]]) or
// ESPNow.mod_peer(peer_mac, [lmk=b'0123456789abcdef'|b''|None|False],
@@ -736,7 +736,7 @@
// Raise ValueError if mac or LMK are not bytes-like objects or wrong length.
// Raise TypeError if invalid keyword args or too many positional args.
// Return None.
-STATIC mp_obj_t espnow_mod_peer(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+static mp_obj_t espnow_mod_peer(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
esp_now_peer_info_t peer = {0};
memcpy(peer.peer_addr, _get_peer(args[1]), ESP_NOW_ETH_ALEN);
check_esp_err(esp_now_get_peer(peer.peer_addr, &peer));
@@ -748,12 +748,12 @@
return mp_const_none;
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espnow_mod_peer_obj, 2, espnow_mod_peer);
+static MP_DEFINE_CONST_FUN_OBJ_KW(espnow_mod_peer_obj, 2, espnow_mod_peer);
// ESPNow.espnow_peer_count(): Get the number of registered peers.
// Raise OSError if ESPNow.init() has not been called.
// Return a tuple of (num_total_peers, num_encrypted_peers).
-STATIC mp_obj_t espnow_peer_count(mp_obj_t _) {
+static mp_obj_t espnow_peer_count(mp_obj_t _) {
esp_now_peer_num_t peer_num = {0};
check_esp_err(esp_now_get_peer_num(&peer_num));
@@ -761,10 +761,10 @@
mp_obj_new_int(peer_num.total_num),
mp_obj_new_int(peer_num.encrypt_num));
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_count_obj, espnow_peer_count);
+static MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_count_obj, espnow_peer_count);
#endif
-STATIC const mp_rom_map_elem_t esp_espnow_locals_dict_table[] = {
+static const mp_rom_map_elem_t esp_espnow_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&espnow_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&espnow_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&espnow_irq_obj) },
@@ -786,9 +786,9 @@
{ MP_ROM_QSTR(MP_QSTR_peer_count), MP_ROM_PTR(&espnow_peer_count_obj) },
#endif // MICROPY_PY_ESPNOW_EXTRA_PEER_METHODS
};
-STATIC MP_DEFINE_CONST_DICT(esp_espnow_locals_dict, esp_espnow_locals_dict_table);
+static MP_DEFINE_CONST_DICT(esp_espnow_locals_dict, esp_espnow_locals_dict_table);
-STATIC const mp_rom_map_elem_t espnow_globals_dict_table[] = {
+static const mp_rom_map_elem_t espnow_globals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__espnow) },
{ MP_ROM_QSTR(MP_QSTR_ESPNowBase), MP_ROM_PTR(&esp_espnow_type) },
{ MP_ROM_QSTR(MP_QSTR_MAX_DATA_LEN), MP_ROM_INT(ESP_NOW_MAX_DATA_LEN)},
@@ -797,13 +797,13 @@
{ MP_ROM_QSTR(MP_QSTR_MAX_TOTAL_PEER_NUM), MP_ROM_INT(ESP_NOW_MAX_TOTAL_PEER_NUM)},
{ MP_ROM_QSTR(MP_QSTR_MAX_ENCRYPT_PEER_NUM), MP_ROM_INT(ESP_NOW_MAX_ENCRYPT_PEER_NUM)},
};
-STATIC MP_DEFINE_CONST_DICT(espnow_globals_dict, espnow_globals_dict_table);
+static MP_DEFINE_CONST_DICT(espnow_globals_dict, espnow_globals_dict_table);
// ### Dummy Buffer Protocol support
// ...so asyncio can poll.ipoll() on this device
// Support ioctl(MP_STREAM_POLL, ) for asyncio
-STATIC mp_uint_t espnow_stream_ioctl(
+static mp_uint_t espnow_stream_ioctl(
mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
if (request != MP_STREAM_POLL) {
*errcode = MP_EINVAL;
@@ -818,7 +818,7 @@
((self->tx_responses < self->tx_packets) ? MP_STREAM_POLL_WR : 0));
}
-STATIC const mp_stream_p_t espnow_stream_p = {
+static const mp_stream_p_t espnow_stream_p = {
.ioctl = espnow_stream_ioctl,
};
@@ -830,7 +830,7 @@
// rssi is the wifi signal strength from the last msg received
// (in dBm from -127 to 0)
// time_sec is the time in milliseconds since device last booted.
-STATIC void espnow_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
+static void espnow_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
esp_espnow_obj_t *self = _get_singleton();
if (dest[0] != MP_OBJ_NULL) { // Only allow "Load" operation
return;