source: libcfa/src/memory.cfa @ 3267041

Last change on this file since 3267041 was 789f279, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

More standard lib visibility

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