| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 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 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2013-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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 |
------------------------------------------------------------------------------ |
| 27 |
|
| 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). |
| 34 |
|
| 35 |
with Ada.Unchecked_Deallocation; |
| 36 |
with System.Put_Images; |
| 37 |
|
| 38 |
package body Ada.Containers.Indefinite_Holders is |
| 39 |
|
| 40 |
procedure Free is |
| 41 |
new Ada.Unchecked_Deallocation (Element_Type, Element_Access); |
| 42 |
|
| 43 |
procedure Detach (Container : Holder); |
| 44 |
-- Detach data from shared copy if necessary. This is necessary to prepare |
| 45 |
-- container to be modified. |
| 46 |
|
| 47 |
--------- |
| 48 |
-- "=" -- |
| 49 |
--------- |
| 50 |
|
| 51 |
function "=" (Left, Right : Holder) return Boolean is |
| 52 |
begin |
| 53 |
if Left.Reference = Right.Reference then |
| 54 |
|
| 55 |
-- Covers both null and not null but the same shared object cases |
| 56 |
|
| 57 |
return True; |
| 58 |
|
| 59 |
elsif Left.Reference /= null and Right.Reference /= null then |
| 60 |
return Left.Reference.Element.all = Right.Reference.Element.all; |
| 61 |
|
| 62 |
else |
| 63 |
return False; |
| 64 |
end if; |
| 65 |
end "="; |
| 66 |
|
| 67 |
------------ |
| 68 |
-- Adjust -- |
| 69 |
------------ |
| 70 |
|
| 71 |
overriding procedure Adjust (Container : in out Holder) is |
| 72 |
begin |
| 73 |
if Container.Reference /= null then |
| 74 |
if Container.Busy = 0 then |
| 75 |
|
| 76 |
-- Container is not locked, reuse existing internal shared object |
| 77 |
|
| 78 |
Reference (Container.Reference); |
| 79 |
else |
| 80 |
-- Otherwise, create copy of both internal shared object and |
| 81 |
-- element. |
| 82 |
|
| 83 |
Container.Reference := |
| 84 |
new Shared_Holder' |
| 85 |
(Counter => <>, |
| 86 |
Element => |
| 87 |
new Element_Type'(Container.Reference.Element.all)); |
| 88 |
end if; |
| 89 |
end if; |
| 90 |
|
| 91 |
Container.Busy := 0; |
| 92 |
end Adjust; |
| 93 |
|
| 94 |
overriding procedure Adjust (Control : in out Reference_Control_Type) is |
| 95 |
begin |
| 96 |
if Control.Container /= null then |
| 97 |
Reference (Control.Container.Reference); |
| 98 |
Control.Container.Busy := Control.Container.Busy + 1; |
| 99 |
end if; |
| 100 |
end Adjust; |
| 101 |
|
| 102 |
------------ |
| 103 |
-- Assign -- |
| 104 |
------------ |
| 105 |
|
| 106 |
procedure Assign (Target : in out Holder; Source : Holder) is |
| 107 |
begin |
| 108 |
if Target.Busy /= 0 then |
| 109 |
raise Program_Error with "attempt to tamper with elements"; |
| 110 |
end if; |
| 111 |
|
| 112 |
if Target.Reference /= Source.Reference then |
| 113 |
if Target.Reference /= null then |
| 114 |
Unreference (Target.Reference); |
| 115 |
end if; |
| 116 |
|
| 117 |
Target.Reference := Source.Reference; |
| 118 |
|
| 119 |
if Source.Reference /= null then |
| 120 |
Reference (Target.Reference); |
| 121 |
end if; |
| 122 |
end if; |
| 123 |
end Assign; |
| 124 |
|
| 125 |
----------- |
| 126 |
-- Clear -- |
| 127 |
----------- |
| 128 |
|
| 129 |
procedure Clear (Container : in out Holder) is |
| 130 |
begin |
| 131 |
if Container.Busy /= 0 then |
| 132 |
raise Program_Error with "attempt to tamper with elements"; |
| 133 |
end if; |
| 134 |
|
| 135 |
if Container.Reference /= null then |
| 136 |
Unreference (Container.Reference); |
| 137 |
Container.Reference := null; |
| 138 |
end if; |
| 139 |
end Clear; |
| 140 |
|
| 141 |
------------------------ |
| 142 |
-- Constant_Reference -- |
| 143 |
------------------------ |
| 144 |
|
| 145 |
function Constant_Reference |
| 146 |
(Container : aliased Holder) return Constant_Reference_Type is |
| 147 |
begin |
| 148 |
if Container.Reference = null then |
| 149 |
raise Constraint_Error with "container is empty"; |
| 150 |
end if; |
| 151 |
|
| 152 |
declare |
| 153 |
Ref : constant Constant_Reference_Type := |
| 154 |
(Element => Container.Reference.Element.all'Access, |
| 155 |
Control => (Controlled with Container'Unrestricted_Access)); |
| 156 |
begin |
| 157 |
Reference (Ref.Control.Container.Reference); |
| 158 |
Ref.Control.Container.Busy := Ref.Control.Container.Busy + 1; |
| 159 |
return Ref; |
| 160 |
end; |
| 161 |
end Constant_Reference; |
| 162 |
|
| 163 |
---------- |
| 164 |
-- Copy -- |
| 165 |
---------- |
| 166 |
|
| 167 |
function Copy (Source : Holder) return Holder is |
| 168 |
begin |
| 169 |
if Source.Reference = null then |
| 170 |
return (Controlled with null, 0); |
| 171 |
|
| 172 |
elsif Source.Busy = 0 then |
| 173 |
|
| 174 |
-- Container is not locked, reuse internal shared object |
| 175 |
|
| 176 |
Reference (Source.Reference); |
| 177 |
|
| 178 |
return (Controlled with Source.Reference, 0); |
| 179 |
|
| 180 |
else |
| 181 |
-- Otherwise, create copy of both internal shared object and element |
| 182 |
|
| 183 |
return |
| 184 |
(Controlled with |
| 185 |
new Shared_Holder' |
| 186 |
(Counter => <>, |
| 187 |
Element => new Element_Type'(Source.Reference.Element.all)), |
| 188 |
0); |
| 189 |
end if; |
| 190 |
end Copy; |
| 191 |
|
| 192 |
------------ |
| 193 |
-- Detach -- |
| 194 |
------------ |
| 195 |
|
| 196 |
procedure Detach (Container : Holder) is |
| 197 |
begin |
| 198 |
if Container.Busy = 0 |
| 199 |
and then not System.Atomic_Counters.Is_One |
| 200 |
(Container.Reference.Counter) |
| 201 |
then |
| 202 |
-- Container is not locked and internal shared object is used by |
| 203 |
-- other container, create copy of both internal shared object and |
| 204 |
-- element. |
| 205 |
|
| 206 |
declare |
| 207 |
Old : constant Shared_Holder_Access := Container.Reference; |
| 208 |
|
| 209 |
begin |
| 210 |
Container'Unrestricted_Access.Reference := |
| 211 |
new Shared_Holder' |
| 212 |
(Counter => <>, |
| 213 |
Element => |
| 214 |
new Element_Type'(Container.Reference.Element.all)); |
| 215 |
Unreference (Old); |
| 216 |
end; |
| 217 |
end if; |
| 218 |
end Detach; |
| 219 |
|
| 220 |
------------- |
| 221 |
-- Element -- |
| 222 |
------------- |
| 223 |
|
| 224 |
function Element (Container : Holder) return Element_Type is |
| 225 |
begin |
| 226 |
if Container.Reference = null then |
| 227 |
raise Constraint_Error with "container is empty"; |
| 228 |
else |
| 229 |
return Container.Reference.Element.all; |
| 230 |
end if; |
| 231 |
end Element; |
| 232 |
|
| 233 |
-------------- |
| 234 |
-- Finalize -- |
| 235 |
-------------- |
| 236 |
|
| 237 |
overriding procedure Finalize (Container : in out Holder) is |
| 238 |
begin |
| 239 |
if Container.Busy /= 0 then |
| 240 |
raise Program_Error with "attempt to tamper with elements"; |
| 241 |
end if; |
| 242 |
|
| 243 |
if Container.Reference /= null then |
| 244 |
Unreference (Container.Reference); |
| 245 |
Container.Reference := null; |
| 246 |
end if; |
| 247 |
end Finalize; |
| 248 |
|
| 249 |
overriding procedure Finalize (Control : in out Reference_Control_Type) is |
| 250 |
begin |
| 251 |
if Control.Container /= null then |
| 252 |
Unreference (Control.Container.Reference); |
| 253 |
Control.Container.Busy := Control.Container.Busy - 1; |
| 254 |
Control.Container := null; |
| 255 |
end if; |
| 256 |
end Finalize; |
| 257 |
|
| 258 |
-------------- |
| 259 |
-- Is_Empty -- |
| 260 |
-------------- |
| 261 |
|
| 262 |
function Is_Empty (Container : Holder) return Boolean is |
| 263 |
begin |
| 264 |
return Container.Reference = null; |
| 265 |
end Is_Empty; |
| 266 |
|
| 267 |
---------- |
| 268 |
-- Move -- |
| 269 |
---------- |
| 270 |
|
| 271 |
procedure Move (Target : in out Holder; Source : in out Holder) is |
| 272 |
begin |
| 273 |
if Target.Busy /= 0 then |
| 274 |
raise Program_Error with "attempt to tamper with elements"; |
| 275 |
end if; |
| 276 |
|
| 277 |
if Source.Busy /= 0 then |
| 278 |
raise Program_Error with "attempt to tamper with elements"; |
| 279 |
end if; |
| 280 |
|
| 281 |
if Target.Reference /= Source.Reference then |
| 282 |
if Target.Reference /= null then |
| 283 |
Unreference (Target.Reference); |
| 284 |
end if; |
| 285 |
|
| 286 |
Target.Reference := Source.Reference; |
| 287 |
Source.Reference := null; |
| 288 |
end if; |
| 289 |
end Move; |
| 290 |
|
| 291 |
------------------- |
| 292 |
-- Query_Element -- |
| 293 |
------------------- |
| 294 |
|
| 295 |
procedure Query_Element |
| 296 |
(Container : Holder; |
| 297 |
Process : not null access procedure (Element : Element_Type)) |
| 298 |
is |
| 299 |
B : Natural renames Container'Unrestricted_Access.Busy; |
| 300 |
|
| 301 |
begin |
| 302 |
if Container.Reference = null then |
| 303 |
raise Constraint_Error with "container is empty"; |
| 304 |
end if; |
| 305 |
|
| 306 |
B := B + 1; |
| 307 |
|
| 308 |
begin |
| 309 |
Process (Container.Reference.Element.all); |
| 310 |
exception |
| 311 |
when others => |
| 312 |
B := B - 1; |
| 313 |
raise; |
| 314 |
end; |
| 315 |
|
| 316 |
B := B - 1; |
| 317 |
end Query_Element; |
| 318 |
|
| 319 |
--------------- |
| 320 |
-- Put_Image -- |
| 321 |
--------------- |
| 322 |
|
| 323 |
procedure Put_Image |
| 324 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Holder) |
| 325 |
is |
| 326 |
use System.Put_Images; |
| 327 |
begin |
| 328 |
Array_Before (S); |
| 329 |
if not Is_Empty (V) then |
| 330 |
Element_Type'Put_Image (S, Element (V)); |
| 331 |
end if; |
| 332 |
Array_After (S); |
| 333 |
end Put_Image; |
| 334 |
|
| 335 |
---------- |
| 336 |
-- Read -- |
| 337 |
---------- |
| 338 |
|
| 339 |
procedure Read |
| 340 |
(Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 341 |
Container : out Holder) |
| 342 |
is |
| 343 |
begin |
| 344 |
Clear (Container); |
| 345 |
|
| 346 |
if not Boolean'Input (Stream) then |
| 347 |
Container.Reference := |
| 348 |
new Shared_Holder' |
| 349 |
(Counter => <>, |
| 350 |
Element => new Element_Type'(Element_Type'Input (Stream))); |
| 351 |
end if; |
| 352 |
end Read; |
| 353 |
|
| 354 |
procedure Read |
| 355 |
(Stream : not null access Root_Stream_Type'Class; |
| 356 |
Item : out Constant_Reference_Type) |
| 357 |
is |
| 358 |
begin |
| 359 |
raise Program_Error with "attempt to stream reference"; |
| 360 |
end Read; |
| 361 |
|
| 362 |
procedure Read |
| 363 |
(Stream : not null access Root_Stream_Type'Class; |
| 364 |
Item : out Reference_Type) |
| 365 |
is |
| 366 |
begin |
| 367 |
raise Program_Error with "attempt to stream reference"; |
| 368 |
end Read; |
| 369 |
|
| 370 |
--------------- |
| 371 |
-- Reference -- |
| 372 |
--------------- |
| 373 |
|
| 374 |
procedure Reference (Item : not null Shared_Holder_Access) is |
| 375 |
begin |
| 376 |
System.Atomic_Counters.Increment (Item.Counter); |
| 377 |
end Reference; |
| 378 |
|
| 379 |
function Reference |
| 380 |
(Container : aliased in out Holder) return Reference_Type |
| 381 |
is |
| 382 |
begin |
| 383 |
if Container.Reference = null then |
| 384 |
raise Constraint_Error with "container is empty"; |
| 385 |
end if; |
| 386 |
|
| 387 |
Detach (Container); |
| 388 |
|
| 389 |
declare |
| 390 |
Ref : constant Reference_Type := |
| 391 |
(Element => Container.Reference.Element.all'Access, |
| 392 |
Control => (Controlled with Container'Unrestricted_Access)); |
| 393 |
begin |
| 394 |
Reference (Ref.Control.Container.Reference); |
| 395 |
Ref.Control.Container.Busy := Ref.Control.Container.Busy + 1; |
| 396 |
return Ref; |
| 397 |
end; |
| 398 |
end Reference; |
| 399 |
|
| 400 |
--------------------- |
| 401 |
-- Replace_Element -- |
| 402 |
--------------------- |
| 403 |
|
| 404 |
procedure Replace_Element |
| 405 |
(Container : in out Holder; |
| 406 |
New_Item : Element_Type) |
| 407 |
is |
| 408 |
-- Element allocator may need an accessibility check in case actual type |
| 409 |
-- is class-wide or has access discriminants (RM 4.8(10.1) and |
| 410 |
-- AI12-0035). |
| 411 |
|
| 412 |
pragma Unsuppress (Accessibility_Check); |
| 413 |
|
| 414 |
begin |
| 415 |
if Container.Busy /= 0 then |
| 416 |
raise Program_Error with "attempt to tamper with elements"; |
| 417 |
end if; |
| 418 |
|
| 419 |
if Container.Reference = null then |
| 420 |
-- Holder is empty, allocate new Shared_Holder. |
| 421 |
|
| 422 |
Container.Reference := |
| 423 |
new Shared_Holder' |
| 424 |
(Counter => <>, |
| 425 |
Element => new Element_Type'(New_Item)); |
| 426 |
|
| 427 |
elsif System.Atomic_Counters.Is_One (Container.Reference.Counter) then |
| 428 |
-- Shared_Holder can be reused. |
| 429 |
|
| 430 |
Free (Container.Reference.Element); |
| 431 |
Container.Reference.Element := new Element_Type'(New_Item); |
| 432 |
|
| 433 |
else |
| 434 |
Unreference (Container.Reference); |
| 435 |
Container.Reference := |
| 436 |
new Shared_Holder' |
| 437 |
(Counter => <>, |
| 438 |
Element => new Element_Type'(New_Item)); |
| 439 |
end if; |
| 440 |
end Replace_Element; |
| 441 |
|
| 442 |
---------- |
| 443 |
-- Swap -- |
| 444 |
---------- |
| 445 |
|
| 446 |
procedure Swap (Left, Right : in out Holder) is |
| 447 |
begin |
| 448 |
if Left.Busy /= 0 then |
| 449 |
raise Program_Error with "attempt to tamper with elements"; |
| 450 |
end if; |
| 451 |
|
| 452 |
if Right.Busy /= 0 then |
| 453 |
raise Program_Error with "attempt to tamper with elements"; |
| 454 |
end if; |
| 455 |
|
| 456 |
if Left.Reference /= Right.Reference then |
| 457 |
declare |
| 458 |
Tmp : constant Shared_Holder_Access := Left.Reference; |
| 459 |
begin |
| 460 |
Left.Reference := Right.Reference; |
| 461 |
Right.Reference := Tmp; |
| 462 |
end; |
| 463 |
end if; |
| 464 |
end Swap; |
| 465 |
|
| 466 |
--------------- |
| 467 |
-- To_Holder -- |
| 468 |
--------------- |
| 469 |
|
| 470 |
function To_Holder (New_Item : Element_Type) return Holder is |
| 471 |
-- The element allocator may need an accessibility check in the case the |
| 472 |
-- actual type is class-wide or has access discriminants (RM 4.8(10.1) |
| 473 |
-- and AI12-0035). |
| 474 |
|
| 475 |
pragma Unsuppress (Accessibility_Check); |
| 476 |
|
| 477 |
begin |
| 478 |
return |
| 479 |
(Controlled with |
| 480 |
new Shared_Holder' |
| 481 |
(Counter => <>, |
| 482 |
Element => new Element_Type'(New_Item)), 0); |
| 483 |
end To_Holder; |
| 484 |
|
| 485 |
----------------- |
| 486 |
-- Unreference -- |
| 487 |
----------------- |
| 488 |
|
| 489 |
procedure Unreference (Item : not null Shared_Holder_Access) is |
| 490 |
|
| 491 |
procedure Free is |
| 492 |
new Ada.Unchecked_Deallocation (Shared_Holder, Shared_Holder_Access); |
| 493 |
|
| 494 |
Aux : Shared_Holder_Access := Item; |
| 495 |
|
| 496 |
begin |
| 497 |
if System.Atomic_Counters.Decrement (Aux.Counter) then |
| 498 |
Free (Aux.Element); |
| 499 |
Free (Aux); |
| 500 |
end if; |
| 501 |
end Unreference; |
| 502 |
|
| 503 |
-------------------- |
| 504 |
-- Update_Element -- |
| 505 |
-------------------- |
| 506 |
|
| 507 |
procedure Update_Element |
| 508 |
(Container : in out Holder; |
| 509 |
Process : not null access procedure (Element : in out Element_Type)) |
| 510 |
is |
| 511 |
B : Natural renames Container.Busy; |
| 512 |
|
| 513 |
begin |
| 514 |
if Container.Reference = null then |
| 515 |
raise Constraint_Error with "container is empty"; |
| 516 |
end if; |
| 517 |
|
| 518 |
Detach (Container); |
| 519 |
|
| 520 |
B := B + 1; |
| 521 |
|
| 522 |
begin |
| 523 |
Process (Container.Reference.Element.all); |
| 524 |
exception |
| 525 |
when others => |
| 526 |
B := B - 1; |
| 527 |
raise; |
| 528 |
end; |
| 529 |
|
| 530 |
B := B - 1; |
| 531 |
end Update_Element; |
| 532 |
|
| 533 |
----------- |
| 534 |
-- Write -- |
| 535 |
----------- |
| 536 |
|
| 537 |
procedure Write |
| 538 |
(Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 539 |
Container : Holder) |
| 540 |
is |
| 541 |
begin |
| 542 |
Boolean'Output (Stream, Container.Reference = null); |
| 543 |
|
| 544 |
if Container.Reference /= null then |
| 545 |
Element_Type'Output (Stream, Container.Reference.Element.all); |
| 546 |
end if; |
| 547 |
end Write; |
| 548 |
|
| 549 |
procedure Write |
| 550 |
(Stream : not null access Root_Stream_Type'Class; |
| 551 |
Item : Reference_Type) |
| 552 |
is |
| 553 |
begin |
| 554 |
raise Program_Error with "attempt to stream reference"; |
| 555 |
end Write; |
| 556 |
|
| 557 |
procedure Write |
| 558 |
(Stream : not null access Root_Stream_Type'Class; |
| 559 |
Item : Constant_Reference_Type) |
| 560 |
is |
| 561 |
begin |
| 562 |
raise Program_Error with "attempt to stream reference"; |
| 563 |
end Write; |
| 564 |
|
| 565 |
end Ada.Containers.Indefinite_Holders; |