| 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 | #pragma GCC visibility push(default)
 | 
|---|
| 20 | 
 | 
|---|
| 21 | // Internal data object.
 | 
|---|
| 22 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
 | 
|---|
| 23 | void ?{}(counter_data(T) & this, Args args) {
 | 
|---|
| 24 |         (this.counter){1};
 | 
|---|
| 25 |         (this.object){args};
 | 
|---|
| 26 | }
 | 
|---|
| 27 | 
 | 
|---|
| 28 | forall(T & | sized(T) | { void ^?{}(T &); })
 | 
|---|
| 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.
 | 
|---|
| 35 | forall(T & | sized(T))
 | 
|---|
| 36 | void ?{}(counter_ptr(T) & this) {
 | 
|---|
| 37 |         this.data = 0p;
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | forall(T & | sized(T))
 | 
|---|
| 41 | void ?{}(counter_ptr(T) & this, zero_t) {
 | 
|---|
| 42 |         this.data = 0p;
 | 
|---|
| 43 | }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | forall(T & | sized(T) | { void ^?{}(T &); })
 | 
|---|
| 46 | static void internal_decrement(counter_ptr(T) & this) {
 | 
|---|
| 47 |         if (this.data && 0 == --this.data->counter) {
 | 
|---|
| 48 |                 delete(this.data);
 | 
|---|
| 49 |         }
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | forall(T & | sized(T))
 | 
|---|
| 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 | 
 | 
|---|
| 60 | forall(T & | sized(T))
 | 
|---|
| 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 | 
 | 
|---|
| 67 | forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
 | 
|---|
| 68 | void ?{}(counter_ptr(T) & this, Args args) {
 | 
|---|
| 69 |         this.data = malloc();
 | 
|---|
| 70 |         this.data->counter = 1;
 | 
|---|
| 71 |         (this.data->object){args};
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | forall(T & | sized(T) | { void ^?{}(T &); })
 | 
|---|
| 75 | void ^?{}(counter_ptr(T) & this) {
 | 
|---|
| 76 |         internal_decrement(this);
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | forall(T & | sized(T))
 | 
|---|
| 80 | T & *?(counter_ptr(T) & this) {
 | 
|---|
| 81 |         return *((this.data) ? &this.data->object : 0p);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | forall(T & | sized(T) | { void ^?{}(T &); })
 | 
|---|
| 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 | 
 | 
|---|
| 92 | forall(T & | sized(T) | { void ^?{}(T &); })
 | 
|---|
| 93 | void ?=?(counter_ptr(T) & this, zero_t) {
 | 
|---|
| 94 |         internal_decrement(this);
 | 
|---|
| 95 |         this.data = 0p;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | forall(T & | sized(T))
 | 
|---|
| 99 | int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
 | 
|---|
| 100 |         return this.data == that.data;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | forall(T & | sized(T))
 | 
|---|
| 104 | int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
 | 
|---|
| 105 |         return !?==?(this, that);
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | forall(T & | sized(T))
 | 
|---|
| 109 | int ?==?(counter_ptr(T) const & this, zero_t) {
 | 
|---|
| 110 |         return this.data == 0;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | forall(T & | sized(T))
 | 
|---|
| 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.
 | 
|---|
| 119 | forall(T &)
 | 
|---|
| 120 | void ?{}(unique_ptr(T) & this) {
 | 
|---|
| 121 |         this.data = 0p;
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | forall(T &)
 | 
|---|
| 125 | void ?{}(unique_ptr(T) & this, zero_t) {
 | 
|---|
| 126 |         this.data = 0p;
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
 | 
|---|
| 130 | void ?{}(unique_ptr(T) & this, Args args) {
 | 
|---|
| 131 |         this.data = malloc();
 | 
|---|
| 132 |         (*this.data){args};
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | forall(T & | { void ^?{}(T &); })
 | 
|---|
| 136 | void ^?{}(unique_ptr(T) & this) {
 | 
|---|
| 137 |         delete(this.data);
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | forall(T &)
 | 
|---|
| 141 | T & *?(unique_ptr(T) & this) {
 | 
|---|
| 142 |         return *this.data;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | forall(T & | { void ^?{}(T &); })
 | 
|---|
| 146 | void ?=?(unique_ptr(T) & this, zero_t) {
 | 
|---|
| 147 |         delete(this.data);
 | 
|---|
| 148 |         this.data = 0p;
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | forall(T & | { void ^?{}(T &); })
 | 
|---|
| 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 | 
 | 
|---|
| 158 | forall(T &)
 | 
|---|
| 159 | T * release(unique_ptr(T) & this) {
 | 
|---|
| 160 |         T * data = this.data;
 | 
|---|
| 161 |         this.data = 0p;
 | 
|---|
| 162 |         return data;
 | 
|---|
| 163 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | forall(T &)
 | 
|---|
| 166 | int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
 | 
|---|
| 167 |         return this.data == that.data;
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | forall(T &)
 | 
|---|
| 171 | int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
 | 
|---|
| 172 |         return !?==?(this, that);
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | forall(T &)
 | 
|---|
| 176 | int ?==?(unique_ptr(T) const & this, zero_t) {
 | 
|---|
| 177 |         return this.data == 0;
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | forall(T &)
 | 
|---|
| 181 | int ?!=?(unique_ptr(T) const & this, zero_t) {
 | 
|---|
| 182 |         return !?==?(this, (zero_t)0);
 | 
|---|
| 183 | }
 | 
|---|