1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // memory.cfa -- Memory Management Tools for CFA |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Tue Jun 2 16:48:00 2020 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Tue Jun 3 12:30:00 2020 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include "memory.hfa" |
---|
17 | #include "stdlib.hfa" |
---|
18 | |
---|
19 | // Internal data object. |
---|
20 | forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); }) |
---|
21 | void ?{}(counter_data(T) & this, Args args) { |
---|
22 | (this.counter){1}; |
---|
23 | (this.object){args}; |
---|
24 | } |
---|
25 | |
---|
26 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
27 | void ^?{}(counter_data(T) & this) { |
---|
28 | assert(0 == this.counter); |
---|
29 | ^(this.object){}; |
---|
30 | } |
---|
31 | |
---|
32 | // This is one of many pointers keeping this alive. |
---|
33 | forall(dtype T | sized(T)) |
---|
34 | void ?{}(counter_ptr(T) & this) { |
---|
35 | this.data = 0p; |
---|
36 | } |
---|
37 | |
---|
38 | forall(dtype T | sized(T)) |
---|
39 | void ?{}(counter_ptr(T) & this, zero_t) { |
---|
40 | this.data = 0p; |
---|
41 | } |
---|
42 | |
---|
43 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
44 | static void internal_decrement(counter_ptr(T) & this) { |
---|
45 | if (this.data && 0 == --this.data->counter) { |
---|
46 | delete(this.data); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | forall(dtype T | sized(T)) |
---|
51 | static void internal_copy(counter_ptr(T) & this, counter_ptr(T) & that) { |
---|
52 | this.data = that.data; |
---|
53 | if (this.data) { |
---|
54 | ++this.data->counter; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
59 | void ?{}(counter_ptr(T) & this, counter_ptr(T) that) { |
---|
60 | // `that` is a copy but it should have neither a constructor |
---|
61 | // nor destructor run on it so it shouldn't need adjustment. |
---|
62 | internal_decrement(this); |
---|
63 | internal_copy(this, that); |
---|
64 | } |
---|
65 | |
---|
66 | forall(dtype T | sized(T), ttype Args | { void ?{}(T&, Args); }) |
---|
67 | void ?{}(counter_ptr(T) & this, Args args) { |
---|
68 | this.data = (counter_data(T)*)new(args); |
---|
69 | } |
---|
70 | |
---|
71 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
72 | void ^?{}(counter_ptr(T) & this) { |
---|
73 | internal_decrement(this); |
---|
74 | } |
---|
75 | |
---|
76 | forall(dtype T | sized(T)) |
---|
77 | T & *?(counter_ptr(T) & this) { |
---|
78 | return *((this.data) ? &this.data->object : 0p); |
---|
79 | } |
---|
80 | |
---|
81 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
82 | void ?=?(counter_ptr(T) & this, counter_ptr(T) that) { |
---|
83 | if (this.data != that.data) { |
---|
84 | internal_decrement(this); |
---|
85 | internal_copy(this, that); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | forall(dtype T | sized(T) | { void ^?{}(T &); }) |
---|
90 | void ?=?(counter_ptr(T) & this, zero_t) { |
---|
91 | internal_decrement(this); |
---|
92 | this.data = 0p; |
---|
93 | } |
---|
94 | |
---|
95 | forall(dtype T | sized(T)) |
---|
96 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
97 | return this.data == that.data; |
---|
98 | } |
---|
99 | |
---|
100 | forall(dtype T | sized(T)) |
---|
101 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
102 | return !?==?(this, that); |
---|
103 | } |
---|
104 | |
---|
105 | forall(dtype T | sized(T)) |
---|
106 | int ?==?(counter_ptr(T) const & this, zero_t) { |
---|
107 | return this.data == 0; |
---|
108 | } |
---|
109 | |
---|
110 | forall(dtype T | sized(T)) |
---|
111 | int ?!=?(counter_ptr(T) const & this, zero_t) { |
---|
112 | return !?==?(this, (zero_t)0); |
---|
113 | } |
---|
114 | |
---|
115 | // This is the only pointer that keeps this alive. |
---|
116 | forall(dtype T) |
---|
117 | void ?{}(unique_ptr(T) & this) { |
---|
118 | this.data = 0p; |
---|
119 | } |
---|
120 | |
---|
121 | forall(dtype T) |
---|
122 | void ?{}(unique_ptr(T) & this, zero_t) { |
---|
123 | this.data = 0p; |
---|
124 | } |
---|
125 | |
---|
126 | forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); }) |
---|
127 | void ?{}(unique_ptr(T) & this, Args args) { |
---|
128 | this.data = (T *)new(args); |
---|
129 | } |
---|
130 | |
---|
131 | forall(dtype T | { void ^?{}(T &); }) |
---|
132 | void ^?{}(unique_ptr(T) & this) { |
---|
133 | delete(this.data); |
---|
134 | } |
---|
135 | |
---|
136 | forall(dtype T) |
---|
137 | T & *?(unique_ptr(T) & this) { |
---|
138 | return *this.data; |
---|
139 | } |
---|
140 | |
---|
141 | forall(dtype T | { void ^?{}(T &); }) |
---|
142 | void ?=?(unique_ptr(T) & this, zero_t) { |
---|
143 | delete(this.data); |
---|
144 | this.data = 0p; |
---|
145 | } |
---|
146 | |
---|
147 | forall(dtype T | { void ^?{}(T &); }) |
---|
148 | void move(unique_ptr(T) & this, unique_ptr(T) & that) { |
---|
149 | delete(this.data); |
---|
150 | this.data = that.data; |
---|
151 | that.data = 0p; |
---|
152 | } |
---|
153 | |
---|
154 | forall(dtype T) |
---|
155 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
156 | return this.data == that.data; |
---|
157 | } |
---|
158 | |
---|
159 | forall(dtype T) |
---|
160 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
161 | return !?==?(this, that); |
---|
162 | } |
---|
163 | |
---|
164 | forall(dtype T) |
---|
165 | int ?==?(unique_ptr(T) const & this, zero_t) { |
---|
166 | return this.data == 0; |
---|
167 | } |
---|
168 | |
---|
169 | forall(dtype T) |
---|
170 | int ?!=?(unique_ptr(T) const & this, zero_t) { |
---|
171 | return !?==?(this, (zero_t)0); |
---|
172 | } |
---|