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 : Mon Feb 1 16:10:00 2021 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "memory.hfa" |
---|
17 | #include "stdlib.hfa" |
---|
18 | |
---|
19 | // Internal data object. |
---|
20 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) |
---|
21 | void ?{}(counter_data(T) & this, Args args) { |
---|
22 | (this.counter){1}; |
---|
23 | (this.object){args}; |
---|
24 | } |
---|
25 | |
---|
26 | forall(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(T & | sized(T)) |
---|
34 | void ?{}(counter_ptr(T) & this) { |
---|
35 | this.data = 0p; |
---|
36 | } |
---|
37 | |
---|
38 | forall(T & | sized(T)) |
---|
39 | void ?{}(counter_ptr(T) & this, zero_t) { |
---|
40 | this.data = 0p; |
---|
41 | } |
---|
42 | |
---|
43 | forall(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(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(T & | sized(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_copy(this, that); |
---|
63 | } |
---|
64 | |
---|
65 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); }) |
---|
66 | void ?{}(counter_ptr(T) & this, Args args) { |
---|
67 | this.data = malloc(); |
---|
68 | this.data->counter = 1; |
---|
69 | (this.data->object){args}; |
---|
70 | } |
---|
71 | |
---|
72 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
73 | void ^?{}(counter_ptr(T) & this) { |
---|
74 | internal_decrement(this); |
---|
75 | } |
---|
76 | |
---|
77 | forall(T & | sized(T)) |
---|
78 | T & *?(counter_ptr(T) & this) { |
---|
79 | return *((this.data) ? &this.data->object : 0p); |
---|
80 | } |
---|
81 | |
---|
82 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
83 | void ?=?(counter_ptr(T) & this, counter_ptr(T) that) { |
---|
84 | if (this.data != that.data) { |
---|
85 | internal_decrement(this); |
---|
86 | internal_copy(this, that); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
91 | void ?=?(counter_ptr(T) & this, zero_t) { |
---|
92 | internal_decrement(this); |
---|
93 | this.data = 0p; |
---|
94 | } |
---|
95 | |
---|
96 | forall(T & | sized(T)) |
---|
97 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
98 | return this.data == that.data; |
---|
99 | } |
---|
100 | |
---|
101 | forall(T & | sized(T)) |
---|
102 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
103 | return !?==?(this, that); |
---|
104 | } |
---|
105 | |
---|
106 | forall(T & | sized(T)) |
---|
107 | int ?==?(counter_ptr(T) const & this, zero_t) { |
---|
108 | return this.data == 0; |
---|
109 | } |
---|
110 | |
---|
111 | forall(T & | sized(T)) |
---|
112 | int ?!=?(counter_ptr(T) const & this, zero_t) { |
---|
113 | return !?==?(this, (zero_t)0); |
---|
114 | } |
---|
115 | |
---|
116 | // This is the only pointer that keeps this alive. |
---|
117 | forall(T &) |
---|
118 | void ?{}(unique_ptr(T) & this) { |
---|
119 | this.data = 0p; |
---|
120 | } |
---|
121 | |
---|
122 | forall(T &) |
---|
123 | void ?{}(unique_ptr(T) & this, zero_t) { |
---|
124 | this.data = 0p; |
---|
125 | } |
---|
126 | |
---|
127 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) |
---|
128 | void ?{}(unique_ptr(T) & this, Args args) { |
---|
129 | this.data = malloc(); |
---|
130 | (*this.data){args}; |
---|
131 | } |
---|
132 | |
---|
133 | forall(T & | { void ^?{}(T &); }) |
---|
134 | void ^?{}(unique_ptr(T) & this) { |
---|
135 | delete(this.data); |
---|
136 | } |
---|
137 | |
---|
138 | forall(T &) |
---|
139 | T & *?(unique_ptr(T) & this) { |
---|
140 | return *this.data; |
---|
141 | } |
---|
142 | |
---|
143 | forall(T & | { void ^?{}(T &); }) |
---|
144 | void ?=?(unique_ptr(T) & this, zero_t) { |
---|
145 | delete(this.data); |
---|
146 | this.data = 0p; |
---|
147 | } |
---|
148 | |
---|
149 | forall(T & | { void ^?{}(T &); }) |
---|
150 | void move(unique_ptr(T) & this, unique_ptr(T) & that) { |
---|
151 | delete(this.data); |
---|
152 | this.data = that.data; |
---|
153 | that.data = 0p; |
---|
154 | } |
---|
155 | |
---|
156 | forall(T &) |
---|
157 | T * release(unique_ptr(T) & this) { |
---|
158 | T * data = this.data; |
---|
159 | this.data = 0p; |
---|
160 | return data; |
---|
161 | } |
---|
162 | |
---|
163 | forall(T &) |
---|
164 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
165 | return this.data == that.data; |
---|
166 | } |
---|
167 | |
---|
168 | forall(T &) |
---|
169 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
170 | return !?==?(this, that); |
---|
171 | } |
---|
172 | |
---|
173 | forall(T &) |
---|
174 | int ?==?(unique_ptr(T) const & this, zero_t) { |
---|
175 | return this.data == 0; |
---|
176 | } |
---|
177 | |
---|
178 | forall(T &) |
---|
179 | int ?!=?(unique_ptr(T) const & this, zero_t) { |
---|
180 | return !?==?(this, (zero_t)0); |
---|
181 | } |
---|