@@ -134,16 +134,28 @@ STATIC void mp_map_rehash(mp_map_t *map) {
134
134
DEBUG_printf ("mp_map_rehash(%p): " UINT_FMT " -> " UINT_FMT "\n" , map , old_alloc , new_alloc );
135
135
mp_map_elem_t * old_table = map -> table ;
136
136
mp_map_elem_t * new_table = m_new0 (mp_map_elem_t , new_alloc );
137
- // If we reach this point, table resizing succeeded, now we can edit the old map.
138
- map -> alloc = new_alloc ;
139
- map -> used = 0 ;
140
- map -> all_keys_are_qstrs = 1 ;
141
- map -> table = new_table ;
137
+
138
+ // If we reach this point then allocating the new table succeeded, so construct the new map.
139
+ mp_map_t new_map = {
140
+ .all_keys_are_qstrs = 1 ,
141
+ .is_fixed = 0 ,
142
+ .is_ordered = 0 ,
143
+ .used = 0 ,
144
+ .alloc = new_alloc ,
145
+ .table = new_table ,
146
+ };
142
147
for (size_t i = 0 ; i < old_alloc ; i ++ ) {
143
148
if (old_table [i ].key != MP_OBJ_NULL && old_table [i ].key != MP_OBJ_SENTINEL ) {
144
- mp_map_lookup (map , old_table [i ].key , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND )-> value = old_table [i ].value ;
149
+ mp_map_lookup (& new_map , old_table [i ].key , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND )-> value = old_table [i ].value ;
145
150
}
146
151
}
152
+
153
+ // Swap the old map for the new one.
154
+ mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION ();
155
+ * map = new_map ;
156
+ MICROPY_END_ATOMIC_SECTION (atomic_state );
157
+
158
+ // Free the old table.
147
159
m_del (mp_map_elem_t , old_table , old_alloc );
148
160
}
149
161
0 commit comments