-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFreeList.jl
67 lines (57 loc) · 1.46 KB
/
FreeList.jl
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
type FreeListEntry
array::AFArray
isResult::Bool
end
FreeListEntry(arr) = FreeListEntry(arr, false)
type FreeList <: AFImpl
list::Vector{Vector{FreeListEntry}}
end
FreeList() = FreeList(Vector{Vector{FreeListEntry}}())
current(fl::FreeList) =
length(fl.list) > 0 ?
Nullable{Vector{FreeListEntry}}(last(fl.list)) :
Nullable{Vector{FreeListEntry}}()
function previous(fl::FreeList)
len = length(fl.list)
len > 1 ?
Nullable{Vector{FreeListEntry}}(fl.list[len - 1]) :
Nullable{Vector{FreeListEntry}}()
end
newScope!(fl::FreeList) = push!(fl.list, Vector{FreeListEntry}())
function register!(fl::FreeList, arr::AFArray)
curr = current(fl)
if !isnull(curr)
push!(get(curr), FreeListEntry(arr))
return true
end
false
end
raiseNoScope() = error("There is no active scope.")
function markResult!(fl::FreeList, arr::AFArray)
currN = current(fl)
if !isnull(currN)
curr = get(currN)
idx = findfirst(x -> x.array == arr, curr)
idx == 0 && error("Array isn't registered in the current scope.")
curr[idx].isResult = true
# prev = previous(fl)
# if !isnull(prev)
# push!(get(prev), FreeListEntry(arr))
# end
arr
else
raiseNoScope()
end
end
function endScope!(fl::FreeList)
curr = current(fl)
if !isnull(curr)
curr = get(curr)
for entry in filter(x -> !x.isResult, curr)
release!(entry.array)
end
pop!(fl.list)
else
raiseNoScope()
end
end