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_decrement(this); |
---|
63 | internal_copy(this, that); |
---|
64 | } |
---|
65 | |
---|
66 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); }) |
---|
67 | void ?{}(counter_ptr(T) & this, Args args) { |
---|
68 | this.data = malloc(); |
---|
69 | this.data->counter = 1; |
---|
70 | (this.data->object){args}; |
---|
71 | } |
---|
72 | |
---|
73 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
74 | void ^?{}(counter_ptr(T) & this) { |
---|
75 | internal_decrement(this); |
---|
76 | } |
---|
77 | |
---|
78 | forall(T & | sized(T)) |
---|
79 | T & *?(counter_ptr(T) & this) { |
---|
80 | return *((this.data) ? &this.data->object : 0p); |
---|
81 | } |
---|
82 | |
---|
83 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
84 | void ?=?(counter_ptr(T) & this, counter_ptr(T) that) { |
---|
85 | if (this.data != that.data) { |
---|
86 | internal_decrement(this); |
---|
87 | internal_copy(this, that); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
92 | void ?=?(counter_ptr(T) & this, zero_t) { |
---|
93 | internal_decrement(this); |
---|
94 | this.data = 0p; |
---|
95 | } |
---|
96 | |
---|
97 | forall(T & | sized(T)) |
---|
98 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
99 | return this.data == that.data; |
---|
100 | } |
---|
101 | |
---|
102 | forall(T & | sized(T)) |
---|
103 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
104 | return !?==?(this, that); |
---|
105 | } |
---|
106 | |
---|
107 | forall(T & | sized(T)) |
---|
108 | int ?==?(counter_ptr(T) const & this, zero_t) { |
---|
109 | return this.data == 0; |
---|
110 | } |
---|
111 | |
---|
112 | forall(T & | sized(T)) |
---|
113 | int ?!=?(counter_ptr(T) const & this, zero_t) { |
---|
114 | return !?==?(this, (zero_t)0); |
---|
115 | } |
---|
116 | |
---|
117 | // This is the only pointer that keeps this alive. |
---|
118 | forall(T &) |
---|
119 | void ?{}(unique_ptr(T) & this) { |
---|
120 | this.data = 0p; |
---|
121 | } |
---|
122 | |
---|
123 | forall(T &) |
---|
124 | void ?{}(unique_ptr(T) & this, zero_t) { |
---|
125 | this.data = 0p; |
---|
126 | } |
---|
127 | |
---|
128 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) |
---|
129 | void ?{}(unique_ptr(T) & this, Args args) { |
---|
130 | this.data = malloc(); |
---|
131 | (*this.data){args}; |
---|
132 | } |
---|
133 | |
---|
134 | forall(T & | { void ^?{}(T &); }) |
---|
135 | void ^?{}(unique_ptr(T) & this) { |
---|
136 | delete(this.data); |
---|
137 | } |
---|
138 | |
---|
139 | forall(T &) |
---|
140 | T & *?(unique_ptr(T) & this) { |
---|
141 | return *this.data; |
---|
142 | } |
---|
143 | |
---|
144 | forall(T & | { void ^?{}(T &); }) |
---|
145 | void ?=?(unique_ptr(T) & this, zero_t) { |
---|
146 | delete(this.data); |
---|
147 | this.data = 0p; |
---|
148 | } |
---|
149 | |
---|
150 | forall(T & | { void ^?{}(T &); }) |
---|
151 | void move(unique_ptr(T) & this, unique_ptr(T) & that) { |
---|
152 | delete(this.data); |
---|
153 | this.data = that.data; |
---|
154 | that.data = 0p; |
---|
155 | } |
---|
156 | |
---|
157 | forall(T &) |
---|
158 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
159 | return this.data == that.data; |
---|
160 | } |
---|
161 | |
---|
162 | forall(T &) |
---|
163 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
164 | return !?==?(this, that); |
---|
165 | } |
---|
166 | |
---|
167 | forall(T &) |
---|
168 | int ?==?(unique_ptr(T) const & this, zero_t) { |
---|
169 | return this.data == 0; |
---|
170 | } |
---|
171 | |
---|
172 | forall(T &) |
---|
173 | int ?!=?(unique_ptr(T) const & this, zero_t) { |
---|
174 | return !?==?(this, (zero_t)0); |
---|
175 | } |
---|