summaryrefslogtreecommitdiffstatshomepage
path: root/py/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/map.c')
-rw-r--r--py/map.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/py/map.c b/py/map.c
index dff0bdfea0..8554150adf 100644
--- a/py/map.c
+++ b/py/map.c
@@ -58,13 +58,13 @@ static void mp_map_rehash (mp_map_t *map) {
map->table = m_new0(mp_map_elem_t, map->alloc);
for (int i = 0; i < old_alloc; i++) {
if (old_table[i].key != NULL) {
- mp_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value;
+ mp_map_lookup_helper(map, old_table[i].key, true, false)->value = old_table[i].value;
}
}
m_del(mp_map_elem_t, old_table, old_alloc);
}
-mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found) {
+mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_not_found, bool remove_if_found) {
bool is_map_mp_obj = (map->kind == MP_MAP_OBJ);
machine_uint_t hash;
if (is_map_mp_obj) {
@@ -105,6 +105,15 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
elem->key = index;
}
*/
+ if (remove_if_found) {
+ map->used--;
+ /* this leaks this memory (but see dict_get_helper) */
+ mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
+ retval->key = elem->key;
+ retval->value = elem->value;
+ elem->key = NULL;
+ return retval;
+ }
return elem;
} else {
// not yet found, keep searching in this table
@@ -115,7 +124,7 @@ mp_map_elem_t* mp_map_lookup_helper(mp_map_t *map, mp_obj_t index, bool add_if_n
mp_map_elem_t* mp_qstr_map_lookup(mp_map_t *map, qstr index, bool add_if_not_found) {
mp_obj_t o = (mp_obj_t)(machine_uint_t)index;
- return mp_map_lookup_helper(map, o, add_if_not_found);
+ return mp_map_lookup_helper(map, o, add_if_not_found, false);
}
/******************************************************************************/