Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / ada / libgnat / s-shasto.adb
blobbe2f20bc688916c81859974147df240cf5772191
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . S H A R E D _ M E M O R Y --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2023, 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 ------------------------------------------------------------------------------
32 with Ada.IO_Exceptions;
33 with Ada.Streams;
34 with Ada.Streams.Stream_IO;
36 with System.Global_Locks;
37 with System.Soft_Links;
39 with System.CRTL;
40 with System.File_Control_Block;
41 with System.File_IO;
42 with System.HTable;
44 with Ada.Unchecked_Deallocation;
45 with Ada.Unchecked_Conversion;
47 package body System.Shared_Storage is
49 package AS renames Ada.Streams;
51 package IOX renames Ada.IO_Exceptions;
53 package FCB renames System.File_Control_Block;
55 package SFI renames System.File_IO;
57 package SIO renames Ada.Streams.Stream_IO;
59 type String_Access is access String;
60 procedure Free is new Ada.Unchecked_Deallocation
61 (Object => String, Name => String_Access);
63 Dir : String_Access;
64 -- Holds the directory
66 ------------------------------------------------
67 -- Variables for Shared Variable Access Files --
68 ------------------------------------------------
70 Max_Shared_Var_Files : constant := 20;
71 -- Maximum number of lock files that can be open
73 Shared_Var_Files_Open : Natural := 0;
74 -- Number of shared variable access files currently open
76 type File_Stream_Type is new AS.Root_Stream_Type with record
77 File : SIO.File_Type;
78 end record;
79 type File_Stream_Access is access all File_Stream_Type'Class;
81 procedure Read
82 (Stream : in out File_Stream_Type;
83 Item : out AS.Stream_Element_Array;
84 Last : out AS.Stream_Element_Offset);
86 procedure Write
87 (Stream : in out File_Stream_Type;
88 Item : AS.Stream_Element_Array);
90 subtype Hash_Header is Natural range 0 .. 30;
91 -- Number of hash headers, related (for efficiency purposes only) to the
92 -- maximum number of lock files.
94 type Shared_Var_File_Entry;
95 type Shared_Var_File_Entry_Ptr is access Shared_Var_File_Entry;
97 type Shared_Var_File_Entry is record
98 Name : String_Access;
99 -- Name of variable, as passed to Read_File/Write_File routines
101 Stream : File_Stream_Access;
102 -- Stream_IO file for the shared variable file
104 Next : Shared_Var_File_Entry_Ptr;
105 Prev : Shared_Var_File_Entry_Ptr;
106 -- Links for LRU chain
107 end record;
109 procedure Free is new Ada.Unchecked_Deallocation
110 (Object => Shared_Var_File_Entry,
111 Name => Shared_Var_File_Entry_Ptr);
113 procedure Free is new Ada.Unchecked_Deallocation
114 (Object => File_Stream_Type'Class,
115 Name => File_Stream_Access);
117 function To_AFCB_Ptr is
118 new Ada.Unchecked_Conversion (SIO.File_Type, FCB.AFCB_Ptr);
120 LRU_Head : Shared_Var_File_Entry_Ptr;
121 LRU_Tail : Shared_Var_File_Entry_Ptr;
122 -- As lock files are opened, they are organized into a least recently
123 -- used chain, which is a doubly linked list using the Next and Prev
124 -- fields of Shared_Var_File_Entry records. The field LRU_Head points
125 -- to the least recently used entry, whose prev pointer is null, and
126 -- LRU_Tail points to the most recently used entry, whose next pointer
127 -- is null. These pointers are null only if the list is empty.
129 function Hash (F : String_Access) return Hash_Header;
130 function Equal (F1, F2 : String_Access) return Boolean;
131 -- Hash and equality functions for hash table
133 package SFT is new System.HTable.Simple_HTable
134 (Header_Num => Hash_Header,
135 Element => Shared_Var_File_Entry_Ptr,
136 No_Element => null,
137 Key => String_Access,
138 Hash => Hash,
139 Equal => Equal);
141 --------------------------------
142 -- Variables for Lock Control --
143 --------------------------------
145 Global_Lock : Global_Locks.Lock_Type;
147 Lock_Count : Natural := 0;
148 -- Counts nesting of lock calls, 0 means lock is not held
150 -----------------------
151 -- Local Subprograms --
152 -----------------------
154 procedure Initialize;
155 -- Called to initialize data structures for this package.
156 -- Has no effect except on the first call.
158 procedure Enter_SFE (SFE : Shared_Var_File_Entry_Ptr; Fname : String);
159 -- The first parameter is a pointer to a newly allocated SFE, whose
160 -- File field is already set appropriately. Fname is the name of the
161 -- variable as passed to Shared_Var_RFile/Shared_Var_WFile. Enter_SFE
162 -- completes the SFE value, and enters it into the hash table. If the
163 -- hash table is already full, the least recently used entry is first
164 -- closed and discarded.
166 function Retrieve (File : String) return Shared_Var_File_Entry_Ptr;
167 -- Given a file name, this function searches the hash table to see if
168 -- the file is currently open. If so, then a pointer to the already
169 -- created entry is returned, after first moving it to the head of
170 -- the LRU chain. If not, then null is returned.
172 function Shared_Var_ROpen (Var : String) return SIO.Stream_Access;
173 -- As described above, this routine returns null if the
174 -- corresponding shared storage does not exist, and otherwise, if
175 -- the storage does exist, a Stream_Access value that references
176 -- the shared storage, ready to read the current value.
178 function Shared_Var_WOpen (Var : String) return SIO.Stream_Access;
179 -- As described above, this routine returns a Stream_Access value
180 -- that references the shared storage, ready to write the new
181 -- value. The storage is created by this call if it does not
182 -- already exist.
184 procedure Shared_Var_Close (Var : SIO.Stream_Access);
185 -- This routine signals the end of a read/assign operation. It can
186 -- be useful to embrace a read/write operation between a call to
187 -- open and a call to close which protect the whole operation.
188 -- Otherwise, two simultaneous operations can result in the
189 -- raising of exception Data_Error by setting the access mode of
190 -- the variable in an incorrect mode.
192 ---------------
193 -- Enter_SFE --
194 ---------------
196 procedure Enter_SFE (SFE : Shared_Var_File_Entry_Ptr; Fname : String) is
197 Freed : Shared_Var_File_Entry_Ptr;
199 begin
200 SFE.Name := new String'(Fname);
202 -- Release least recently used entry if we have to
204 if Shared_Var_Files_Open = Max_Shared_Var_Files then
205 Freed := LRU_Head;
207 if Freed.Next /= null then
208 Freed.Next.Prev := null;
209 end if;
211 LRU_Head := Freed.Next;
212 SFT.Remove (Freed.Name);
213 SIO.Close (Freed.Stream.File);
214 Free (Freed.Name);
215 Free (Freed.Stream);
216 Free (Freed);
218 else
219 Shared_Var_Files_Open := Shared_Var_Files_Open + 1;
220 end if;
222 -- Add new entry to hash table
224 SFT.Set (SFE.Name, SFE);
226 -- Add new entry at end of LRU chain
228 if LRU_Head = null then
229 LRU_Head := SFE;
230 LRU_Tail := SFE;
232 else
233 SFE.Prev := LRU_Tail;
234 LRU_Tail.Next := SFE;
235 LRU_Tail := SFE;
236 end if;
237 end Enter_SFE;
239 -----------
240 -- Equal --
241 -----------
243 function Equal (F1, F2 : String_Access) return Boolean is
244 begin
245 return F1.all = F2.all;
246 end Equal;
248 ----------
249 -- Hash --
250 ----------
252 function Hash (F : String_Access) return Hash_Header is
253 N : Natural := 0;
255 begin
256 -- Add up characters of name, mod our table size
258 for J in F'Range loop
259 N := (N + Character'Pos (F (J))) mod (Hash_Header'Last + 1);
260 end loop;
262 return N;
263 end Hash;
265 ----------------
266 -- Initialize --
267 ----------------
269 procedure Initialize is
270 procedure Get_Env_Value_Ptr (Name, Length, Ptr : Address);
271 pragma Import (C, Get_Env_Value_Ptr, "__gnat_getenv");
273 subtype size_t is CRTL.size_t;
275 procedure Strncpy (dest, src : System.Address; n : size_t)
276 renames CRTL.strncpy;
278 Dir_Name : aliased constant String :=
279 "SHARED_MEMORY_DIRECTORY" & ASCII.NUL;
281 Env_Value_Ptr : aliased Address;
282 Env_Value_Len : aliased Integer;
284 begin
285 if Dir = null then
286 Get_Env_Value_Ptr
287 (Dir_Name'Address, Env_Value_Len'Address, Env_Value_Ptr'Address);
289 Dir := new String (1 .. Env_Value_Len);
291 if Env_Value_Len > 0 then
292 Strncpy (Dir.all'Address, Env_Value_Ptr, size_t (Env_Value_Len));
293 end if;
295 System.Global_Locks.Create_Lock (Global_Lock, Dir.all & "__lock");
296 end if;
297 end Initialize;
299 ----------
300 -- Read --
301 ----------
303 procedure Read
304 (Stream : in out File_Stream_Type;
305 Item : out AS.Stream_Element_Array;
306 Last : out AS.Stream_Element_Offset)
308 begin
309 SIO.Read (Stream.File, Item, Last);
311 exception when others =>
312 Last := Item'Last;
313 end Read;
315 --------------
316 -- Retrieve --
317 --------------
319 function Retrieve (File : String) return Shared_Var_File_Entry_Ptr is
320 SFE : Shared_Var_File_Entry_Ptr;
322 begin
323 Initialize;
324 SFE := SFT.Get (File'Unrestricted_Access);
326 if SFE /= null then
328 -- Move to head of LRU chain
330 if SFE = LRU_Tail then
331 null;
333 elsif SFE = LRU_Head then
334 LRU_Head := LRU_Head.Next;
335 LRU_Head.Prev := null;
337 else
338 SFE.Next.Prev := SFE.Prev;
339 SFE.Prev.Next := SFE.Next;
340 end if;
342 SFE.Next := null;
343 SFE.Prev := LRU_Tail;
344 LRU_Tail.Next := SFE;
345 LRU_Tail := SFE;
346 end if;
348 return SFE;
349 end Retrieve;
351 ----------------------
352 -- Shared_Var_Close --
353 ----------------------
355 procedure Shared_Var_Close (Var : SIO.Stream_Access) is
356 pragma Warnings (Off, Var);
358 begin
359 System.Soft_Links.Unlock_Task.all;
360 end Shared_Var_Close;
362 ---------------------
363 -- Shared_Var_Lock --
364 ---------------------
366 procedure Shared_Var_Lock (Var : String) is
367 pragma Warnings (Off, Var);
369 begin
370 System.Soft_Links.Lock_Task.all;
371 Initialize;
373 if Lock_Count /= 0 then
374 Lock_Count := Lock_Count + 1;
375 System.Soft_Links.Unlock_Task.all;
377 else
378 Lock_Count := 1;
379 System.Soft_Links.Unlock_Task.all;
380 System.Global_Locks.Acquire_Lock (Global_Lock);
381 end if;
383 exception
384 when others =>
385 System.Soft_Links.Unlock_Task.all;
386 raise;
387 end Shared_Var_Lock;
389 ----------------------
390 -- Shared_Var_Procs --
391 ----------------------
393 package body Shared_Var_Procs is
395 use type SIO.Stream_Access;
397 ----------
398 -- Read --
399 ----------
401 procedure Read is
402 S : SIO.Stream_Access := null;
403 begin
404 S := Shared_Var_ROpen (Full_Name);
405 if S /= null then
406 Typ'Read (S, V);
407 Shared_Var_Close (S);
408 end if;
409 end Read;
411 ------------
412 -- Write --
413 ------------
415 procedure Write is
416 S : SIO.Stream_Access := null;
417 begin
418 S := Shared_Var_WOpen (Full_Name);
419 Typ'Write (S, V);
420 Shared_Var_Close (S);
421 return;
422 end Write;
424 end Shared_Var_Procs;
426 ----------------------
427 -- Shared_Var_ROpen --
428 ----------------------
430 function Shared_Var_ROpen (Var : String) return SIO.Stream_Access is
431 SFE : Shared_Var_File_Entry_Ptr;
433 use type Ada.Streams.Stream_IO.File_Mode;
435 begin
436 System.Soft_Links.Lock_Task.all;
437 SFE := Retrieve (Var);
439 -- Here if file is not already open, try to open it
441 if SFE = null then
442 declare
443 S : aliased constant String := Dir.all & Var;
445 begin
446 SFE := new Shared_Var_File_Entry;
447 SFE.Stream := new File_Stream_Type;
448 SIO.Open (SFE.Stream.File, SIO.In_File, Name => S);
449 SFI.Make_Unbuffered (To_AFCB_Ptr (SFE.Stream.File));
451 -- File opened successfully, put new entry in hash table. Note
452 -- that in this case, file is positioned correctly for read.
454 Enter_SFE (SFE, Var);
456 exception
457 -- If we get an exception, it means that the file does not
458 -- exist, and in this case, we don't need the SFE and we
459 -- return null;
461 when IOX.Name_Error =>
462 Free (SFE);
463 System.Soft_Links.Unlock_Task.all;
464 return null;
465 end;
467 -- Here if file is already open, set file for reading
469 else
470 if SIO.Mode (SFE.Stream.File) /= SIO.In_File then
471 SIO.Set_Mode (SFE.Stream.File, SIO.In_File);
472 SFI.Make_Unbuffered (To_AFCB_Ptr (SFE.Stream.File));
473 end if;
475 SIO.Set_Index (SFE.Stream.File, 1);
476 end if;
478 return SIO.Stream_Access (SFE.Stream);
480 exception
481 when others =>
482 System.Soft_Links.Unlock_Task.all;
483 raise;
484 end Shared_Var_ROpen;
486 -----------------------
487 -- Shared_Var_Unlock --
488 -----------------------
490 procedure Shared_Var_Unlock (Var : String) is
491 pragma Warnings (Off, Var);
493 begin
494 System.Soft_Links.Lock_Task.all;
495 Initialize;
496 Lock_Count := Lock_Count - 1;
498 if Lock_Count = 0 then
499 System.Global_Locks.Release_Lock (Global_Lock);
500 end if;
501 System.Soft_Links.Unlock_Task.all;
503 exception
504 when others =>
505 System.Soft_Links.Unlock_Task.all;
506 raise;
507 end Shared_Var_Unlock;
509 ----------------------
510 -- Shared_Var_WOpen --
511 ----------------------
513 function Shared_Var_WOpen (Var : String) return SIO.Stream_Access is
514 SFE : Shared_Var_File_Entry_Ptr;
516 use type Ada.Streams.Stream_IO.File_Mode;
518 begin
519 System.Soft_Links.Lock_Task.all;
520 SFE := Retrieve (Var);
522 if SFE = null then
523 declare
524 S : aliased constant String := Dir.all & Var;
526 begin
527 SFE := new Shared_Var_File_Entry;
528 SFE.Stream := new File_Stream_Type;
529 SIO.Open (SFE.Stream.File, SIO.Out_File, Name => S);
530 SFI.Make_Unbuffered (To_AFCB_Ptr (SFE.Stream.File));
532 exception
533 -- If we get an exception, it means that the file does not
534 -- exist, and in this case, we create the file.
536 when IOX.Name_Error =>
538 begin
539 SIO.Create (SFE.Stream.File, SIO.Out_File, Name => S);
541 exception
542 -- Error if we cannot create the file
544 when others =>
545 raise Program_Error with
546 "cannot create shared variable file for """ & S & '"';
547 end;
548 end;
550 -- Make new hash table entry for opened/created file. Note that
551 -- in both cases, the file is already in write mode at the start
552 -- of the file, ready to be written.
554 Enter_SFE (SFE, Var);
556 -- Here if file is already open, set file for writing
558 else
559 if SIO.Mode (SFE.Stream.File) /= SIO.Out_File then
560 SIO.Set_Mode (SFE.Stream.File, SIO.Out_File);
561 SFI.Make_Unbuffered (To_AFCB_Ptr (SFE.Stream.File));
562 end if;
564 SIO.Set_Index (SFE.Stream.File, 1);
565 end if;
567 return SIO.Stream_Access (SFE.Stream);
569 exception
570 when others =>
571 System.Soft_Links.Unlock_Task.all;
572 raise;
573 end Shared_Var_WOpen;
575 -----------
576 -- Write --
577 -----------
579 procedure Write
580 (Stream : in out File_Stream_Type;
581 Item : AS.Stream_Element_Array)
583 begin
584 SIO.Write (Stream.File, Item);
585 end Write;
587 end System.Shared_Storage;