1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

map: fix leaksfree fix (#4254)

This commit is contained in:
ka-weihe 2020-04-05 23:31:53 +02:00 committed by GitHub
parent edb93914bb
commit a30d292385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -352,7 +352,6 @@ pub fn (m mut map) delete(key string) {
for meta == m.metas[index] { for meta == m.metas[index] {
kv_index := m.metas[index + 1] kv_index := m.metas[index + 1]
if key == m.key_values.data[kv_index].key { if key == m.key_values.data[kv_index].key {
C.memset(&m.key_values.data[kv_index], 0, sizeof(KeyValue))
for (m.metas[index + 2]>>hashbits) > 1 { for (m.metas[index + 2]>>hashbits) > 1 {
m.metas[index] = m.metas[index + 2] - probe_inc m.metas[index] = m.metas[index + 2] - probe_inc
m.metas[index + 1] = m.metas[index + 3] m.metas[index + 1] = m.metas[index + 3]
@ -364,6 +363,7 @@ pub fn (m mut map) delete(key string) {
if m.key_values.size <= 32 { if m.key_values.size <= 32 {
return return
} }
C.memset(&m.key_values.data[kv_index], 0, sizeof(KeyValue))
// Clean up key_values if too many have been deleted // Clean up key_values if too many have been deleted
if m.key_values.deletes >= (m.key_values.size >> 1) { if m.key_values.deletes >= (m.key_values.size >> 1) {
m.key_values.zeros_to_end() m.key_values.zeros_to_end()
@ -393,11 +393,16 @@ pub fn (m &map) keys() []string {
return keys return keys
} }
[unsafe_fn]
pub fn (m map) free() { pub fn (m map) free() {
unsafe{
free(m.metas) free(m.metas)
free(m.key_values.data) for i := u32(0); i < m.key_values.size; i++ {
if m.key_values.data[i].key.str == 0 {
continue
} }
m.key_values.data[i].key.free()
}
free(m.key_values.data)
} }
pub fn (m map) print() { pub fn (m map) print() {