mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: minimise sizeof(EmptyStruct) to 0 for gcc/clang and to 1 for tcc/msvc, by changing EMPTY_STRUCT_DECLARATION and EMPTY_STRUCT_INITIALIZATION (#16733)
This commit is contained in:
@@ -47,9 +47,13 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
|
||||
len: mylen
|
||||
cap: cap_
|
||||
}
|
||||
// x := []EmptyStruct{cap:5} ; for clang/gcc with -gc none,
|
||||
// -> sizeof(EmptyStruct) == 0 -> elm_size == 0
|
||||
// -> total_size == 0 -> malloc(0) -> panic;
|
||||
// to avoid it, just allocate a single byte
|
||||
total_size := u64(cap_) * u64(elm_size)
|
||||
if cap_ > 0 && mylen == 0 {
|
||||
arr.data = unsafe { malloc(total_size) }
|
||||
arr.data = unsafe { malloc(__at_least_one(total_size)) }
|
||||
} else {
|
||||
arr.data = vcalloc(total_size)
|
||||
}
|
||||
@@ -78,7 +82,7 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, d
|
||||
cap_ := if cap < mylen { mylen } else { cap }
|
||||
mut arr := array{
|
||||
element_size: elm_size
|
||||
data: unsafe { malloc(u64(cap_) * u64(elm_size)) }
|
||||
data: unsafe { malloc(__at_least_one(u64(cap_) * u64(elm_size))) }
|
||||
len: mylen
|
||||
cap: cap_
|
||||
}
|
||||
@@ -99,7 +103,7 @@ fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array
|
||||
cap_ := if cap < mylen { mylen } else { cap }
|
||||
mut arr := array{
|
||||
element_size: elm_size
|
||||
data: unsafe { malloc(u64(cap_) * u64(elm_size)) }
|
||||
data: unsafe { malloc(__at_least_one(u64(cap_) * u64(elm_size))) }
|
||||
len: mylen
|
||||
cap: cap_
|
||||
}
|
||||
@@ -156,7 +160,7 @@ fn (mut a array) ensure_cap(required int) {
|
||||
cap *= 2
|
||||
}
|
||||
new_size := u64(cap) * u64(a.element_size)
|
||||
new_data := unsafe { malloc(new_size) }
|
||||
new_data := unsafe { malloc(__at_least_one(new_size)) }
|
||||
if a.data != unsafe { nil } {
|
||||
unsafe { vmemcpy(new_data, a.data, u64(a.len) * u64(a.element_size)) }
|
||||
// TODO: the old data may be leaked when no GC is used (ref-counting?)
|
||||
@@ -601,13 +605,9 @@ pub fn (a &array) clone() array {
|
||||
// recursively clone given array - `unsafe` when called directly because depth is not checked
|
||||
[unsafe]
|
||||
pub fn (a &array) clone_to_depth(depth int) array {
|
||||
mut size := u64(a.cap) * u64(a.element_size)
|
||||
if size == 0 {
|
||||
size++
|
||||
}
|
||||
mut arr := array{
|
||||
element_size: a.element_size
|
||||
data: vcalloc(size)
|
||||
data: vcalloc(u64(a.cap) * u64(a.element_size))
|
||||
len: a.len
|
||||
cap: a.cap
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user