[aabb846] | 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 |
---|
[8be729f] | 12 | // Last Modified On : Mon Feb 1 16:10:00 2021 |
---|
| 13 | // Update Count : 1 |
---|
[aabb846] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "memory.hfa" |
---|
| 17 | #include "stdlib.hfa" |
---|
| 18 | |
---|
| 19 | // Internal data object. |
---|
[fd54fef] | 20 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) |
---|
[aabb846] | 21 | void ?{}(counter_data(T) & this, Args args) { |
---|
| 22 | (this.counter){1}; |
---|
| 23 | (this.object){args}; |
---|
| 24 | } |
---|
| 25 | |
---|
[fd54fef] | 26 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
[aabb846] | 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. |
---|
[fd54fef] | 33 | forall(T & | sized(T)) |
---|
[aabb846] | 34 | void ?{}(counter_ptr(T) & this) { |
---|
| 35 | this.data = 0p; |
---|
| 36 | } |
---|
| 37 | |
---|
[fd54fef] | 38 | forall(T & | sized(T)) |
---|
[aabb846] | 39 | void ?{}(counter_ptr(T) & this, zero_t) { |
---|
| 40 | this.data = 0p; |
---|
| 41 | } |
---|
| 42 | |
---|
[fd54fef] | 43 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
[aabb846] | 44 | static void internal_decrement(counter_ptr(T) & this) { |
---|
| 45 | if (this.data && 0 == --this.data->counter) { |
---|
| 46 | delete(this.data); |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
[fd54fef] | 50 | forall(T & | sized(T)) |
---|
[aabb846] | 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 | |
---|
[8be729f] | 58 | forall(T & | sized(T)) |
---|
[aabb846] | 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 | |
---|
[fd54fef] | 65 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); }) |
---|
[aabb846] | 66 | void ?{}(counter_ptr(T) & this, Args args) { |
---|
[8be729f] | 67 | this.data = malloc(); |
---|
| 68 | this.data->counter = 1; |
---|
| 69 | (this.data->object){args}; |
---|
[aabb846] | 70 | } |
---|
| 71 | |
---|
[fd54fef] | 72 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
[aabb846] | 73 | void ^?{}(counter_ptr(T) & this) { |
---|
| 74 | internal_decrement(this); |
---|
| 75 | } |
---|
| 76 | |
---|
[fd54fef] | 77 | forall(T & | sized(T)) |
---|
[aabb846] | 78 | T & *?(counter_ptr(T) & this) { |
---|
| 79 | return *((this.data) ? &this.data->object : 0p); |
---|
| 80 | } |
---|
| 81 | |
---|
[fd54fef] | 82 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
[aabb846] | 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 | |
---|
[fd54fef] | 90 | forall(T & | sized(T) | { void ^?{}(T &); }) |
---|
[aabb846] | 91 | void ?=?(counter_ptr(T) & this, zero_t) { |
---|
| 92 | internal_decrement(this); |
---|
| 93 | this.data = 0p; |
---|
| 94 | } |
---|
| 95 | |
---|
[fd54fef] | 96 | forall(T & | sized(T)) |
---|
[aabb846] | 97 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
| 98 | return this.data == that.data; |
---|
| 99 | } |
---|
| 100 | |
---|
[fd54fef] | 101 | forall(T & | sized(T)) |
---|
[aabb846] | 102 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) { |
---|
| 103 | return !?==?(this, that); |
---|
| 104 | } |
---|
| 105 | |
---|
[fd54fef] | 106 | forall(T & | sized(T)) |
---|
[aabb846] | 107 | int ?==?(counter_ptr(T) const & this, zero_t) { |
---|
| 108 | return this.data == 0; |
---|
| 109 | } |
---|
| 110 | |
---|
[fd54fef] | 111 | forall(T & | sized(T)) |
---|
[aabb846] | 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. |
---|
[fd54fef] | 117 | forall(T &) |
---|
[aabb846] | 118 | void ?{}(unique_ptr(T) & this) { |
---|
| 119 | this.data = 0p; |
---|
| 120 | } |
---|
| 121 | |
---|
[fd54fef] | 122 | forall(T &) |
---|
[aabb846] | 123 | void ?{}(unique_ptr(T) & this, zero_t) { |
---|
| 124 | this.data = 0p; |
---|
| 125 | } |
---|
| 126 | |
---|
[fd54fef] | 127 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); }) |
---|
[aabb846] | 128 | void ?{}(unique_ptr(T) & this, Args args) { |
---|
[8be729f] | 129 | this.data = malloc(); |
---|
| 130 | (*this.data){args}; |
---|
[aabb846] | 131 | } |
---|
| 132 | |
---|
[fd54fef] | 133 | forall(T & | { void ^?{}(T &); }) |
---|
[aabb846] | 134 | void ^?{}(unique_ptr(T) & this) { |
---|
| 135 | delete(this.data); |
---|
| 136 | } |
---|
| 137 | |
---|
[fd54fef] | 138 | forall(T &) |
---|
[aabb846] | 139 | T & *?(unique_ptr(T) & this) { |
---|
| 140 | return *this.data; |
---|
| 141 | } |
---|
| 142 | |
---|
[fd54fef] | 143 | forall(T & | { void ^?{}(T &); }) |
---|
[aabb846] | 144 | void ?=?(unique_ptr(T) & this, zero_t) { |
---|
| 145 | delete(this.data); |
---|
| 146 | this.data = 0p; |
---|
| 147 | } |
---|
| 148 | |
---|
[fd54fef] | 149 | forall(T & | { void ^?{}(T &); }) |
---|
[aabb846] | 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 | |
---|
[1341ce1] | 156 | forall(T &) |
---|
| 157 | T * release(unique_ptr(T) & this) { |
---|
| 158 | T * data = this.data; |
---|
| 159 | this.data = 0p; |
---|
| 160 | return data; |
---|
| 161 | } |
---|
| 162 | |
---|
[fd54fef] | 163 | forall(T &) |
---|
[aabb846] | 164 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
| 165 | return this.data == that.data; |
---|
| 166 | } |
---|
| 167 | |
---|
[fd54fef] | 168 | forall(T &) |
---|
[aabb846] | 169 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) { |
---|
| 170 | return !?==?(this, that); |
---|
| 171 | } |
---|
| 172 | |
---|
[fd54fef] | 173 | forall(T &) |
---|
[aabb846] | 174 | int ?==?(unique_ptr(T) const & this, zero_t) { |
---|
| 175 | return this.data == 0; |
---|
| 176 | } |
---|
| 177 | |
---|
[fd54fef] | 178 | forall(T &) |
---|
[aabb846] | 179 | int ?!=?(unique_ptr(T) const & this, zero_t) { |
---|
| 180 | return !?==?(this, (zero_t)0); |
---|
| 181 | } |
---|