Skip to content

Commit e29e07c

Browse files
committed
Use the new @extern field, .is_dll_import, to link the memory allocation functions from a shared library with the -msvc ABI
1 parent f8f5281 commit e29e07c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/memory.zig

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const options = @import("zmesh_options");
23

34
pub fn init(alloc: std.mem.Allocator) void {
45
std.debug.assert(mem_allocator == null and mem_allocations == null);
@@ -7,10 +8,27 @@ pub fn init(alloc: std.mem.Allocator) void {
78
mem_allocations = std.AutoHashMap(usize, usize).init(alloc);
89
mem_allocations.?.ensureTotalCapacity(32) catch unreachable;
910

10-
zmeshMallocPtr = zmeshMalloc;
11-
zmeshCallocPtr = zmeshCalloc;
12-
zmeshReallocPtr = zmeshRealloc;
13-
zmeshFreePtr = zmeshFree;
11+
const zmeshMallocPtr = @extern(*?*const fn (size: usize) callconv(.C) ?*anyopaque, .{
12+
.name = "zmeshMallocPtr",
13+
.is_dll_import = options.shared,
14+
});
15+
const zmeshCallocPtr = @extern(*?*const fn (num: usize, size: usize) callconv(.C) ?*anyopaque, .{
16+
.name = "zmeshCallocPtr",
17+
.is_dll_import = options.shared,
18+
});
19+
const zmeshReallocPtr = @extern(*?*const fn (ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque, .{
20+
.name = "zmeshReallocPtr",
21+
.is_dll_import = options.shared,
22+
});
23+
const zmeshFreePtr = @extern(*?*const fn (maybe_ptr: ?*anyopaque) callconv(.C) void, .{
24+
.name = "zmeshFreePtr",
25+
.is_dll_import = options.shared,
26+
});
27+
28+
zmeshMallocPtr.* = zmeshMalloc;
29+
zmeshCallocPtr.* = zmeshCalloc;
30+
zmeshReallocPtr.* = zmeshRealloc;
31+
zmeshFreePtr.* = zmeshFree;
1432
meshopt_setAllocator(zmeshMalloc, zmeshFree);
1533
}
1634

@@ -33,8 +51,6 @@ var mem_allocations: ?std.AutoHashMap(usize, usize) = null;
3351
var mem_mutex: std.Thread.Mutex = .{};
3452
const mem_alignment = 16;
3553

36-
extern var zmeshMallocPtr: ?*const fn (size: usize) callconv(.C) ?*anyopaque;
37-
3854
pub fn zmeshMalloc(size: usize) callconv(.C) ?*anyopaque {
3955
mem_mutex.lock();
4056
defer mem_mutex.unlock();
@@ -50,8 +66,6 @@ pub fn zmeshMalloc(size: usize) callconv(.C) ?*anyopaque {
5066
return mem.ptr;
5167
}
5268

53-
extern var zmeshCallocPtr: ?*const fn (num: usize, size: usize) callconv(.C) ?*anyopaque;
54-
5569
fn zmeshCalloc(num: usize, size: usize) callconv(.C) ?*anyopaque {
5670
const ptr = zmeshMalloc(num * size);
5771
if (ptr != null) {
@@ -66,8 +80,6 @@ pub fn zmeshAllocUser(user: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
6680
return zmeshMalloc(size);
6781
}
6882

69-
extern var zmeshReallocPtr: ?*const fn (ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque;
70-
7183
fn zmeshRealloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
7284
mem_mutex.lock();
7385
defer mem_mutex.unlock();
@@ -91,8 +103,6 @@ fn zmeshRealloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
91103
return mem.ptr;
92104
}
93105

94-
extern var zmeshFreePtr: ?*const fn (maybe_ptr: ?*anyopaque) callconv(.C) void;
95-
96106
fn zmeshFree(maybe_ptr: ?*anyopaque) callconv(.C) void {
97107
if (maybe_ptr) |ptr| {
98108
mem_mutex.lock();

0 commit comments

Comments
 (0)