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
40 new Ada
.Unchecked_Deallocation
(Element_Type
, Element_Access
);
46 function "=" (Left
, Right
: Holder
) return Boolean is
48 if Left
.Reference
= Right
.Reference
then
50 -- Covers both null and not null but the same shared object cases
54 elsif Left
.Reference
/= null and Right
.Reference
/= null then
55 return Left
.Reference
.Element
.all = Right
.Reference
.Element
.all;
66 overriding
procedure Adjust
(Container
: in out Holder
) is
68 if Container
.Reference
/= null then
69 if Container
.Busy
= 0 then
71 -- Container is not locked, reuse existing internal shared object
73 Reference
(Container
.Reference
);
75 -- Otherwise, create copy of both internal shared object and
78 Container
.Reference
:=
82 new Element_Type'(Container
.Reference
.Element
.all));
89 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
) is
91 if Control
.Container
/= null then
92 Reference
(Control
.Container
.Reference
);
93 Control
.Container
.Busy
:= Control
.Container
.Busy
+ 1;
101 procedure Assign
(Target
: in out Holder
; Source
: Holder
) is
103 if Target
.Busy
/= 0 then
104 raise Program_Error
with "attempt to tamper with elements";
107 if Target
.Reference
/= Source
.Reference
then
108 if Target
.Reference
/= null then
109 Unreference
(Target
.Reference
);
112 Target
.Reference
:= Source
.Reference
;
114 if Source
.Reference
/= null then
115 Reference
(Target
.Reference
);
124 procedure Clear
(Container
: in out Holder
) is
126 if Container
.Busy
/= 0 then
127 raise Program_Error
with "attempt to tamper with elements";
130 if Container
.Reference
/= null then
131 Unreference
(Container
.Reference
);
132 Container
.Reference
:= null;
136 ------------------------
137 -- Constant_Reference --
138 ------------------------
140 function Constant_Reference
141 (Container
: aliased Holder
) return Constant_Reference_Type
is
143 if Container
.Reference
= null then
144 raise Constraint_Error
with "container is empty";
146 elsif Container
.Busy
= 0
147 and then not System
.Atomic_Counters
.Is_One
148 (Container
.Reference
.Counter
)
150 -- Container is not locked and internal shared object is used by
151 -- other container, create copy of both internal shared object and
154 Container
'Unrestricted_Access.Reference
:=
157 Element => new Element_Type'(Container
.Reference
.Element
.all));
161 Ref
: constant Constant_Reference_Type
:=
162 (Element
=> Container
.Reference
.Element
.all'Access,
163 Control
=> (Controlled
with Container
'Unrestricted_Access));
165 Reference
(Ref
.Control
.Container
.Reference
);
166 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
169 end Constant_Reference
;
175 function Copy
(Source
: Holder
) return Holder
is
177 if Source
.Reference
= null then
178 return (Controlled
with null, 0);
180 elsif Source
.Busy
= 0 then
182 -- Container is not locked, reuse internal shared object
184 Reference
(Source
.Reference
);
186 return (Controlled
with Source
.Reference
, 0);
189 -- Otherwise, create copy of both internal shared object and element
195 Element => new Element_Type'(Source
.Reference
.Element
.all)),
204 function Element
(Container
: Holder
) return Element_Type
is
206 if Container
.Reference
= null then
207 raise Constraint_Error
with "container is empty";
209 return Container
.Reference
.Element
.all;
217 overriding
procedure Finalize
(Container
: in out Holder
) is
219 if Container
.Busy
/= 0 then
220 raise Program_Error
with "attempt to tamper with elements";
223 if Container
.Reference
/= null then
224 Unreference
(Container
.Reference
);
225 Container
.Reference
:= null;
229 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
) is
231 if Control
.Container
/= null then
232 Unreference
(Control
.Container
.Reference
);
233 Control
.Container
.Busy
:= Control
.Container
.Busy
- 1;
234 Control
.Container
:= null;
242 function Is_Empty
(Container
: Holder
) return Boolean is
244 return Container
.Reference
= null;
251 procedure Move
(Target
: in out Holder
; Source
: in out Holder
) is
253 if Target
.Busy
/= 0 then
254 raise Program_Error
with "attempt to tamper with elements";
257 if Source
.Busy
/= 0 then
258 raise Program_Error
with "attempt to tamper with elements";
261 if Target
.Reference
/= Source
.Reference
then
262 if Target
.Reference
/= null then
263 Unreference
(Target
.Reference
);
266 Target
.Reference
:= Source
.Reference
;
267 Source
.Reference
:= null;
275 procedure Query_Element
277 Process
: not null access procedure (Element
: Element_Type
))
279 B
: Natural renames Container
'Unrestricted_Access.Busy
;
282 if Container
.Reference
= null then
283 raise Constraint_Error
with "container is empty";
285 elsif Container
.Busy
= 0
287 not System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
)
289 -- Container is not locked and internal shared object is used by
290 -- other container, create copy of both internal shared object and
293 Container
'Unrestricted_Access.Reference
:=
296 Element => new Element_Type'(Container
.Reference
.Element
.all));
302 Process
(Container
.Reference
.Element
.all);
317 (Stream
: not null access Ada
.Streams
.Root_Stream_Type
'Class;
318 Container
: out Holder
)
323 if not Boolean'Input (Stream
) then
324 Container
.Reference
:=
327 Element => new Element_Type'(Element_Type
'Input (Stream
)));
332 (Stream
: not null access Root_Stream_Type
'Class;
333 Item
: out Constant_Reference_Type
)
336 raise Program_Error
with "attempt to stream reference";
340 (Stream
: not null access Root_Stream_Type
'Class;
341 Item
: out Reference_Type
)
344 raise Program_Error
with "attempt to stream reference";
351 procedure Reference
(Item
: not null Shared_Holder_Access
) is
353 System
.Atomic_Counters
.Increment
(Item
.Counter
);
357 (Container
: aliased in out Holder
) return Reference_Type
360 if Container
.Reference
= null then
361 raise Constraint_Error
with "container is empty";
363 elsif Container
.Busy
= 0
365 not System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
)
367 -- Container is not locked and internal shared object is used by
368 -- other container, create copy of both internal shared object and
371 Container
.Reference
:=
374 Element => new Element_Type'(Container
.Reference
.Element
.all));
378 Ref
: constant Reference_Type
:=
379 (Element
=> Container
.Reference
.Element
.all'Access,
380 Control
=> (Controlled
with Container
'Unrestricted_Access));
382 Reference
(Ref
.Control
.Container
.Reference
);
383 Ref
.Control
.Container
.Busy
:= Ref
.Control
.Container
.Busy
+ 1;
388 ---------------------
389 -- Replace_Element --
390 ---------------------
392 procedure Replace_Element
393 (Container
: in out Holder
;
394 New_Item
: Element_Type
)
396 -- Element allocator may need an accessibility check in case actual type
397 -- is class-wide or has access discriminants (RM 4.8(10.1) and
400 pragma Unsuppress
(Accessibility_Check
);
403 if Container
.Busy
/= 0 then
404 raise Program_Error
with "attempt to tamper with elements";
407 if Container
.Reference
= null then
408 -- Holder is empty, allocate new Shared_Holder.
410 Container
.Reference
:=
413 Element => new Element_Type'(New_Item
));
415 elsif System
.Atomic_Counters
.Is_One
(Container
.Reference
.Counter
) then
416 -- Shared_Holder can be reused.
418 Free
(Container
.Reference
.Element
);
419 Container
.Reference
.Element
:= new Element_Type
'(New_Item);
422 Unreference (Container.Reference);
423 Container.Reference :=
426 Element
=> new Element_Type
'(New_Item));
434 function To_Holder (New_Item : Element_Type) return Holder is
435 -- The element allocator may need an accessibility check in the case the
436 -- actual type is class-wide or has access discriminants (RM 4.8(10.1)
439 pragma Unsuppress (Accessibility_Check);
446 Element
=> new Element_Type
'(New_Item)), 0);
453 procedure Unreference (Item : not null Shared_Holder_Access) is
456 new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access);
458 Aux : Shared_Holder_Access := Item;
461 if System.Atomic_Counters.Decrement (Aux.Counter) then
471 procedure Update_Element
472 (Container : in out Holder;
473 Process : not null access procedure (Element : in out Element_Type))
475 B : Natural renames Container.Busy;
478 if Container.Reference = null then
479 raise Constraint_Error with "container is empty";
481 elsif Container.Busy = 0
483 not System.Atomic_Counters.Is_One (Container.Reference.Counter)
485 -- Container is not locked and internal shared object is used by
486 -- other container, create copy of both internal shared object and
489 Container'Unrestricted_Access.Reference :=
492 Element
=> new Element_Type
'(Container.Reference.Element.all));
498 Process (Container.Reference.Element.all);
513 (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
517 Boolean'Output (Stream, Container.Reference = null);
519 if Container.Reference /= null then
520 Element_Type'Output (Stream, Container.Reference.Element.all);
525 (Stream : not null access Root_Stream_Type'Class;
526 Item : Reference_Type)
529 raise Program_Error with "attempt to stream reference";
533 (Stream : not null access Root_Stream_Type'Class;
534 Item : Constant_Reference_Type)
537 raise Program_Error with "attempt to stream reference";
540 end Ada.Containers.Indefinite_Holders;