-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartmem.t
367 lines (309 loc) · 10.6 KB
/
smartmem.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
require "terralibext"
local C = terralib.includecstring[[
#include <stdio.h>
#include <string.h>
]]
local base = require("base")
local interface = require("interface")
local range = require("range")
local err = require("assert")
local size_t = uint64
local u8 = uint8
local function ismanaged(args)
local T, method = args.type, args.method
if not T:isstruct() then
return false
end
terralib.ext.addmissing[method](T)
if T.methods[method] then
return true
end
return false
end
local function Base(block, T, options)
local options = terralib.newlist(options)
options.copyby = options.copyby or "view"
--copy-assignment is one of the following three options
local valid_copyby = {["move"] = true, ["view"] = true, ["clone"] = true}
assert(
valid_copyby[options.copyby],
"Provided invalid option " .. options.copyby .. " for copy constructor"
)
--type traits
block.isblock = true
block.type = block
block.traits.eltype = T
block.elsize = T==opaque and 1 or sizeof(T)
block.methods.getdataptr = terra(self : &block)
return self.ptr
end
--block is empty, no resource and no allocator
block.methods.isempty = terra(self : &block)
return self.ptr==nil
end
--resource is borrowed, there is no allocator
--this represents a view of the data
block.methods.borrows_resource = terra(self : &block)
return self.ptr~=nil and self.alloc.data==nil
end
--resource is owned, there is an allocator
block.methods.owns_resource = terra(self : &block)
return self.ptr~=nil and self.alloc.data~=nil
end
block.methods.size_in_bytes = terra(self : &block) : size_t
return self.nbytes
end
if T==opaque then
block.methods.size = terra(self : &block) : size_t
return self.nbytes
end
else
block.methods.size = terra(self : &block) : size_t
return self.nbytes / [block.elsize]
end
end
--initialize to empty block
block.methods.__init = terra(self : &block)
self.ptr = nil
self.nbytes = 0
self.alloc.data = nil
self.alloc.tab = nil
end
--exact clone of the block
block.methods.clone = terra(self : &block)
--allocate memory for exact clone
var newblk : block
if not self:isempty() then
self.alloc:__allocators_best_friend(&newblk, [ block.elsize ], self:size())
if not newblk:isempty() then
C.memcpy(newblk.ptr, self.ptr, self:size_in_bytes())
end
end
return newblk
end
--specialized copy-assignment, moving resources over
if options.copyby == "move" then
block.methods.__copy = terra(from : &block, to : &block)
--set to
to.ptr = from.ptr
to.nbytes = from.nbytes
to.alloc = from.alloc
--reset from
from:__init()
end
--specialized copy-assignment, returning a non-owning view of the data
elseif options.copyby == "view" then
block.methods.__copy = terra(from : &block, to : &block)
to.ptr = from.ptr
to.nbytes = from.nbytes
--no allocator
to.alloc.data = nil
to.alloc.tab = nil
end
--specialized copy-assignment, returning a deepcopy or clone
elseif options.copyby == "clone" then
block.methods.__copy = terra(from : &block, to : &block)
@to = from:clone()
end
end
--add raii move method
terralib.ext.addmissing.__move(block)
end
--abstraction of a memory block without any type information.
local struct block
local __Allocator = interface.Interface:new{
__allocators_best_friend = {&block, size_t, size_t}->{}
}
struct block{
ptr : &opaque
nbytes : size_t
alloc : __Allocator
}
function block.metamethods.__typename(self)
return "block"
end
base.AbstractBase(block)
--add base functionality
Base(block, opaque)
--__dtor for opaque memory block
terra block.methods.__dtor(self : &block)
if self:borrows_resource() then
self:__init()
elseif self:owns_resource() then
self.alloc:__allocators_best_friend(self, 0, 0)
end
end
block:complete()
--abstraction of a memory block with type information.
local SmartBlock = terralib.memoize(function(T, options)
local struct block{
ptr : &T
nbytes : size_t
alloc : __Allocator
}
function block.metamethods.__typename(self)
return ("SmartBlock(%s)"):format(tostring(T))
end
base.AbstractBase(block)
-- Cast block from one type to another
function block.metamethods.__cast(from, to, exp)
local function passbyvalue(to, from)
if from:ispointertostruct() and to:ispointertostruct() then
return false, to.type, from.type
end
return true, to, from
end
--process types
local byvalue, to, from = passbyvalue(to, from)
--exit early if types do not match
if not to.isblock or not from.isblock then
error("Arguments to cast need to be of generic type SmartBlock.")
end
--perform cast
if byvalue then
--case when to.eltype is a managed type
if ismanaged{type=to.traits.eltype, method="__init"} then
return quote
var tmp = __move__(exp)
--debug check if sizes are compatible, that is, is the
--remainder zero after integer division
err.assert(tmp:size_in_bytes() % [to.elsize] == 0)
--loop over all elements of blk and initialize their entries
var size = tmp:size_in_bytes() / [to.elsize]
var ptr = [&to.traits.eltype](tmp.ptr)
for i = 0, size do
ptr:__init()
ptr = ptr + 1
end
in
[to.type]{[&to.traits.eltype](tmp.ptr), tmp.nbytes, tmp.alloc}
end
--simple case when to.eltype is not managed
else
return quote
var tmp = __move__(exp)
--debug check if sizes are compatible, that is, is the
--remainder zero after integer division
err.assert(tmp:size_in_bytes() % [to.elsize] == 0)
in
[to.type]{[&to.traits.eltype](tmp.ptr), tmp.nbytes, tmp.alloc}
end
end
else
--passing by reference
terralib.ext.addmissing.__forward(from.type)
return quote
--var blk = exp invokes __copy, so we turn exp into an rvalue such
--that __copy is not called
var blk = exp:__forward()
err.assert(blk:size_in_bytes() % [to.elsize] == 0)
in
[&to.type](blk)
end
end
end --__cast
function block.metamethods.__staticinitialize(self)
--add base functionality
Base(block, T, options)
--setters and getters
block.methods.get = terra(self : &block, i : size_t)
err.assert(i < self:size())
return self.ptr[i]
end
block.methods.set = terra(self : &block, i : size_t, v : T)
err.assert(i < self:size())
self.ptr[i] = v
end
block.metamethods.__apply = macro(function(self, i)
return quote
err.assert(i < self:size())
in
self.ptr[i]
end
end)
block.staticmethods.frombuffer = terra(size: size_t, ptr: &T)
var nbytes = size * sizeof(T)
var b: block
b.ptr = ptr
b.nbytes = nbytes
b.alloc.data = nil
b.alloc.tab = nil
return b
end
--iterator - behaves like a pointer and can be passed
--around like a value, convenient for use in ranges.
local struct iterator{
parent : &block
ptr : &T
}
terra block:getiterator()
return iterator{self, self.ptr}
end
terra iterator:getvalue()
return @self.ptr
end
terra iterator:next()
self.ptr = self.ptr + 1
end
terra iterator:isvalid()
return (self.ptr - self.parent.ptr) * [block.elsize] < self.parent.nbytes
end
block.iterator = iterator
range.Base(block, iterator)
terra block:reallocate(size : size_t)
self.alloc:__allocators_best_friend(self, sizeof(T), size)
end
--declaring __dtor for use in implementation below
terra block.methods.__dtor :: {&block} -> {}
--implementation __dtor
--ToDo: change recursion to a loop
local terra __dtor(self : &block)
--insert metamethods.__dtor if defined, which is used to introduce
--side effects (e.g. counting number of calls for the purpose of testing)
escape
if block.metamethods and block.metamethods.__dtor then
emit quote
[block.metamethods.__dtor](self)
end
end
end
--return if block is empty
if self:isempty() then
return
end
--initialize block and return if block borrows a resource
if self:borrows_resource() then
self:__init()
return
end
--first destroy other memory block resources pointed to by self.ptr
--ToDo: change recursion into a loop
escape
if ismanaged{type=T,method="__dtor"} then
emit quote
var ptr = self.ptr
for i = 0, self:size() do
ptr:__dtor()
ptr = ptr + 1
end
end
end
end
--free current memory block resources
self.alloc:__allocators_best_friend(self, 0, 0)
end
--call implementation
terra block.methods.__dtor(self : &block)
__dtor(self)
end
end --__staticinitialize
return block
end)
return {
block = block,
SmartBlock = SmartBlock
}