File: posix-shared_memory_objects.adb

package info (click to toggle)
libflorist 2011-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,948 kB
  • sloc: ada: 11,664; ansic: 7,164; makefile: 204
file content (126 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
------------------------------------------------------------------------------
--                                                                          --
--            FLORIST (FSU Implementation of POSIX.5) COMPONENTS            --
--                                                                          --
--           P O S I X . S H A R E D _ M E M O R Y _ O B J E C T S          --
--                                                                          --
--                                  B o d y                                 --
--                                                                          --
--                                                                          --
--             Copyright (C) 1996-1997 Florida State University             --
--                     Copyright (C) 1998-2010, AdaCore                     --
--                                                                          --
--  This file is a component of FLORIST, an  implementation of an  Ada API  --
--  for the POSIX OS services, for use with  the  GNAT  Ada  compiler  and  --
--  the FSU Gnu Ada Runtime Library (GNARL).   The  interface  is intended  --
--  to be close to that specified in  IEEE STD  1003.5: 1990  and IEEE STD  --
--  1003.5b: 1996.                                                          --
--                                                                          --
--  FLORIST is free software;  you can  redistribute  it and/or  modify it  --
--  under terms of the  GNU  General  Public  License as  published by the  --
--  Free Software Foundation;  either version  2, or (at  your option) any  --
--  later version.  FLORIST is distributed  in  the hope  that  it will be  --
--  useful, but WITHOUT ANY WARRANTY;  without  even the implied  warranty  --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See  the GNU  --
--  General Public License for more details.  You  should have  received a  --
--  copy of the GNU General Public License  distributed  with  GNARL;  see  --
--  file  COPYING.  If not,  write to  the  Free  Software  Foundation, 59  --
--  Temple Place - Suite 330, Boston, MA 02111-1307, USA.                   --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
------------------------------------------------------------------------------

with POSIX.C,
     POSIX.Implementation,
     POSIX.Permissions.Implementation,
     Unchecked_Conversion;

package body POSIX.Shared_Memory_Objects is

   use POSIX.C;
   use POSIX.Implementation;
   use POSIX.Permissions.Implementation;

   function To_int is new Unchecked_Conversion (Bits, int);

   C_File_Mode : constant array (POSIX.IO.File_Mode) of Bits :=
     (POSIX.IO.Read_Only  => O_RDONLY,
      POSIX.IO.Write_Only => O_WRONLY,
      POSIX.IO.Read_Write => O_RDWR);

   --------------------------
   --  Open_Shared_Memory  --
   --------------------------

   function shm_open
     (name  : char_ptr;
      oflag : int;
      mode  : mode_t) return int;
   pragma Import (C, shm_open, shm_open_LINKNAME);

   function Open_Shared_Memory
     (Name           : POSIX.POSIX_String;
      Mode           : POSIX.IO.File_Mode;
      Options        : POSIX.IO.Open_Option_Set := POSIX.IO.Empty_Set;
      Masked_Signals : POSIX.Signal_Masking := POSIX.RTS_Signals)
      return POSIX.IO.File_Descriptor is
      Old_Mask : aliased Signal_Mask;
      Name_With_NUL : POSIX_String := Name & NUL;
      Result : POSIX.IO.File_Descriptor;
   begin
      Mask_Signals (Masked_Signals, Old_Mask'Unchecked_Access);
      Result := POSIX.IO.File_Descriptor (Check (shm_open
        (Name_With_NUL (Name_With_NUL'First)'Unchecked_Access,
         To_int (Option_Set (Options).Option or C_File_Mode (Mode)),
         0), Old_Mask'Unchecked_Access));
      Check_NNeg_And_Restore_Signals
        (int (Result), Masked_Signals, Old_Mask'Unchecked_Access);
      return Result;
   end Open_Shared_Memory;

   ------------------------------------
   --  Open_Or_Create_Shared_Memory  --
   ------------------------------------

   function Open_Or_Create_Shared_Memory
     (Name           : POSIX.POSIX_String;
      Mode           : POSIX.IO.File_Mode;
      Permissions    : POSIX.Permissions.Permission_Set;
      Options        : POSIX.IO.Open_Option_Set := POSIX.IO.Empty_Set;
      Masked_Signals : POSIX.Signal_Masking := POSIX.RTS_Signals)
      return POSIX.IO.File_Descriptor is
      Old_Mask : aliased Signal_Mask;
      Name_With_NUL : POSIX_String := Name & NUL;
      Result : POSIX.IO.File_Descriptor;
   begin
      Mask_Signals (Masked_Signals, Old_Mask'Unchecked_Access);
      Result := POSIX.IO.File_Descriptor (Check (shm_open
        (Name_With_NUL (Name_With_NUL'First)'Unchecked_Access,
         To_int (Option_Set (Options).Option or C_File_Mode (Mode) or O_CREAT),
         Form_C_Permission (Permissions)), Old_Mask'Unchecked_Access));
      Check_NNeg_And_Restore_Signals
        (int (Result), Masked_Signals, Old_Mask'Unchecked_Access);
      return Result;
   end Open_Or_Create_Shared_Memory;

   ----------------------------
   --  Unlink_Shared_Memory  --
   ----------------------------

   procedure Unlink_Shared_Memory
     (Name : POSIX.POSIX_String) is
      Name_With_NUL : POSIX_String := Name & NUL;
      function shm_unlink (name : char_ptr) return int;
      pragma Import (C, shm_unlink, shm_unlink_LINKNAME);
   begin
      Check (shm_unlink
        (Name_With_NUL (Name_With_NUL'First)'Unchecked_Access));
   end Unlink_Shared_Memory;

end POSIX.Shared_Memory_Objects;