gc: fix another alignment issue (#9489)

pull/9493/head
Uwe Krüger 2021-03-27 18:59:51 +01:00 committed by GitHub
parent 4a10514081
commit 76dc4cf13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 32 deletions

View File

@ -190,11 +190,7 @@ pub fn malloc(n int) byteptr {
} $else { } $else {
$if gcboehm ? { $if gcboehm ? {
unsafe { unsafe {
if C.__v_inside_init == 0 { res = C.GC_MALLOC(n)
res = C.GC_MALLOC(n)
} else {
res = C.GC_MALLOC_UNCOLLECTABLE(n)
}
} }
} $else { } $else {
res = unsafe { C.malloc(n) } res = unsafe { C.malloc(n) }
@ -298,11 +294,7 @@ pub fn vcalloc(n int) byteptr {
return byteptr(0) return byteptr(0)
} }
$if gcboehm ? { $if gcboehm ? {
return if C.__v_inside_init == 0 { return byteptr(C.GC_MALLOC(n))
byteptr(C.GC_MALLOC(n))
} else {
byteptr(C.GC_MALLOC_UNCOLLECTABLE(n))
}
} $else { } $else {
return C.calloc(1, n) return C.calloc(1, n)
} }

View File

@ -94,6 +94,7 @@ fn fast_string_eq(a string, b string) bool {
// DenseArray represents a dynamic array with very low growth factor // DenseArray represents a dynamic array with very low growth factor
struct DenseArray { struct DenseArray {
key_bytes int key_bytes int
key_bytes_ int
value_bytes int value_bytes int
slot_bytes int // sum of 2 fields above slot_bytes int // sum of 2 fields above
mut: mut:
@ -108,14 +109,18 @@ mut:
[inline] [inline]
fn new_dense_array(key_bytes int, value_bytes int) DenseArray { 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 ? { $if gcboehm ? {
align, mask := $if x64 { 7, int(0xfffffff8) } $else { 3, int(0xfffffffc) } 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 cap := 8
return DenseArray{ return DenseArray{
key_bytes: key_bytes key_bytes: key_bytes
key_bytes_: key_bytes_
value_bytes: value_bytes value_bytes: value_bytes
slot_bytes: slot_bytes slot_bytes: slot_bytes
cap: cap cap: cap
@ -134,7 +139,7 @@ fn (d &DenseArray) key(i int) voidptr {
// for cgen // for cgen
[inline] [inline]
fn (d &DenseArray) value(i int) voidptr { 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] [inline]
@ -209,7 +214,8 @@ type MapFreeFn = fn (voidptr)
// map is the internal representation of a V `map` type. // map is the internal representation of a V `map` type.
pub struct map { pub struct map {
// Number of bytes of a key // Number of bytes of a key
key_bytes int key_bytes int
key_bytes_ int
// Number of bytes of a value // Number of bytes of a value
value_bytes int value_bytes int
mut: 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)) metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
// for now assume anything bigger than a pointer is a string // for now assume anything bigger than a pointer is a string
has_string_keys := key_bytes > sizeof(voidptr) 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{ return map{
key_bytes: key_bytes key_bytes: key_bytes
key_bytes_: key_bytes_
value_bytes: value_bytes value_bytes: value_bytes
even_index: init_even_index even_index: init_even_index
cached_hashbits: max_cached_hashbits 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) } pkey := unsafe { m.key_values.key(kv_index) }
if m.key_eq_fn(key, pkey) { if m.key_eq_fn(key, pkey) {
unsafe { unsafe {
pval := byteptr(pkey) + m.key_bytes pval := byteptr(pkey) + m.key_bytes_
C.memcpy(pval, value, m.value_bytes) C.memcpy(pval, value, m.value_bytes)
} }
return return
@ -457,7 +469,7 @@ fn (mut m map) set_1(key voidptr, value voidptr) {
unsafe { unsafe {
pkey := m.key_values.key(kv_index) pkey := m.key_values.key(kv_index)
m.clone_fn(pkey, key) 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.meta_greater(index, meta, u32(kv_index))
m.len++ 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] }) kv_index := int(unsafe { m.metas[index + 1] })
pkey := unsafe { m.key_values.key(kv_index) } pkey := unsafe { m.key_values.key(kv_index) }
if m.key_eq_fn(key, pkey) { 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 index += 2
@ -562,7 +574,7 @@ fn (m &map) get_1(key voidptr, zero voidptr) voidptr {
kv_index := int(unsafe { m.metas[index + 1] }) kv_index := int(unsafe { m.metas[index + 1] })
pkey := unsafe { m.key_values.key(kv_index) } pkey := unsafe { m.key_values.key(kv_index) }
if m.key_eq_fn(key, pkey) { 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 index += 2
@ -585,7 +597,7 @@ fn (m &map) get_1_check(key voidptr) voidptr {
kv_index := int(unsafe { m.metas[index + 1] }) kv_index := int(unsafe { m.metas[index + 1] })
pkey := unsafe { m.key_values.key(kv_index) } pkey := unsafe { m.key_values.key(kv_index) }
if m.key_eq_fn(key, pkey) { 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 index += 2
@ -725,6 +737,7 @@ fn (m &map) keys_1() array {
fn (d &DenseArray) clone() DenseArray { fn (d &DenseArray) clone() DenseArray {
res := DenseArray{ res := DenseArray{
key_bytes: d.key_bytes key_bytes: d.key_bytes
key_bytes_: d.key_bytes_
value_bytes: d.value_bytes value_bytes: d.value_bytes
slot_bytes: d.slot_bytes slot_bytes: d.slot_bytes
cap: d.cap cap: d.cap
@ -748,6 +761,7 @@ pub fn (m &map) clone() map {
metasize := int(sizeof(u32) * (m.even_index + 2 + m.extra_metas)) metasize := int(sizeof(u32) * (m.even_index + 2 + m.extra_metas))
res := map{ res := map{
key_bytes: m.key_bytes key_bytes: m.key_bytes
key_bytes_: m.key_bytes_
value_bytes: m.value_bytes value_bytes: m.value_bytes
even_index: m.even_index even_index: m.even_index
cached_hashbits: m.cached_hashbits cached_hashbits: m.cached_hashbits

View File

@ -335,9 +335,6 @@ static inline bool _us64_lt(uint64_t a, int64_t b) { return a < INT64_MAX && (in
#endif #endif
//================================== GLOBALS =================================*/ //================================== GLOBALS =================================*/
#if defined(_VGCBOEHM)
int __v_inside_init = 1;
#endif
//byte g_str_buf[1024]; //byte g_str_buf[1024];
byte* g_str_buf; byte* g_str_buf;
int load_so(byteptr); int load_so(byteptr);

View File

@ -76,11 +76,6 @@ fn (mut g Gen) gen_c_main_header() {
g.writeln('#endif') g.writeln('#endif')
} }
g.writeln('\t_vinit(___argc, (voidptr)___argv);') 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 { if g.pref.is_prof {
g.writeln('') g.writeln('')
g.writeln('\tatexit(vprint_profile_stats);') 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('#endif')
} }
g.writeln('\t_vinit(___argc, (voidptr)___argv);') 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() all_tfuncs := g.get_all_test_function_names()
if g.pref.is_stats { if g.pref.is_stats {
g.writeln('\tmain__BenchedTests bt = main__start_testing($all_tfuncs.len, _SLIT("$g.pref.path"));') g.writeln('\tmain__BenchedTests bt = main__start_testing($all_tfuncs.len, _SLIT("$g.pref.path"));')