Debian Codegraph @

gcc/ada/libgnat/a-conhel.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . H E L P E R S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2015-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 package body Ada.Containers.Helpers is
29
30 Max_Count : constant := 2**31 - 1;
31 -- Used in assertions below, to make sure the counts don't wrap around.
32 -- This can help detect bugs in which Adjust and Finalize calls are
33 -- improperly generated. An extra Decrement could otherwise cause
34 -- wraparound from 0 to 2**32-1. The highest count seen so far is
35 -- around 25, so this should be plenty.
36
37 package body Generic_Implementation is
38
39 ------------
40 -- Adjust --
41 ------------
42
43 procedure Adjust (Control : in out Reference_Control_Type) is
44 begin
45 if Control.T_Counts /= null then
46 Busy (Control.T_Counts.all);
47 end if;
48 end Adjust;
49
50 ----------
51 -- Busy --
52 ----------
53
54 procedure Busy (T_Counts : in out Tamper_Counts) is
55 begin
56 if T_Check then
57 SAC.Increment (T_Counts.Busy);
58 pragma Assert (T_Counts.Busy <= Max_Count);
59 end if;
60 end Busy;
61
62 --------------
63 -- Finalize --
64 --------------
65
66 procedure Finalize (Control : in out Reference_Control_Type) is
67 begin
68 if Control.T_Counts /= null then
69 Unbusy (Control.T_Counts.all);
70 Control.T_Counts := null;
71 end if;
72 end Finalize;
73
74 -- No need to protect against double Finalize here, because these types
75 -- are limited.
76
77 procedure Finalize (Busy : in out With_Busy) is
78 pragma Warnings (Off);
79 pragma Assert (T_Check); -- not called if check suppressed
80 pragma Warnings (On);
81 begin
82 Unbusy (Busy.T_Counts.all);
83 end Finalize;
84
85 procedure Finalize (Lock : in out With_Lock) is
86 pragma Warnings (Off);
87 pragma Assert (T_Check); -- not called if check suppressed
88 pragma Warnings (On);
89 begin
90 Unlock (Lock.T_Counts.all);
91 end Finalize;
92
93 ----------------
94 -- Initialize --
95 ----------------
96
97 procedure Initialize (Busy : in out With_Busy) is
98 pragma Warnings (Off);
99 pragma Assert (T_Check); -- not called if check suppressed
100 pragma Warnings (On);
101 begin
102 Generic_Implementation.Busy (Busy.T_Counts.all);
103 end Initialize;
104
105 procedure Initialize (Lock : in out With_Lock) is
106 pragma Warnings (Off);
107 pragma Assert (T_Check); -- not called if check suppressed
108 pragma Warnings (On);
109 begin
110 Generic_Implementation.Lock (Lock.T_Counts.all);
111 end Initialize;
112
113 ----------
114 -- Lock --
115 ----------
116
117 procedure Lock (T_Counts : in out Tamper_Counts) is
118 begin
119 if T_Check then
120 SAC.Increment (T_Counts.Lock);
121 pragma Assert (T_Counts.Lock <= Max_Count);
122 SAC.Increment (T_Counts.Busy);
123 pragma Assert (T_Counts.Busy <= Max_Count);
124 end if;
125 end Lock;
126
127 --------------
128 -- TC_Check --
129 --------------
130
131 procedure TC_Check (T_Counts : Tamper_Counts) is
132 begin
133 if T_Check then
134 if Is_Busy (T_Counts) then
135 raise Program_Error with
136 "attempt to tamper with cursors";
137 end if;
138
139 -- The lock status (which monitors "element tampering") always
140 -- implies that the busy status (which monitors "cursor
141 -- tampering") is set too; this is a representation invariant.
142 -- Thus if the busy count is zero, then the lock count
143 -- must also be zero.
144
145 pragma Assert (not Is_Locked (T_Counts));
146 end if;
147 end TC_Check;
148
149 --------------
150 -- TE_Check --
151 --------------
152
153 procedure TE_Check (T_Counts : Tamper_Counts) is
154 begin
155 if T_Check and then Is_Locked (T_Counts) then
156 raise Program_Error with
157 "attempt to tamper with elements";
158 end if;
159 end TE_Check;
160
161 ------------
162 -- Unbusy --
163 ------------
164
165 procedure Unbusy (T_Counts : in out Tamper_Counts) is
166 begin
167 if T_Check then
168 SAC.Decrement (T_Counts.Busy);
169 pragma Assert (T_Counts.Busy <= Max_Count);
170 end if;
171 end Unbusy;
172
173 ------------
174 -- Unlock --
175 ------------
176
177 procedure Unlock (T_Counts : in out Tamper_Counts) is
178 begin
179 if T_Check then
180 SAC.Decrement (T_Counts.Lock);
181 pragma Assert (T_Counts.Lock <= Max_Count);
182 SAC.Decrement (T_Counts.Busy);
183 pragma Assert (T_Counts.Busy <= Max_Count);
184 end if;
185 end Unlock;
186
187 -----------------
188 -- Zero_Counts --
189 -----------------
190
191 procedure Zero_Counts (T_Counts : out Tamper_Counts) is
192 begin
193 if T_Check then
194 T_Counts := (others => <>);
195 end if;
196 end Zero_Counts;
197
198 end Generic_Implementation;
199
200 end Ada.Containers.Helpers;