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-2015, 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
;
37 package body Ada
.Containers
.Indefinite_Holders
is
39 pragma Annotate
(CodePeer
, Skip_Analysis
);
42 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
48 function "=" (Left
, Right
: Holder
) return Boolean is
50 if Left
.Reference
= Right
.Reference
then
52 -- Covers both null and not null but the same shared object cases
56 elsif Left
.Reference
/= null and Right
.Reference
/= null then
57 return Left
.Reference
.Element
.all = Right
.Reference
.Element
.all;
68 overriding
procedure Adjust
(Container
: in out Holder
) is
70 if Container
.Reference
/= null then
71 if Container
.Busy
= 0 then
73 -- Container is not locked, reuse existing internal shared object
75 Reference
(Container
.Reference
);
77 -- Otherwise, create copy of both internal shared object and
80 Container
.Reference
:=
84 new Element_Type'(Container
.Reference
.Element
.all));
91 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
) is
93 if Control
.Container
/= null then
94 Reference
(Control
.Container
.Reference
);
95 Control
.Container
.Busy
:= Control
.Container
.Busy
+ 1;
103 procedure Assign
(Target
: in out Holder
; Source
: Holder
) is
105 if Target
.Busy
/= 0 then
106 raise Program_Error
with "attempt to tamper with elements";
109 if Target
.Reference
/= Source
.Reference
then
110 if Target
.Reference
/= null then
111 Unreference
(Target
.Reference
);
114 Target
.Reference
:= Source
.Reference
;
116 if Source
.Reference
/= null then
117 Reference
(Target
.Reference
);
126 procedure Clear
(Container
: in out Holder
) is
128 if Container
.Busy
/= 0 then
129 raise Program_Error
with "attempt to tamper with elements";
132 if Container
.Reference
/= null then
133 Unreference
(Container
.Reference
);
134 Container
.Reference
:= null;
138 ------------------------
139 -- Constant_Reference --
140 ------------------------
142 function Constant_Reference
143 (Container
: aliased Holder
) return Constant_Reference_Type
is
145 if Container
.Reference
= null then
146 raise Constraint_Error
with "container is empty";
148 elsif Container
.Busy
= 0
149 and then not System
.Atomic_Counters
.Is_One
150 (Container
.Reference
.Counter
)
152 -- Container is not locked and internal shared object is used by
153 -- other container, create copy of both internal shared object and
156 Container
'Unrestricted_Access.Reference
:=
159 Element => new Element_Type'(Container
.Reference
.Element
.all));
163 Ref
: constant Constant_Reference_Type
:=
164 (Element
=> Container
.Reference
.Element
.all'Access,
165 Control
=> (Controlled
with Container
'Unrestricted_Access));
167 Reference
(Ref
.Control
.Container
.Reference
);
168 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
171 end Constant_Reference
;
177 function Copy
(Source
: Holder
) return Holder
is
179 if Source
.Reference
= null then
180 return (Controlled
with null, 0);
182 elsif Source
.Busy
= 0 then
184 -- Container is not locked, reuse internal shared object
186 Reference
(Source
.Reference
);
188 return (Controlled
with Source
.Reference
, 0);
191 -- Otherwise, create copy of both internal shared object and element
197 Element => new Element_Type'(Source
.Reference
.Element
.all)),
206 function Element
(Container
: Holder
) return Element_Type
is
208 if Container
.Reference
= null then
209 raise Constraint_Error
with "container is empty";
211 return Container
.Reference
.Element
.all;
219 overriding
procedure Finalize
(Container
: in out Holder
) is
221 if Container
.Busy
/= 0 then
222 raise Program_Error
with "attempt to tamper with elements";
225 if Container
.Reference
/= null then
226 Unreference
(Container
.Reference
);
227 Container
.Reference
:= null;
231 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
) is
233 if Control
.Container
/= null then
234 Unreference
(Control
.Container
.Reference
);
235 Control
.Container
.Busy
:= Control
.Container
.Busy
- 1;
236 Control
.Container
:= null;
244 function Is_Empty
(Container
: Holder
) return Boolean is
246 return Container
.Reference
= null;
253 procedure Move
(Target
: in out Holder
; Source
: in out Holder
) is
255 if Target
.Busy
/= 0 then
256 raise Program_Error
with "attempt to tamper with elements";
259 if Source
.Busy
/= 0 then
260 raise Program_Error
with "attempt to tamper with elements";
263 if Target
.Reference
/= Source
.Reference
then
264 if Target
.Reference
/= null then
265 Unreference
(Target
.Reference
);
268 Target
.Reference
:= Source
.Reference
;
269 Source
.Reference
:= null;
277 procedure Query_Element
279 Process
: not null access procedure (Element
: Element_Type
))
281 B
: Natural renames Container
'Unrestricted_Access.Busy
;
284 if Container
.Reference
= null then
285 raise Constraint_Error
with "container is empty";
287 elsif Container
.Busy
= 0
289 not System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
)
291 -- Container is not locked and internal shared object is used by
292 -- other container, create copy of both internal shared object and
295 Container
'Unrestricted_Access.Reference
:=
298 Element => new Element_Type'(Container
.Reference
.Element
.all));
304 Process
(Container
.Reference
.Element
.all);
319 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
320 Container
: out Holder
)
325 if not Boolean'Input (Stream
) then
326 Container
.Reference
:=
329 Element => new Element_Type'(Element_Type
'Input (Stream
)));
334 (Stream
: not null access Root_Stream_Type
'Class;
335 Item
: out Constant_Reference_Type
)
338 raise Program_Error
with "attempt to stream reference";
342 (Stream
: not null access Root_Stream_Type
'Class;
343 Item
: out Reference_Type
)
346 raise Program_Error
with "attempt to stream reference";
353 procedure Reference
(Item
: not null Shared_Holder_Access
) is
355 System
.Atomic_Counters
.Increment
(Item
.Counter
);
359 (Container
: aliased in out Holder
) return Reference_Type
362 if Container
.Reference
= null then
363 raise Constraint_Error
with "container is empty";
365 elsif Container
.Busy
= 0
367 not System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
)
369 -- Container is not locked and internal shared object is used by
370 -- other container, create copy of both internal shared object and
373 Container
.Reference
:=
376 Element => new Element_Type'(Container
.Reference
.Element
.all));
380 Ref
: constant Reference_Type
:=
381 (Element
=> Container
.Reference
.Element
.all'Access,
382 Control
=> (Controlled
with Container
'Unrestricted_Access));
384 Reference
(Ref
.Control
.Container
.Reference
);
385 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
390 ---------------------
391 -- Replace_Element --
392 ---------------------
394 procedure Replace_Element
395 (Container
: in out Holder
;
396 New_Item
: Element_Type
)
398 -- Element allocator may need an accessibility check in case actual type
399 -- is class-wide or has access discriminants (RM 4.8(10.1) and
402 pragma Unsuppress
(Accessibility_Check
);
405 if Container
.Busy
/= 0 then
406 raise Program_Error
with "attempt to tamper with elements";
409 if Container
.Reference
= null then
410 -- Holder is empty, allocate new Shared_Holder.
412 Container
.Reference
:=
415 Element => new Element_Type'(New_Item
));
417 elsif System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
) then
418 -- Shared_Holder can be reused.
420 Free
(Container
.Reference
.Element
);
421 Container
.Reference
.Element
:= new Element_Type
'(New_Item);
424 Unreference (Container.Reference);
425 Container.Reference :=
428 Element
=> new Element_Type
'(New_Item));
436 function To_Holder (New_Item : Element_Type) return Holder is
437 -- The element allocator may need an accessibility check in the case the
438 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
441 pragma Unsuppress (Accessibility_Check);
448 Element
=> new Element_Type
'(New_Item)), 0);
455 procedure Unreference (Item : not null Shared_Holder_Access) is
458 new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access);
460 Aux : Shared_Holder_Access := Item;
463 if System.Atomic_Counters.Decrement (Aux.Counter) then
473 procedure Update_Element
474 (Container : in out Holder;
475 Process : not null access procedure (Element : in out Element_Type))
477 B : Natural renames Container.Busy;
480 if Container.Reference = null then
481 raise Constraint_Error with "container is empty";
483 elsif Container.Busy = 0
485 not System.Atomic_Counters.Is_One (Container.Reference.Counter)
487 -- Container is not locked and internal shared object is used by
488 -- other container, create copy of both internal shared object and
491 Container'Unrestricted_Access.Reference :=
494 Element
=> new Element_Type
'(Container.Reference.Element.all));
500 Process (Container.Reference.Element.all);
515 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
519 Boolean'Output (Stream, Container.Reference = null);
521 if Container.Reference /= null then
522 Element_Type'Output (Stream, Container.Reference.Element.all);
527 (Stream : not null access Root_Stream_Type'Class;
528 Item : Reference_Type)
531 raise Program_Error with "attempt to stream reference";
535 (Stream : not null access Root_Stream_Type'Class;
536 Item : Constant_Reference_Type)
539 raise Program_Error with "attempt to stream reference";
542 end Ada.Containers.Indefinite_Holders;