[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
|
---|
| 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.
|
---|
[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 |
|
---|
[fd54fef] | 58 | forall(T & | sized(T) | { void ^?{}(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_decrement(this);
|
---|
| 63 | internal_copy(this, that);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[fd54fef] | 66 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
|
---|
[aabb846] | 67 | void ?{}(counter_ptr(T) & this, Args args) {
|
---|
[09da82d] | 68 | this.data = (counter_data(T)*)new(args);
|
---|
[aabb846] | 69 | }
|
---|
| 70 |
|
---|
[fd54fef] | 71 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 72 | void ^?{}(counter_ptr(T) & this) {
|
---|
| 73 | internal_decrement(this);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[fd54fef] | 76 | forall(T & | sized(T))
|
---|
[aabb846] | 77 | T & *?(counter_ptr(T) & this) {
|
---|
| 78 | return *((this.data) ? &this.data->object : 0p);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[fd54fef] | 81 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 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 |
|
---|
[fd54fef] | 89 | forall(T & | sized(T) | { void ^?{}(T &); })
|
---|
[aabb846] | 90 | void ?=?(counter_ptr(T) & this, zero_t) {
|
---|
| 91 | internal_decrement(this);
|
---|
| 92 | this.data = 0p;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[fd54fef] | 95 | forall(T & | sized(T))
|
---|
[aabb846] | 96 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
|
---|
| 97 | return this.data == that.data;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[fd54fef] | 100 | forall(T & | sized(T))
|
---|
[aabb846] | 101 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
|
---|
| 102 | return !?==?(this, that);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[fd54fef] | 105 | forall(T & | sized(T))
|
---|
[aabb846] | 106 | int ?==?(counter_ptr(T) const & this, zero_t) {
|
---|
| 107 | return this.data == 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[fd54fef] | 110 | forall(T & | sized(T))
|
---|
[aabb846] | 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.
|
---|
[fd54fef] | 116 | forall(T &)
|
---|
[aabb846] | 117 | void ?{}(unique_ptr(T) & this) {
|
---|
| 118 | this.data = 0p;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[fd54fef] | 121 | forall(T &)
|
---|
[aabb846] | 122 | void ?{}(unique_ptr(T) & this, zero_t) {
|
---|
| 123 | this.data = 0p;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[fd54fef] | 126 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
|
---|
[aabb846] | 127 | void ?{}(unique_ptr(T) & this, Args args) {
|
---|
[09da82d] | 128 | this.data = (T *)new(args);
|
---|
[aabb846] | 129 | }
|
---|
| 130 |
|
---|
[fd54fef] | 131 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 132 | void ^?{}(unique_ptr(T) & this) {
|
---|
| 133 | delete(this.data);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[fd54fef] | 136 | forall(T &)
|
---|
[aabb846] | 137 | T & *?(unique_ptr(T) & this) {
|
---|
| 138 | return *this.data;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[fd54fef] | 141 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 142 | void ?=?(unique_ptr(T) & this, zero_t) {
|
---|
| 143 | delete(this.data);
|
---|
| 144 | this.data = 0p;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[fd54fef] | 147 | forall(T & | { void ^?{}(T &); })
|
---|
[aabb846] | 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 |
|
---|
[fd54fef] | 154 | forall(T &)
|
---|
[aabb846] | 155 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
|
---|
| 156 | return this.data == that.data;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[fd54fef] | 159 | forall(T &)
|
---|
[aabb846] | 160 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
|
---|
| 161 | return !?==?(this, that);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[fd54fef] | 164 | forall(T &)
|
---|
[aabb846] | 165 | int ?==?(unique_ptr(T) const & this, zero_t) {
|
---|
| 166 | return this.data == 0;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[fd54fef] | 169 | forall(T &)
|
---|
[aabb846] | 170 | int ?!=?(unique_ptr(T) const & this, zero_t) {
|
---|
| 171 | return !?==?(this, (zero_t)0);
|
---|
| 172 | }
|
---|