source: libcfa/src/memory.cfa @ 3f39009

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 3f39009 was 1341ce1, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Simple release function on unique_ptrs

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[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]20forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
[aabb846]21void ?{}(counter_data(T) & this, Args args) {
22        (this.counter){1};
23        (this.object){args};
24}
25
[fd54fef]26forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]27void ^?{}(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]33forall(T & | sized(T))
[aabb846]34void ?{}(counter_ptr(T) & this) {
35        this.data = 0p;
36}
37
[fd54fef]38forall(T & | sized(T))
[aabb846]39void ?{}(counter_ptr(T) & this, zero_t) {
40        this.data = 0p;
41}
42
[fd54fef]43forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]44static void internal_decrement(counter_ptr(T) & this) {
45        if (this.data && 0 == --this.data->counter) {
46                delete(this.data);
47        }
48}
49
[fd54fef]50forall(T & | sized(T))
[aabb846]51static 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]58forall(T & | sized(T))
[aabb846]59void ?{}(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]65forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
[aabb846]66void ?{}(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]72forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]73void ^?{}(counter_ptr(T) & this) {
74        internal_decrement(this);
75}
76
[fd54fef]77forall(T & | sized(T))
[aabb846]78T & *?(counter_ptr(T) & this) {
79        return *((this.data) ? &this.data->object : 0p);
80}
81
[fd54fef]82forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]83void ?=?(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]90forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]91void ?=?(counter_ptr(T) & this, zero_t) {
92        internal_decrement(this);
93        this.data = 0p;
94}
95
[fd54fef]96forall(T & | sized(T))
[aabb846]97int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
98        return this.data == that.data;
99}
100
[fd54fef]101forall(T & | sized(T))
[aabb846]102int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
103        return !?==?(this, that);
104}
105
[fd54fef]106forall(T & | sized(T))
[aabb846]107int ?==?(counter_ptr(T) const & this, zero_t) {
108        return this.data == 0;
109}
110
[fd54fef]111forall(T & | sized(T))
[aabb846]112int ?!=?(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]117forall(T &)
[aabb846]118void ?{}(unique_ptr(T) & this) {
119        this.data = 0p;
120}
121
[fd54fef]122forall(T &)
[aabb846]123void ?{}(unique_ptr(T) & this, zero_t) {
124        this.data = 0p;
125}
126
[fd54fef]127forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
[aabb846]128void ?{}(unique_ptr(T) & this, Args args) {
[8be729f]129        this.data = malloc();
130        (*this.data){args};
[aabb846]131}
132
[fd54fef]133forall(T & | { void ^?{}(T &); })
[aabb846]134void ^?{}(unique_ptr(T) & this) {
135        delete(this.data);
136}
137
[fd54fef]138forall(T &)
[aabb846]139T & *?(unique_ptr(T) & this) {
140        return *this.data;
141}
142
[fd54fef]143forall(T & | { void ^?{}(T &); })
[aabb846]144void ?=?(unique_ptr(T) & this, zero_t) {
145        delete(this.data);
146        this.data = 0p;
147}
148
[fd54fef]149forall(T & | { void ^?{}(T &); })
[aabb846]150void 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]156forall(T &)
157T * release(unique_ptr(T) & this) {
158        T * data = this.data;
159        this.data = 0p;
160        return data;
161}
162
[fd54fef]163forall(T &)
[aabb846]164int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
165        return this.data == that.data;
166}
167
[fd54fef]168forall(T &)
[aabb846]169int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
170        return !?==?(this, that);
171}
172
[fd54fef]173forall(T &)
[aabb846]174int ?==?(unique_ptr(T) const & this, zero_t) {
175        return this.data == 0;
176}
177
[fd54fef]178forall(T &)
[aabb846]179int ?!=?(unique_ptr(T) const & this, zero_t) {
180        return !?==?(this, (zero_t)0);
181}
Note: See TracBrowser for help on using the repository browser.