-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.t
174 lines (160 loc) · 3.98 KB
/
mem.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local cstdlib = terralib.includec("stdlib.h")
local function genVtableInitStatement(inst, typ)
if typ:isstruct() and typ:getmethod("__initvtable") then
return `inst:__initvtable()
else
return quote end
end
end
local function initerr(typ)
error(string.format("Non-POD type '%s' must have a no-argument constructor.", tostring(typ)))
end
local init = macro(function(val)
local t = val:gettype()
if t:isstruct() then
t:complete()
local ctors = t:getmethod("__construct")
if not ctors then return quote end end
for _,d in ipairs(ctors:getdefinitions()) do
if #d:gettype().parameters == 1 then
return quote
[genVtableInitStatement(val, t)]
val:__construct()
end
end
end
initerr(t)
else
return quote
[genVtableInitStatement(val, t)]
end
end
end)
local new = macro(function(type)
local t = type:astype()
return quote
var nt = [&t](cstdlib.malloc(sizeof(t)))
[genVtableInitStatement(nt, t)]
in
nt
end
end)
local delete = macro(function(ptr)
local t = ptr:gettype()
if t:ispointertostruct() and t.type:getmethod("__destruct") then
t.type:complete()
return quote
[ptr]:__destruct()
cstdlib.free([ptr])
end
else
return `cstdlib.free([ptr])
end
end)
local destruct = macro(function(val)
local t = val:gettype()
if t:isstruct() and t:getmethod("__destruct") then
t:complete()
return `val:__destruct()
else
return quote end
end
end)
local function copyfn(val)
local t = val:gettype()
if t:isstruct() and t:getmethod("__destruct") and not t:getmethod("__copy") then
error(string.format("Attempt to copy non-POD struct type '%s' that does not have a __copy method", tostring(t)))
end
if t:isstruct() and t:getmethod("__copy") then
t:complete()
return quote
var cp : t
[genVtableInitStatement(cp, t)]
cp:__copy(&val)
in
cp
end
else
return val
end
end
local copy = macro(copyfn)
local function templatecopy(NewType)
return macro(function(val)
local OldType = val:gettype()
local OldTemplate = rawget(OldType, "__generatorTemplate")
local NewTemplate = rawget(NewType, "__generatorTemplate")
if OldType:isstruct() and OldTemplate and rawget(OldType, "__templatecopy") then
if not (NewTemplate and NewTemplate == OldTemplate) then
error(string.format("templatecopy: copy destination type %s does not have the same generator template as copy source type %s", tostring(NewType), tostring(OldType)))
end
return quote
var cp : NewType
[genVtableInitStatement(cp, NewType)]
[NewType.__templatecopy(unpack(OldType.__templateParams))](&cp, &val)
in
cp
end
else
return copyfn(val)
end
end)
end
-- Ensure that a cdata object returned from Terra code to Lua code gets properly destructed.
-- Call this (only) if the Lua code is assuming ownership of the returned object.
local ffi = require("ffi")
local function gc(obj)
local t = terralib.typeof(obj)
if t:isstruct() and t:getmethod("__destruct") then
ffi.gc(obj, t:getmethod("__destruct"))
end
return obj
end
-- Decorate any struct type with this method
-- to automatically add the "stackAlloc" and
-- "heapAlloc" methods to the type
local function addConstructors(structType)
local function genConstructStatement(inst, args)
if structType:getmethod("__construct") then
return `inst:__construct([args])
end
end
structType.methods.stackAlloc = macro(function(...)
structType:complete()
local args = {}
for i=1,select("#", ...) do
args[i] = (select(i, ...))
end
return quote
var x : structType
[genVtableInitStatement(x, structType)]
[genConstructStatement(x, args)]
in
x
end
end)
structType.methods.heapAlloc = macro(function(...)
structType:complete()
local args = {}
for i=1,select("#", ...) do
args[i] = (select(i, ...))
end
return quote
var x = new(structType)
[genConstructStatement(x, args)]
in
x
end
end)
end
return
{
init = init,
new = new,
delete = delete,
destruct = destruct,
copy = copy,
templatecopy = templatecopy,
gc = gc,
addConstructors = addConstructors
}