| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- I N T E R F A C E S . C . P O I N T E R S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with Interfaces.C.Strings; use Interfaces.C.Strings; |
| 33 |
with System.Storage_Elements; use System.Storage_Elements; |
| 34 |
with System; use System; |
| 35 |
|
| 36 |
with Ada.Unchecked_Conversion; |
| 37 |
|
| 38 |
package body Interfaces.C.Pointers is |
| 39 |
|
| 40 |
subtype Offset is Storage_Offset; |
| 41 |
|
| 42 |
function To_Pointer is new Ada.Unchecked_Conversion (Address, Pointer); |
| 43 |
function To_Addr is new Ada.Unchecked_Conversion (Pointer, Address); |
| 44 |
function To_Offset is new Ada.Unchecked_Conversion (ptrdiff_t, Offset); |
| 45 |
function To_Ptrdiff is new Ada.Unchecked_Conversion (Offset, ptrdiff_t); |
| 46 |
|
| 47 |
Elmt_Size : constant ptrdiff_t := |
| 48 |
(Element_Array'Component_Size |
| 49 |
+ Storage_Unit - 1) / Storage_Unit; |
| 50 |
|
| 51 |
subtype Index_Base is Index'Base; |
| 52 |
|
| 53 |
--------- |
| 54 |
-- "+" -- |
| 55 |
--------- |
| 56 |
|
| 57 |
function "+" (Left : Pointer; Right : ptrdiff_t) return Pointer is |
| 58 |
begin |
| 59 |
if Left = null then |
| 60 |
raise Pointer_Error; |
| 61 |
end if; |
| 62 |
|
| 63 |
return To_Pointer (To_Addr (Left) + To_Offset (Elmt_Size * Right)); |
| 64 |
end "+"; |
| 65 |
|
| 66 |
function "+" (Left : ptrdiff_t; Right : Pointer) return Pointer is |
| 67 |
begin |
| 68 |
if Right = null then |
| 69 |
raise Pointer_Error; |
| 70 |
end if; |
| 71 |
|
| 72 |
return To_Pointer (To_Offset (Elmt_Size * Left) + To_Addr (Right)); |
| 73 |
end "+"; |
| 74 |
|
| 75 |
--------- |
| 76 |
-- "-" -- |
| 77 |
--------- |
| 78 |
|
| 79 |
function "-" (Left : Pointer; Right : ptrdiff_t) return Pointer is |
| 80 |
begin |
| 81 |
if Left = null then |
| 82 |
raise Pointer_Error; |
| 83 |
end if; |
| 84 |
|
| 85 |
return To_Pointer (To_Addr (Left) - To_Offset (Right * Elmt_Size)); |
| 86 |
end "-"; |
| 87 |
|
| 88 |
function "-" (Left : Pointer; Right : Pointer) return ptrdiff_t is |
| 89 |
begin |
| 90 |
if Left = null or else Right = null then |
| 91 |
raise Pointer_Error; |
| 92 |
end if; |
| 93 |
|
| 94 |
return To_Ptrdiff (To_Addr (Left) - To_Addr (Right)) / Elmt_Size; |
| 95 |
end "-"; |
| 96 |
|
| 97 |
---------------- |
| 98 |
-- Copy_Array -- |
| 99 |
---------------- |
| 100 |
|
| 101 |
procedure Copy_Array |
| 102 |
(Source : Pointer; |
| 103 |
Target : Pointer; |
| 104 |
Length : ptrdiff_t) |
| 105 |
is |
| 106 |
T : Pointer; |
| 107 |
S : Pointer; |
| 108 |
|
| 109 |
begin |
| 110 |
if Source = null or else Target = null then |
| 111 |
raise Dereference_Error; |
| 112 |
|
| 113 |
-- Forward copy |
| 114 |
|
| 115 |
elsif To_Addr (Target) <= To_Addr (Source) then |
| 116 |
T := Target; |
| 117 |
S := Source; |
| 118 |
for J in 1 .. Length loop |
| 119 |
T.all := S.all; |
| 120 |
Increment (T); |
| 121 |
Increment (S); |
| 122 |
end loop; |
| 123 |
|
| 124 |
-- Backward copy |
| 125 |
|
| 126 |
else |
| 127 |
T := Target + Length; |
| 128 |
S := Source + Length; |
| 129 |
for J in 1 .. Length loop |
| 130 |
Decrement (T); |
| 131 |
Decrement (S); |
| 132 |
T.all := S.all; |
| 133 |
end loop; |
| 134 |
end if; |
| 135 |
end Copy_Array; |
| 136 |
|
| 137 |
--------------------------- |
| 138 |
-- Copy_Terminated_Array -- |
| 139 |
--------------------------- |
| 140 |
|
| 141 |
procedure Copy_Terminated_Array |
| 142 |
(Source : Pointer; |
| 143 |
Target : Pointer; |
| 144 |
Limit : ptrdiff_t := ptrdiff_t'Last; |
| 145 |
Terminator : Element := Default_Terminator) |
| 146 |
is |
| 147 |
L : ptrdiff_t; |
| 148 |
S : Pointer := Source; |
| 149 |
|
| 150 |
begin |
| 151 |
if Source = null or Target = null then |
| 152 |
raise Dereference_Error; |
| 153 |
end if; |
| 154 |
|
| 155 |
-- Compute array limited length (including the terminator) |
| 156 |
|
| 157 |
L := 0; |
| 158 |
while L < Limit loop |
| 159 |
L := L + 1; |
| 160 |
exit when S.all = Terminator; |
| 161 |
Increment (S); |
| 162 |
end loop; |
| 163 |
|
| 164 |
Copy_Array (Source, Target, L); |
| 165 |
end Copy_Terminated_Array; |
| 166 |
|
| 167 |
--------------- |
| 168 |
-- Decrement -- |
| 169 |
--------------- |
| 170 |
|
| 171 |
procedure Decrement (Ref : in out Pointer) is |
| 172 |
begin |
| 173 |
Ref := Ref - 1; |
| 174 |
end Decrement; |
| 175 |
|
| 176 |
--------------- |
| 177 |
-- Increment -- |
| 178 |
--------------- |
| 179 |
|
| 180 |
procedure Increment (Ref : in out Pointer) is |
| 181 |
begin |
| 182 |
Ref := Ref + 1; |
| 183 |
end Increment; |
| 184 |
|
| 185 |
----------- |
| 186 |
-- Value -- |
| 187 |
----------- |
| 188 |
|
| 189 |
function Value |
| 190 |
(Ref : Pointer; |
| 191 |
Terminator : Element := Default_Terminator) return Element_Array |
| 192 |
is |
| 193 |
P : Pointer; |
| 194 |
L : constant Index_Base := Index'First; |
| 195 |
H : Index_Base; |
| 196 |
|
| 197 |
begin |
| 198 |
if Ref = null then |
| 199 |
raise Dereference_Error; |
| 200 |
|
| 201 |
else |
| 202 |
H := L; |
| 203 |
P := Ref; |
| 204 |
|
| 205 |
loop |
| 206 |
exit when P.all = Terminator; |
| 207 |
H := Index_Base'Succ (H); |
| 208 |
Increment (P); |
| 209 |
end loop; |
| 210 |
|
| 211 |
declare |
| 212 |
subtype A is Element_Array (L .. H); |
| 213 |
|
| 214 |
type PA is access A; |
| 215 |
for PA'Size use System.Parameters.ptr_bits; |
| 216 |
function To_PA is new Ada.Unchecked_Conversion (Pointer, PA); |
| 217 |
|
| 218 |
begin |
| 219 |
return To_PA (Ref).all; |
| 220 |
end; |
| 221 |
end if; |
| 222 |
end Value; |
| 223 |
|
| 224 |
function Value |
| 225 |
(Ref : Pointer; |
| 226 |
Length : ptrdiff_t) return Element_Array |
| 227 |
is |
| 228 |
L : Index_Base; |
| 229 |
H : Index_Base; |
| 230 |
|
| 231 |
begin |
| 232 |
if Ref = null then |
| 233 |
raise Dereference_Error; |
| 234 |
|
| 235 |
-- For length zero, we need to return a null slice, but we can't make |
| 236 |
-- the bounds of this slice Index'First, since this could cause a |
| 237 |
-- Constraint_Error if Index'First = Index'Base'First. |
| 238 |
|
| 239 |
elsif Length <= 0 then |
| 240 |
declare |
| 241 |
pragma Warnings (Off); -- kill warnings since X not assigned |
| 242 |
X : Element_Array (Index'Succ (Index'First) .. Index'First); |
| 243 |
pragma Warnings (On); |
| 244 |
|
| 245 |
begin |
| 246 |
return X; |
| 247 |
end; |
| 248 |
|
| 249 |
-- Normal case (length non-zero) |
| 250 |
|
| 251 |
else |
| 252 |
L := Index'First; |
| 253 |
H := Index'Val (Index'Pos (Index'First) + Length - 1); |
| 254 |
|
| 255 |
declare |
| 256 |
subtype A is Element_Array (L .. H); |
| 257 |
|
| 258 |
type PA is access A; |
| 259 |
for PA'Size use System.Parameters.ptr_bits; |
| 260 |
function To_PA is new Ada.Unchecked_Conversion (Pointer, PA); |
| 261 |
|
| 262 |
begin |
| 263 |
return To_PA (Ref).all; |
| 264 |
end; |
| 265 |
end if; |
| 266 |
end Value; |
| 267 |
|
| 268 |
-------------------- |
| 269 |
-- Virtual_Length -- |
| 270 |
-------------------- |
| 271 |
|
| 272 |
function Virtual_Length |
| 273 |
(Ref : Pointer; |
| 274 |
Terminator : Element := Default_Terminator) return ptrdiff_t |
| 275 |
is |
| 276 |
P : Pointer; |
| 277 |
C : ptrdiff_t; |
| 278 |
|
| 279 |
begin |
| 280 |
if Ref = null then |
| 281 |
raise Dereference_Error; |
| 282 |
|
| 283 |
else |
| 284 |
C := 0; |
| 285 |
P := Ref; |
| 286 |
|
| 287 |
while P.all /= Terminator loop |
| 288 |
C := C + 1; |
| 289 |
Increment (P); |
| 290 |
end loop; |
| 291 |
|
| 292 |
return C; |
| 293 |
end if; |
| 294 |
end Virtual_Length; |
| 295 |
|
| 296 |
end Interfaces.C.Pointers; |