From 76dc4cf13fef91f29ae94976a20776f746d55b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 27 Mar 2021 18:59:51 +0100 Subject: [PATCH] gc: fix another alignment issue (#9489) --- vlib/builtin/builtin.c.v | 12 ++---------- vlib/builtin/map.v | 32 +++++++++++++++++++++++--------- vlib/v/gen/c/cheaders.v | 3 --- vlib/v/gen/c/cmain.v | 10 ---------- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 19c779ebfd..a5d71aa96f 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -190,11 +190,7 @@ pub fn malloc(n int) byteptr { } $else { $if gcboehm ? { unsafe { - if C.__v_inside_init == 0 { - res = C.GC_MALLOC(n) - } else { - res = C.GC_MALLOC_UNCOLLECTABLE(n) - } + res = C.GC_MALLOC(n) } } $else { res = unsafe { C.malloc(n) } @@ -298,11 +294,7 @@ pub fn vcalloc(n int) byteptr { return byteptr(0) } $if gcboehm ? { - return if C.__v_inside_init == 0 { - byteptr(C.GC_MALLOC(n)) - } else { - byteptr(C.GC_MALLOC_UNCOLLECTABLE(n)) - } + return byteptr(C.GC_MALLOC(n)) } $else { return C.calloc(1, n) } diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 0763c8f2aa..a57c2ebaf7 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -94,6 +94,7 @@ fn fast_string_eq(a string, b string) bool { // DenseArray represents a dynamic array with very low growth factor struct DenseArray { key_bytes int + key_bytes_ int value_bytes int slot_bytes int // sum of 2 fields above mut: @@ -108,14 +109,18 @@ mut: [inline] fn new_dense_array(key_bytes int, value_bytes int) DenseArray { - mut slot_bytes := key_bytes + value_bytes + mut key_bytes_ := key_bytes + mut value_bytes_ := value_bytes $if gcboehm ? { align, mask := $if x64 { 7, int(0xfffffff8) } $else { 3, int(0xfffffffc) } - slot_bytes = (slot_bytes + align) & mask + key_bytes_ = (key_bytes + align) & mask + value_bytes_ = (value_bytes + align) & mask } + slot_bytes := key_bytes_ + value_bytes_ cap := 8 return DenseArray{ key_bytes: key_bytes + key_bytes_: key_bytes_ value_bytes: value_bytes slot_bytes: slot_bytes cap: cap @@ -134,7 +139,7 @@ fn (d &DenseArray) key(i int) voidptr { // for cgen [inline] fn (d &DenseArray) value(i int) voidptr { - return unsafe { d.data + i * d.slot_bytes + d.key_bytes } + return unsafe { d.data + i * d.slot_bytes + d.key_bytes_ } } [inline] @@ -209,7 +214,8 @@ type MapFreeFn = fn (voidptr) // map is the internal representation of a V `map` type. pub struct map { // Number of bytes of a key - key_bytes int + key_bytes int + key_bytes_ int // Number of bytes of a value value_bytes int mut: @@ -323,8 +329,14 @@ fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqF metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc)) // for now assume anything bigger than a pointer is a string has_string_keys := key_bytes > sizeof(voidptr) + mut key_bytes_ := key_bytes + $if gcboehm ? { + align, mask := $if x64 { 7, int(0xfffffff8) } $else { 3, int(0xfffffffc) } + key_bytes_ = (key_bytes + align) & mask + } return map{ key_bytes: key_bytes + key_bytes_: key_bytes_ value_bytes: value_bytes even_index: init_even_index cached_hashbits: max_cached_hashbits @@ -445,7 +457,7 @@ fn (mut m map) set_1(key voidptr, value voidptr) { pkey := unsafe { m.key_values.key(kv_index) } if m.key_eq_fn(key, pkey) { unsafe { - pval := byteptr(pkey) + m.key_bytes + pval := byteptr(pkey) + m.key_bytes_ C.memcpy(pval, value, m.value_bytes) } return @@ -457,7 +469,7 @@ fn (mut m map) set_1(key voidptr, value voidptr) { unsafe { pkey := m.key_values.key(kv_index) m.clone_fn(pkey, key) - C.memcpy(byteptr(pkey) + m.key_bytes, value, m.value_bytes) + C.memcpy(byteptr(pkey) + m.key_bytes_, value, m.value_bytes) } m.meta_greater(index, meta, u32(kv_index)) m.len++ @@ -536,7 +548,7 @@ fn (mut m map) get_and_set_1(key voidptr, zero voidptr) voidptr { kv_index := int(unsafe { m.metas[index + 1] }) pkey := unsafe { m.key_values.key(kv_index) } if m.key_eq_fn(key, pkey) { - return unsafe { byteptr(pkey) + m.key_values.key_bytes } + return unsafe { byteptr(pkey) + m.key_values.key_bytes_ } } } index += 2 @@ -562,7 +574,7 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr { kv_index := int(unsafe { m.metas[index + 1] }) pkey := unsafe { m.key_values.key(kv_index) } if m.key_eq_fn(key, pkey) { - return unsafe { byteptr(pkey) + m.key_values.key_bytes } + return unsafe { byteptr(pkey) + m.key_values.key_bytes_ } } } index += 2 @@ -585,7 +597,7 @@ fn (m &map) get_1_check(key voidptr) voidptr { kv_index := int(unsafe { m.metas[index + 1] }) pkey := unsafe { m.key_values.key(kv_index) } if m.key_eq_fn(key, pkey) { - return unsafe { byteptr(pkey) + m.key_values.key_bytes } + return unsafe { byteptr(pkey) + m.key_values.key_bytes_ } } } index += 2 @@ -725,6 +737,7 @@ fn (m &map) keys_1() array { fn (d &DenseArray) clone() DenseArray { res := DenseArray{ key_bytes: d.key_bytes + key_bytes_: d.key_bytes_ value_bytes: d.value_bytes slot_bytes: d.slot_bytes cap: d.cap @@ -748,6 +761,7 @@ pub fn (m &map) clone() map { metasize := int(sizeof(u32) * (m.even_index + 2 + m.extra_metas)) res := map{ key_bytes: m.key_bytes + key_bytes_: m.key_bytes_ value_bytes: m.value_bytes even_index: m.even_index cached_hashbits: m.cached_hashbits diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index 50ab7c4f24..ab0c70c9bf 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -335,9 +335,6 @@ static inline bool _us64_lt(uint64_t a, int64_t b) { return a < INT64_MAX && (in #endif //================================== GLOBALS =================================*/ -#if defined(_VGCBOEHM) - int __v_inside_init = 1; -#endif //byte g_str_buf[1024]; byte* g_str_buf; int load_so(byteptr); diff --git a/vlib/v/gen/c/cmain.v b/vlib/v/gen/c/cmain.v index 6abfa6f18d..d08dc766f1 100644 --- a/vlib/v/gen/c/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -76,11 +76,6 @@ fn (mut g Gen) gen_c_main_header() { g.writeln('#endif') } g.writeln('\t_vinit(___argc, (voidptr)___argv);') - if g.pref.gc_mode in [.boehm, .boehm_leak] { - g.writeln('#if defined(_VGCBOEHM)') - g.writeln('\t__v_inside_init = 0;') - g.writeln('#endif') - } if g.pref.is_prof { g.writeln('') g.writeln('\tatexit(vprint_profile_stats);') @@ -172,11 +167,6 @@ pub fn (mut g Gen) gen_c_main_for_tests() { g.writeln('#endif') } g.writeln('\t_vinit(___argc, (voidptr)___argv);') - if g.pref.gc_mode in [.boehm, .boehm_leak] { - g.writeln('#if defined(_VGCBOEHM)') - g.writeln('\t__v_inside_init = 0;') - g.writeln('#endif') - } all_tfuncs := g.get_all_test_function_names() if g.pref.is_stats { g.writeln('\tmain__BenchedTests bt = main__start_testing($all_tfuncs.len, _SLIT("$g.pref.path"));')