v / vlib / v2 / builder / mem_darwin.c.v
27 lines · 23 sloc · 1.01 KB · de365a1fc6ab9c8cecbfd38bb4333bd24f887344
Raw
1// Copyright (c) 2020-2024 Joe Conigliaro. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module builder
5
6#include <malloc/malloc.h>
7
8struct C.malloc_statistics_t {
9 blocks_in_use u32
10 size_in_use usize
11 max_size_in_use usize
12 size_allocated usize
13}
14
15fn C.malloc_default_zone() voidptr
16fn C.malloc_zone_statistics(zone voidptr, stats &C.malloc_statistics_t)
17
18// darwin_live_malloc_bytes returns (current live malloc bytes, peak live bytes)
19// from the default malloc zone. Under `-gc none` (no frees) the current value
20// is monotonic across phases, so per-phase deltas are the exact number of bytes
21// each phase allocated and never released. This is the reliable counterpart to
22// runtime.used_memory(), whose resident_size reading is distorted by OS paging.
23fn darwin_live_malloc_bytes() (u64, u64) {
24 mut st := C.malloc_statistics_t{}
25 C.malloc_zone_statistics(C.malloc_default_zone(), &st)
26 return u64(st.size_in_use), u64(st.max_size_in_use)
27}
28