[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 |
|
---|
[789f279] | 19 | #pragma GCC visibility push(default)
|
---|
| 20 |
|
---|
[aabb846] | 21 | // Internal data object.
|
---|
[fd54fef] | 22 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
|
---|
[aabb846] | 23 | void ?{}(counter_data(T) & this, Args args) {
|
---|
| 24 | (this.counter){1};
|
---|
| 25 | (this.object){args};
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[fd54fef] | 28 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 29 | void ^?{}(counter_data(T) & this) {
|
---|
| 30 | assert(0 == this.counter);
|
---|
| 31 | ^(this.object){};
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | // This is one of many pointers keeping this alive.
|
---|
[fd54fef] | 35 | forall(T & | sized(T))
|
---|
[aabb846] | 36 | void ?{}(counter_ptr(T) & this) {
|
---|
| 37 | this.data = 0p;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[fd54fef] | 40 | forall(T & | sized(T))
|
---|
[aabb846] | 41 | void ?{}(counter_ptr(T) & this, zero_t) {
|
---|
| 42 | this.data = 0p;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[fd54fef] | 45 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 46 | static void internal_decrement(counter_ptr(T) & this) {
|
---|
| 47 | if (this.data && 0 == --this.data->counter) {
|
---|
| 48 | delete(this.data);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[fd54fef] | 52 | forall(T & | sized(T))
|
---|
[aabb846] | 53 | static void internal_copy(counter_ptr(T) & this, counter_ptr(T) & that) {
|
---|
| 54 | this.data = that.data;
|
---|
| 55 | if (this.data) {
|
---|
| 56 | ++this.data->counter;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[8be729f] | 60 | forall(T & | sized(T))
|
---|
[aabb846] | 61 | void ?{}(counter_ptr(T) & this, counter_ptr(T) that) {
|
---|
| 62 | // `that` is a copy but it should have neither a constructor
|
---|
| 63 | // nor destructor run on it so it shouldn't need adjustment.
|
---|
| 64 | internal_copy(this, that);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[fd54fef] | 67 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
|
---|
[aabb846] | 68 | void ?{}(counter_ptr(T) & this, Args args) {
|
---|
[8be729f] | 69 | this.data = malloc();
|
---|
| 70 | this.data->counter = 1;
|
---|
| 71 | (this.data->object){args};
|
---|
[aabb846] | 72 | }
|
---|
| 73 |
|
---|
[fd54fef] | 74 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 75 | void ^?{}(counter_ptr(T) & this) {
|
---|
| 76 | internal_decrement(this);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[fd54fef] | 79 | forall(T & | sized(T))
|
---|
[aabb846] | 80 | T & *?(counter_ptr(T) & this) {
|
---|
| 81 | return *((this.data) ? &this.data->object : 0p);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[fd54fef] | 84 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 85 | void ?=?(counter_ptr(T) & this, counter_ptr(T) that) {
|
---|
| 86 | if (this.data != that.data) {
|
---|
| 87 | internal_decrement(this);
|
---|
| 88 | internal_copy(this, that);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[fd54fef] | 92 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 93 | void ?=?(counter_ptr(T) & this, zero_t) {
|
---|
| 94 | internal_decrement(this);
|
---|
| 95 | this.data = 0p;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[fd54fef] | 98 | forall(T & | sized(T))
|
---|
[aabb846] | 99 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
|
---|
| 100 | return this.data == that.data;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[fd54fef] | 103 | forall(T & | sized(T))
|
---|
[aabb846] | 104 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
|
---|
| 105 | return !?==?(this, that);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[fd54fef] | 108 | forall(T & | sized(T))
|
---|
[aabb846] | 109 | int ?==?(counter_ptr(T) const & this, zero_t) {
|
---|
| 110 | return this.data == 0;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[fd54fef] | 113 | forall(T & | sized(T))
|
---|
[aabb846] | 114 | int ?!=?(counter_ptr(T) const & this, zero_t) {
|
---|
| 115 | return !?==?(this, (zero_t)0);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | // This is the only pointer that keeps this alive.
|
---|
[fd54fef] | 119 | forall(T &)
|
---|
[aabb846] | 120 | void ?{}(unique_ptr(T) & this) {
|
---|
| 121 | this.data = 0p;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[fd54fef] | 124 | forall(T &)
|
---|
[aabb846] | 125 | void ?{}(unique_ptr(T) & this, zero_t) {
|
---|
| 126 | this.data = 0p;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[fd54fef] | 129 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
|
---|
[aabb846] | 130 | void ?{}(unique_ptr(T) & this, Args args) {
|
---|
[8be729f] | 131 | this.data = malloc();
|
---|
| 132 | (*this.data){args};
|
---|
[aabb846] | 133 | }
|
---|
| 134 |
|
---|
[fd54fef] | 135 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 136 | void ^?{}(unique_ptr(T) & this) {
|
---|
| 137 | delete(this.data);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[fd54fef] | 140 | forall(T &)
|
---|
[aabb846] | 141 | T & *?(unique_ptr(T) & this) {
|
---|
| 142 | return *this.data;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[fd54fef] | 145 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 146 | void ?=?(unique_ptr(T) & this, zero_t) {
|
---|
| 147 | delete(this.data);
|
---|
| 148 | this.data = 0p;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[fd54fef] | 151 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 152 | void move(unique_ptr(T) & this, unique_ptr(T) & that) {
|
---|
| 153 | delete(this.data);
|
---|
| 154 | this.data = that.data;
|
---|
| 155 | that.data = 0p;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1341ce1] | 158 | forall(T &)
|
---|
| 159 | T * release(unique_ptr(T) & this) {
|
---|
| 160 | T * data = this.data;
|
---|
| 161 | this.data = 0p;
|
---|
| 162 | return data;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[fd54fef] | 165 | forall(T &)
|
---|
[aabb846] | 166 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
|
---|
| 167 | return this.data == that.data;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[fd54fef] | 170 | forall(T &)
|
---|
[aabb846] | 171 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
|
---|
| 172 | return !?==?(this, that);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[fd54fef] | 175 | forall(T &)
|
---|
[aabb846] | 176 | int ?==?(unique_ptr(T) const & this, zero_t) {
|
---|
| 177 | return this.data == 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[fd54fef] | 180 | forall(T &)
|
---|
[aabb846] | 181 | int ?!=?(unique_ptr(T) const & this, zero_t) {
|
---|
| 182 | return !?==?(this, (zero_t)0);
|
---|
| 183 | }
|
---|