1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . I N D E F I N I T E _ H O L D E R S --
9 -- Copyright (C) 2013-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 ------------------------------------------------------------------------------
28 -- Note: special attention must be paid to the case of simultaneous access
29 -- to internal shared objects and elements by different tasks. The Reference
30 -- counter of internal shared object is the only component protected using
31 -- atomic operations; other components and elements can be modified only when
32 -- reference counter is equal to one (so there are no other references to this
33 -- internal shared object and element).
35 with Ada
.Unchecked_Deallocation
;
36 with System
.Put_Images
;
38 package body Ada
.Containers
.Indefinite_Holders
is
41 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
43 procedure Detach
(Container
: Holder
);
44 -- Detach data from shared copy if necessary. This is necessary to prepare
45 -- container to be modified.
51 function "=" (Left
, Right
: Holder
) return Boolean is
53 if Left
.Reference
= Right
.Reference
then
55 -- Covers both null and not null but the same shared object cases
59 elsif Left
.Reference
/= null and Right
.Reference
/= null then
60 return Left
.Reference
.Element
.all = Right
.Reference
.Element
.all;
71 overriding
procedure Adjust
(Container
: in out Holder
) is
73 if Container
.Reference
/= null then
74 if Container
.Busy
= 0 then
76 -- Container is not locked, reuse existing internal shared object
78 Reference
(Container
.Reference
);
80 -- Otherwise, create copy of both internal shared object and
83 Container
.Reference
:=
87 new Element_Type'(Container
.Reference
.Element
.all));
94 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
) is
96 if Control
.Container
/= null then
97 Reference
(Control
.Container
.Reference
);
98 Control
.Container
.Busy
:= Control
.Container
.Busy
+ 1;
106 procedure Assign
(Target
: in out Holder
; Source
: Holder
) is
108 if Target
.Busy
/= 0 then
109 raise Program_Error
with "attempt to tamper with elements";
112 if Target
.Reference
/= Source
.Reference
then
113 if Target
.Reference
/= null then
114 Unreference
(Target
.Reference
);
117 Target
.Reference
:= Source
.Reference
;
119 if Source
.Reference
/= null then
120 Reference
(Target
.Reference
);
129 procedure Clear
(Container
: in out Holder
) is
131 if Container
.Busy
/= 0 then
132 raise Program_Error
with "attempt to tamper with elements";
135 if Container
.Reference
/= null then
136 Unreference
(Container
.Reference
);
137 Container
.Reference
:= null;
141 ------------------------
142 -- Constant_Reference --
143 ------------------------
145 function Constant_Reference
146 (Container
: aliased Holder
) return Constant_Reference_Type
is
148 if Container
.Reference
= null then
149 raise Constraint_Error
with "container is empty";
155 Ref
: constant Constant_Reference_Type
:=
156 (Element
=> Container
.Reference
.Element
.all'Access,
157 Control
=> (Controlled
with Container
'Unrestricted_Access));
159 Reference
(Ref
.Control
.Container
.Reference
);
160 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
163 end Constant_Reference
;
169 function Copy
(Source
: Holder
) return Holder
is
171 if Source
.Reference
= null then
172 return (Controlled
with null, 0);
174 elsif Source
.Busy
= 0 then
176 -- Container is not locked, reuse internal shared object
178 Reference
(Source
.Reference
);
180 return (Controlled
with Source
.Reference
, 0);
183 -- Otherwise, create copy of both internal shared object and element
189 Element => new Element_Type'(Source
.Reference
.Element
.all)),
198 procedure Detach
(Container
: Holder
) is
200 if Container
.Busy
= 0
201 and then not System
.Atomic_Counters
.Is_One
202 (Container
.Reference
.Counter
)
204 -- Container is not locked and internal shared object is used by
205 -- other container, create copy of both internal shared object and
209 Old
: constant Shared_Holder_Access
:= Container
.Reference
;
212 Container
'Unrestricted_Access.Reference
:=
216 new Element_Type'(Container
.Reference
.Element
.all));
226 function Element
(Container
: Holder
) return Element_Type
is
228 if Container
.Reference
= null then
229 raise Constraint_Error
with "container is empty";
231 return Container
.Reference
.Element
.all;
239 overriding
procedure Finalize
(Container
: in out Holder
) is
241 if Container
.Busy
/= 0 then
242 raise Program_Error
with "attempt to tamper with elements";
245 if Container
.Reference
/= null then
246 Unreference
(Container
.Reference
);
247 Container
.Reference
:= null;
251 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
) is
253 if Control
.Container
/= null then
254 Unreference
(Control
.Container
.Reference
);
255 Control
.Container
.Busy
:= Control
.Container
.Busy
- 1;
256 Control
.Container
:= null;
264 function Is_Empty
(Container
: Holder
) return Boolean is
266 return Container
.Reference
= null;
273 procedure Move
(Target
: in out Holder
; Source
: in out Holder
) is
275 if Target
.Busy
/= 0 then
276 raise Program_Error
with "attempt to tamper with elements";
279 if Source
.Busy
/= 0 then
280 raise Program_Error
with "attempt to tamper with elements";
283 if Target
.Reference
/= Source
.Reference
then
284 if Target
.Reference
/= null then
285 Unreference
(Target
.Reference
);
288 Target
.Reference
:= Source
.Reference
;
289 Source
.Reference
:= null;
297 procedure Query_Element
299 Process
: not null access procedure (Element
: Element_Type
))
301 B
: Natural renames Container
'Unrestricted_Access.Busy
;
304 if Container
.Reference
= null then
305 raise Constraint_Error
with "container is empty";
313 Process
(Container
.Reference
.Element
.all);
328 (S
: in out Ada
.Strings
.Text_Buffers
.Root_Buffer_Type
'Class; V
: Holder
)
330 use System
.Put_Images
;
333 if not Is_Empty
(V
) then
334 Element_Type
'Put_Image (S
, Element
(V
));
344 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
345 Container
: out Holder
)
350 if not Boolean'Input (Stream
) then
351 Container
.Reference
:=
354 Element => new Element_Type'(Element_Type
'Input (Stream
)));
359 (Stream
: not null access Root_Stream_Type
'Class;
360 Item
: out Constant_Reference_Type
)
363 raise Program_Error
with "attempt to stream reference";
367 (Stream
: not null access Root_Stream_Type
'Class;
368 Item
: out Reference_Type
)
371 raise Program_Error
with "attempt to stream reference";
378 procedure Reference
(Item
: not null Shared_Holder_Access
) is
380 System
.Atomic_Counters
.Increment
(Item
.Counter
);
384 (Container
: aliased in out Holder
) return Reference_Type
387 if Container
.Reference
= null then
388 raise Constraint_Error
with "container is empty";
394 Ref
: constant Reference_Type
:=
395 (Element
=> Container
.Reference
.Element
.all'Access,
396 Control
=> (Controlled
with Container
'Unrestricted_Access));
398 Reference
(Ref
.Control
.Container
.Reference
);
399 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
404 ---------------------
405 -- Replace_Element --
406 ---------------------
408 procedure Replace_Element
409 (Container
: in out Holder
;
410 New_Item
: Element_Type
)
412 -- Element allocator may need an accessibility check in case actual type
413 -- is class-wide or has access discriminants (RM 4.8(10.1) and
416 pragma Unsuppress
(Accessibility_Check
);
419 if Container
.Busy
/= 0 then
420 raise Program_Error
with "attempt to tamper with elements";
423 if Container
.Reference
= null then
424 -- Holder is empty, allocate new Shared_Holder.
426 Container
.Reference
:=
429 Element => new Element_Type'(New_Item
));
431 elsif System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
) then
432 -- Shared_Holder can be reused.
434 Free
(Container
.Reference
.Element
);
435 Container
.Reference
.Element
:= new Element_Type
'(New_Item);
438 Unreference (Container.Reference);
439 Container.Reference :=
442 Element
=> new Element_Type
'(New_Item));
450 procedure Swap (Left, Right : in out Holder) is
452 if Left.Busy /= 0 then
453 raise Program_Error with "attempt to tamper with elements";
456 if Right.Busy /= 0 then
457 raise Program_Error with "attempt to tamper with elements";
460 if Left.Reference /= Right.Reference then
462 Tmp : constant Shared_Holder_Access := Left.Reference;
464 Left.Reference := Right.Reference;
465 Right.Reference := Tmp;
474 function To_Holder (New_Item : Element_Type) return Holder is
475 -- The element allocator may need an accessibility check in the case the
476 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
479 pragma Unsuppress (Accessibility_Check);
486 Element
=> new Element_Type
'(New_Item)), 0);
493 procedure Unreference (Item : not null Shared_Holder_Access) is
496 new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access);
498 Aux : Shared_Holder_Access := Item;
501 if System.Atomic_Counters.Decrement (Aux.Counter) then
511 procedure Update_Element
512 (Container : in out Holder;
513 Process : not null access procedure (Element : in out Element_Type))
515 B : Natural renames Container.Busy;
518 if Container.Reference = null then
519 raise Constraint_Error with "container is empty";
527 Process (Container.Reference.Element.all);
542 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
546 Boolean'Output (Stream, Container.Reference = null);
548 if Container.Reference /= null then
549 Element_Type'Output (Stream, Container.Reference.Element.all);
554 (Stream : not null access Root_Stream_Type'Class;
555 Item : Reference_Type)
558 raise Program_Error with "attempt to stream reference";
562 (Stream : not null access Root_Stream_Type'Class;
563 Item : Constant_Reference_Type)
566 raise Program_Error with "attempt to stream reference";
569 end Ada.Containers.Indefinite_Holders;