Skip to content

Commit 1059a28

Browse files
committed
builtin: cleanup prealloc.c.v a bit
1 parent 5be2fca commit 1059a28

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

‎vlib/builtin/prealloc.c.v‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ __global g_memory_block &VMemoryBlock
2020
@[heap]
2121
struct VMemoryBlock {
2222
mut:
23-
id int
24-
mallocs int
25-
cap isize
26-
remaining isize
27-
previous &VMemoryBlock = 0
28-
next &VMemoryBlock = 0
29-
start &u8 = 0
30-
current &u8 = 0
23+
current &u8 = 0 // 8
24+
stop &u8 = 0 // 8
25+
start &u8 = 0 // 8
26+
previous &VMemoryBlock = 0 // 8
27+
next &VMemoryBlock = 0 // 8
28+
id int // 4
29+
mallocs int // 4
3130
}
3231

3332
fn vmemory_abort_on_nil(p voidptr, bytes isize) {
@@ -64,8 +63,7 @@ fn vmemory_block_new(prev &VMemoryBlock, at_least isize) &VMemoryBlock {
6463
$if prealloc_memset ? {
6564
unsafe { C.memset(v.start, int($d('prealloc_memset_value', 0)), block_size) }
6665
}
67-
v.cap = block_size
68-
v.remaining = block_size
66+
v.stop = unsafe { &u8(i64(v.start) + block_size) }
6967
v.current = v.start
7068
return v
7169
}
@@ -77,14 +75,15 @@ fn vmemory_block_malloc(n isize) &u8 {
7775
g_memory_block.id, n)
7876
}
7977
unsafe {
80-
if _unlikely_(g_memory_block.remaining < n) {
78+
remaining := i64(g_memory_block.stop) - i64(g_memory_block.current)
79+
if _unlikely_(remaining < n) {
8180
g_memory_block = vmemory_block_new(g_memory_block, n)
8281
}
83-
mut res := &u8(nil)
84-
res = g_memory_block.current
82+
res := &u8(g_memory_block.current)
8583
g_memory_block.current += n
86-
g_memory_block.remaining -= n
87-
g_memory_block.mallocs++
84+
$if prealloc_stats ? {
85+
g_memory_block.mallocs++
86+
}
8887
return res
8988
}
9089
}
@@ -112,14 +111,16 @@ fn prealloc_vcleanup() {
112111
// in the first loop may still use g_memory_block
113112
// The second loop however should *not* allocate at all.
114113
mut nr_mallocs := i64(0)
115-
mut total_used := u64(0)
114+
mut total_used := i64(0)
116115
mut mb := g_memory_block
117116
for unsafe { mb != 0 } {
118117
nr_mallocs += mb.mallocs
119-
used := u64(mb.current) - u64(mb.start)
118+
used := i64(mb.current) - i64(mb.start)
120119
total_used += used
121-
C.fprintf(C.stderr, c'> freeing mb: %16p, mb.id: %3d | cap: %10lld | rem: %10lld | start: %16p | current: %16p | used: %10lld bytes | mallocs: %6d\n',
122-
mb, mb.id, mb.cap, mb.remaining, mb.start, mb.current, used, mb.mallocs)
120+
remaining := i64(mb.stop) - i64(mb.current)
121+
size := i64(mb.stop) - i64(mb.start)
122+
C.fprintf(C.stderr, c'> freeing mb: %16p, mb.id: %3d | size: %10lld | rem: %10lld | start: %16p | current: %16p | used: %10lld bytes | mallocs: %6d\n',
123+
mb, mb.id, size, remaining, mb.start, mb.current, used, mb.mallocs)
123124
mb = mb.previous
124125
}
125126
C.fprintf(C.stderr, c'> nr_mallocs: %lld, total_used: %lld bytes\n', nr_mallocs,

0 commit comments

Comments
 (0)