@@ -23,93 +23,3 @@ pub const alloc = if (builtin.is_test)
2323 std .testing .allocator
2424else
2525 std .heap .wasm_allocator ;
26-
27- /// For host-owned allocations:
28- /// We need to keep track of our own pointer lengths because Zig
29- /// allocators usually don't do this and we need to be able to send
30- /// a direct pointer back to the host system. A more appropriate thing
31- /// to do would be to probably make a custom allocator that keeps track
32- /// of size.
33- var allocs : std .AutoHashMapUnmanaged ([* ]u8 , usize ) = .{};
34-
35- /// Allocate len bytes and return a pointer to the memory in the host.
36- /// The data is not zeroed.
37- pub export fn malloc (len : usize ) ? [* ]u8 {
38- return alloc_ (len ) catch return null ;
39- }
40-
41- fn alloc_ (len : usize ) ! [* ]u8 {
42- // Create the allocation
43- const slice = try alloc .alloc (u8 , len );
44- errdefer alloc .free (slice );
45-
46- // Store the size so we can deallocate later
47- try allocs .putNoClobber (alloc , slice .ptr , slice .len );
48- errdefer _ = allocs .remove (slice .ptr );
49-
50- return slice .ptr ;
51- }
52-
53- /// Free an allocation from malloc.
54- pub export fn free (ptr : ? [* ]u8 ) void {
55- if (ptr ) | v | {
56- if (allocs .get (v )) | len | {
57- const slice = v [0.. len ];
58- alloc .free (slice );
59- _ = allocs .remove (v );
60- }
61- }
62- }
63-
64- /// Convert an allocated pointer of any type to a host-owned pointer.
65- /// This pushes the responsibility to free it to the host. The returned
66- /// pointer will match the pointer but is typed correctly for returning
67- /// to the host.
68- pub fn toHostOwned (ptr : anytype ) ! [* ]u8 {
69- // Convert our pointer to a byte array
70- const info = @typeInfo (@TypeOf (ptr )).pointer ;
71- const T = info .child ;
72- const size = @sizeOf (T );
73- const casted = @as ([* ]u8 , @ptrFromInt (@intFromPtr (ptr )));
74-
75- // Store the information about it
76- try allocs .putNoClobber (alloc , casted , size );
77- errdefer _ = allocs .remove (casted );
78-
79- return casted ;
80- }
81-
82- /// Returns true if the value is host owned.
83- pub fn isHostOwned (ptr : anytype ) bool {
84- const casted = @as ([* ]u8 , @ptrFromInt (@intFromPtr (ptr )));
85- return allocs .contains (casted );
86- }
87-
88- /// Convert a pointer back to a module-owned value. The caller is expected
89- /// to cast or have the valid pointer for alloc calls.
90- pub fn toModuleOwned (ptr : anytype ) void {
91- const casted = @as ([* ]u8 , @ptrFromInt (@intFromPtr (ptr )));
92- _ = allocs .remove (casted );
93- }
94-
95- test "basics" {
96- const testing = std .testing ;
97- const buf = malloc (32 ).? ;
98- try testing .expect (allocs .size == 1 );
99- free (buf );
100- try testing .expect (allocs .size == 0 );
101- }
102-
103- test "toHostOwned" {
104- const testing = std .testing ;
105-
106- const Point = struct { x : u32 = 0 , y : u32 = 0 };
107- const p = try alloc .create (Point );
108- errdefer alloc .destroy (p );
109- const ptr = try toHostOwned (p );
110- try testing .expect (allocs .size == 1 );
111- try testing .expect (isHostOwned (p ));
112- try testing .expect (isHostOwned (ptr ));
113- free (ptr );
114- try testing .expect (allocs .size == 0 );
115- }
0 commit comments