aboutsummaryrefslogtreecommitdiff
path: root/src/memory_allocator.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/memory_allocator.zig')
-rw-r--r--src/memory_allocator.zig18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/memory_allocator.zig b/src/memory_allocator.zig
index a3cd71583..aeb91dfac 100644
--- a/src/memory_allocator.zig
+++ b/src/memory_allocator.zig
@@ -3,12 +3,24 @@ const builtin = @import("std").builtin;
const std = @import("std");
const mimalloc = @import("./allocators/mimalloc.zig");
+const FeatureFlags = @import("./feature_flags.zig");
+
const c = struct {
pub const malloc_size = mimalloc.mi_malloc_size;
pub const malloc_usable_size = mimalloc.mi_malloc_usable_size;
- pub const malloc = mimalloc.mi_malloc;
+ pub const malloc = struct {
+ pub inline fn malloc_wrapped(size: usize) ?*anyopaque {
+ if (comptime FeatureFlags.log_allocations) std.debug.print("Malloc: {d}\n", .{size});
+ return mimalloc.mi_malloc(size);
+ }
+ }.malloc_wrapped;
pub const free = mimalloc.mi_free;
- pub const posix_memalign = mimalloc.mi_posix_memalign;
+ pub const posix_memalign = struct {
+ pub inline fn mi_posix_memalign(p: [*c]?*anyopaque, alignment: usize, size: usize) c_int {
+ if (comptime FeatureFlags.log_allocations) std.debug.print("Posix_memalign: {d}\n", .{std.mem.alignForward(size, alignment)});
+ return mimalloc.mi_posix_memalign(p, alignment, size);
+ }
+ }.mi_posix_memalign;
};
const Allocator = mem.Allocator;
const assert = std.debug.assert;
@@ -51,7 +63,7 @@ const CAllocator = struct {
// multiple of the pointer size
const eff_alignment = @maximum(alignment, @sizeOf(usize));
- var aligned_ptr: ?*anyopaque = undefined;
+ var aligned_ptr: ?*anyopaque = null;
if (c.posix_memalign(&aligned_ptr, eff_alignment, len) != 0)
return null;