Skip to content

Commit 8b77e3d

Browse files
committed
stmhal: Put mod_network_nic_list in global root-pointer state.
It needs to be scanned by GC. Thanks to Daniel Campora.
1 parent 77089be commit 8b77e3d

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

py/mpstate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "py/misc.h"
3333
#include "py/nlr.h"
3434
#include "py/obj.h"
35+
#include "py/objlist.h" // in case port needs mp_obj_list_t in root pointers
3536
#include "py/objexcept.h"
3637

3738
// This file contains structures defining the state of the Micro Python

stmhal/modnetwork.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,25 @@
3838
///
3939
/// This module provides network drivers and routing configuration.
4040

41-
mp_obj_list_t mod_network_nic_list;
42-
4341
void mod_network_init(void) {
44-
mp_obj_list_init(&mod_network_nic_list, 0);
42+
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
4543
}
4644

4745
void mod_network_register_nic(mp_obj_t nic) {
48-
for (mp_uint_t i = 0; i < mod_network_nic_list.len; i++) {
49-
if (mod_network_nic_list.items[i] == nic) {
46+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
47+
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
5048
// nic already registered
5149
return;
5250
}
5351
}
5452
// nic not registered so add to list
55-
mp_obj_list_append(&mod_network_nic_list, nic);
53+
mp_obj_list_append(&MP_STATE_PORT(mod_network_nic_list), nic);
5654
}
5755

5856
mp_obj_t mod_network_find_nic(const uint8_t *ip) {
5957
// find a NIC that is suited to given IP address
60-
for (mp_uint_t i = 0; i < mod_network_nic_list.len; i++) {
61-
mp_obj_t nic = mod_network_nic_list.items[i];
58+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
59+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
6260
// TODO check IP suitability here
6361
//mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
6462
return nic;
@@ -68,7 +66,7 @@ mp_obj_t mod_network_find_nic(const uint8_t *ip) {
6866
}
6967

7068
STATIC mp_obj_t network_route(void) {
71-
return &mod_network_nic_list;
69+
return &MP_STATE_PORT(mod_network_nic_list);
7270
}
7371
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
7472

stmhal/modnetwork.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ typedef struct _mod_network_socket_obj_t {
7171
};
7272
} mod_network_socket_obj_t;
7373

74-
extern struct _mp_obj_list_t mod_network_nic_list;
7574
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
7675
extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
7776

stmhal/modusocket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
385385
mp_int_t port = mp_obj_get_int(port_in);
386386

387387
// find a NIC that can do a name lookup
388-
for (mp_uint_t i = 0; i < mod_network_nic_list.len; i++) {
389-
mp_obj_t nic = mod_network_nic_list.items[i];
388+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
389+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
390390
mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
391391
if (nic_type->gethostbyname != NULL) {
392392
uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE];

stmhal/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ extern const struct _mp_obj_module_t mp_module_network;
152152
\
153153
/* pointers to all UART objects (if they have been created) */ \
154154
struct _pyb_uart_obj_t *pyb_uart_obj_all[6]; \
155+
\
156+
/* list of registered NICs */ \
157+
mp_obj_list_t mod_network_nic_list; \
155158

156159
// type definitions for the specific machine
157160

0 commit comments

Comments
 (0)