-
Notifications
You must be signed in to change notification settings - Fork 1
/
defer.t
54 lines (46 loc) · 1.05 KB
/
defer.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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local alloc = require("alloc")
local io = terralib.includec("stdio.h")
USE_FREE = false
local new = macro(function(A)
return quote
var x = A:alloc(10)
if USE_FREE then
defer A:free(x)
else
defer io.printf("Empty free in new\n")
end
in
x
end
end)
local move = macro(function(x, A)
return quote
var y = @x
@x = nil
if USE_FREE then
defer A:free(y)
else
defer io.printf("Empty free in move\n")
end
in
y
end
end)
local terra foo(A: alloc.Default)
io.printf("In function foo\n")
var x = [&int8](new(A))
x[0] = @'t'
return move(&x, A)
end
terra main()
var A: alloc.Default
var x = foo(A)
io.printf("Outside function foo\n")
io.printf("%c\n", x[0])
end
main()
terralib.saveobj("defer.o", {main = main}, {"-g"})