-
Notifications
You must be signed in to change notification settings - Fork 0
/
autopointer.t
148 lines (115 loc) · 3.19 KB
/
autopointer.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
local util = require("util")
local m = require("mem")
local templatize = require("templatize")
local C = terralib.includecstring [[
#include "stdio.h"
]]
-- Pretty much just like stl::auto_ptr
local struct RefCount
{
count: uint
}
terra RefCount:__construct()
self.count = 1
end
terra RefCount:retain()
self.count = self.count + 1
end
terra RefCount:release()
util.assert(self.count > 0,
"Cannot release on a RefCount with zero references\n")
self.count = self.count - 1
end
terra RefCount:empty()
return self.count == 0
end
m.addConstructors(RefCount)
local AutoPtr = templatize(function(T)
local struct AutoPtrT
{
ptr: &T,
refCount: &RefCount
}
AutoPtrT.metamethods.__typename = function(self)
return string.format("AutoPtr(%s)", tostring(T))
end
terra AutoPtrT:__construct()
self.ptr = nil
self.refCount = nil
end
terra AutoPtrT:__construct(ptr: &T)
self.ptr = ptr
self.refCount = RefCount.heapAlloc()
end
terra AutoPtrT:__copy(other: &AutoPtrT)
self.ptr = other.ptr
self.refCount = other.refCount
self.refCount:retain()
end
terra AutoPtrT:__destruct()
if self.ptr ~= nil and self.refCount ~= nil then
self.refCount:release()
if self.refCount:empty() then
-- C.printf("deleting %p\n", self.ptr)
m.delete(self.refCount)
m.delete(self.ptr)
end
end
end
AutoPtrT.metamethods.__entrymissing = macro(function(fieldname, self)
return `self.ptr.[fieldname]
end)
-- I use this more complicated behavior, rather than just using __methodmissing,
-- because I want AutoPtrT:getmethod to still return nil exactly when T:getmethod
-- would return nil.
AutoPtrT.metamethods.__getmethod = function(self, methodname)
-- IMPORTANT: We shouldn't forward the __initvtable method of the wrapped type
-- (There may need to be more exceptions like this...)
if methodname == "__initvtable" then return nil end
-- If AutoPtrT has the method (i.e. is it __construct, __destruct, __copy),
-- then just return that
local mymethod = self.methods[methodname]
if mymethod then return mymethod end
-- Otherwise, if T has it, then return a macro that will invoke T's
-- method on the .ptr member
local tmethod = T:getmethod(methodname)
if tmethod then
return macro(function(self, ...)
local args = {...}
return `[tmethod](self.ptr, [args])
end)
end
-- Otherwise, return nil
return nil
end
m.addConstructors(AutoPtrT)
return AutoPtrT
end)
-- Convience macro to create a new auto pointer without explicitly specifying the type
-- It'll figure out the type from the type of the argument
AutoPtr.wrap = macro(function(ptr)
local pT = ptr:gettype()
util.luaAssertWithTrace(pT:ispointertostruct(),
"Can only create an auto pointer from a pointer to a struct.")
local T = pT.type
return `[AutoPtr(T)].stackAlloc(ptr)
end)
------- TESTS
-- local struct Foo { x: int }
-- terra Foo:__construct() end
-- terra Foo:setX(x: int) self.x = x end
-- m.addConstructors(Foo)
-- local terra test()
-- var f = Foo.heapAlloc()
-- var af = [AutoPtr(Foo)].stackAlloc(f)
-- af:setX(42)
-- var x = af.x
-- var af2 = m.copy(af)
-- m.destruct(af)
-- m.destruct(af2)
-- return x
-- end
-- test:compile()
-- print(test())
-------
return AutoPtr