awa_2.4.0_59135a52/ada-wiki/src/util/util-refs.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-----------------------------------------------------------------------
--  util-refs -- Reference Counting
--  Copyright (C) 2010, 2011, 2019, 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Unchecked_Deallocation;
package body Util.Refs is

   package body Indefinite_References is

      --  ------------------------------
      --  Create an element and return a reference to that element.
      --  ------------------------------
      function Create (Value : in Element_Access) return Ref is
      begin
         return Result : Ref do
            Result.Target := Value;
            Util.Concurrent.Counters.Increment (Result.Target.Ref_Counter);
         end return;
      end Create;

      --  ------------------------------
      --  Get the element access value.
      --  ------------------------------
      function Value (Object : in Ref'Class) return Element_Accessor is
      begin
         return Element_Accessor '(Element => Object.Target);
      end Value;

      --  ------------------------------
      --  Returns true if the reference does not contain any element.
      --  ------------------------------
      function Is_Null (Object : in Ref'Class) return Boolean is
      begin
         return Object.Target = null;
      end Is_Null;

      function "=" (Left, Right : in Ref'Class) return Boolean is
      begin
         return Left.Target = Right.Target;
      end "=";

      package body Atomic is
         protected body Atomic_Ref is
            --  ------------------------------
            --  Get the reference
            --  ------------------------------
            function Get return Ref is
            begin
               return Value;
            end Get;

            --  ------------------------------
            --  Change the reference
            --  ------------------------------
            procedure Set (Object : in Ref) is
            begin
               Value := Object;
            end Set;

         end Atomic_Ref;
      end Atomic;

      procedure Free is
        new Ada.Unchecked_Deallocation (Object => Element_Type,
                                        Name   => Element_Access);

      --  ------------------------------
      --  Release the reference.  Invoke <b>Finalize</b> and free the storage if it was
      --  the last reference.
      --  ------------------------------
      overriding
      procedure Finalize (Obj : in out Ref) is
         Release : Boolean;
      begin
         if Obj.Target /= null then
            Util.Concurrent.Counters.Decrement (Obj.Target.Ref_Counter, Release);
            if Release then
               Obj.Target.Finalize;
               Free (Obj.Target);
            else
               Obj.Target := null;
            end if;
         end if;
      end Finalize;

      --  ------------------------------
      --  Update the reference counter after an assignment.
      --  ------------------------------
      overriding
      procedure Adjust (Obj : in out Ref) is
      begin
         if Obj.Target /= null then
            Util.Concurrent.Counters.Increment (Obj.Target.Ref_Counter);
         end if;
      end Adjust;

   end Indefinite_References;

   package body References is

      --  ------------------------------
      --  Create an element and return a reference to that element.
      --  ------------------------------
      function Create return Ref is
      begin
         return IR.Create (new Element_Type);
      end Create;
   end References;

   package body General_References is

      --  ------------------------------
      --  Create an element and return a reference to that element.
      --  ------------------------------
      function Create return Ref is
      begin
         return Result : Ref do
            Result.Target := new Ref_Data;
            Util.Concurrent.Counters.Increment (Result.Target.Ref_Counter);
         end return;
      end Create;

      --  ------------------------------
      --  Get the element access value.
      --  ------------------------------
      function Value (Object : in Ref'Class) return Element_Accessor is
      begin
         --  GCC 10 requires the Unrestricted_Access while GCC < 10 allowed Access...
         --  It is safe because the Ref handles the copy and Element_Accessor is limited.
         return Element_Accessor '(Element => Object.Target.Data'Unrestricted_Access);
      end Value;

      --  ------------------------------
      --  Returns true if the reference does not contain any element.
      --  ------------------------------
      function Is_Null (Object : in Ref'Class) return Boolean is
      begin
         return Object.Target = null;
      end Is_Null;

      function "=" (Left, Right : in Ref'Class) return Boolean is
      begin
         return Left.Target = Right.Target;
      end "=";

      package body Atomic is
         protected body Atomic_Ref is
            --  ------------------------------
            --  Get the reference
            --  ------------------------------
            function Get return Ref is
            begin
               return Value;
            end Get;

            --  ------------------------------
            --  Change the reference
            --  ------------------------------
            procedure Set (Object : in Ref) is
            begin
               Value := Object;
            end Set;

         end Atomic_Ref;
      end Atomic;

      procedure Free is
        new Ada.Unchecked_Deallocation (Object => Ref_Data,
                                        Name   => Ref_Data_Access);

      --  ------------------------------
      --  Release the reference.  Invoke <b>Finalize</b> and free the storage if it was
      --  the last reference.
      --  ------------------------------
      overriding
      procedure Finalize (Obj : in out Ref) is
         Release : Boolean;
      begin
         if Obj.Target /= null then
            Util.Concurrent.Counters.Decrement (Obj.Target.Ref_Counter, Release);
            if Release then
               Finalize (Obj.Target.Data);
               Free (Obj.Target);
            else
               Obj.Target := null;
            end if;
         end if;
      end Finalize;

      --  ------------------------------
      --  Update the reference counter after an assignment.
      --  ------------------------------
      overriding
      procedure Adjust (Obj : in out Ref) is
      begin
         if Obj.Target /= null then
            Util.Concurrent.Counters.Increment (Obj.Target.Ref_Counter);
         end if;
      end Adjust;

   end General_References;

end Util.Refs;