florist_blady_6.1.0_05ac0091/libsrc/posix-memory_mapping.adb

  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
------------------------------------------------------------------------------
--                                                                          --
--            FLORIST (FSU Implementation of POSIX.5) COMPONENTS            --
--                                                                          --
--                  P O S I X . M E M O R Y _ M A P P I N G                 --
--                                                                          --
--                                  B o d y                                 --
--                                                                          --
--                                                                          --
--             Copyright (C) 1996-1997 Florida State University             --
--                     Copyright (C) 1998-2014, 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.Implementation,
     Unchecked_Conversion;

package body POSIX.Memory_Mapping is

   use POSIX.Implementation;
   use System;

   function To_Address is
     new Unchecked_Conversion (ptr_as_int, System.Address);

   Zero_Address : constant System.Address := To_Address (0);
   Failure      : constant System.Address := To_Address (MAP_FAILED);

   ------------------
   --  Map_Memory  --
   ------------------

   function mmap
     (addr : System.Address;
      len : size_t;
      prot : int;
      flags : int;
      fildes : int;
      off : off_t) return System.Address;
   pragma Import (C, mmap, mmap_LINKNAME);

   function Map_Memory
     (First      : System.Address;
      Length     : System.Storage_Elements.Storage_Offset;
      Protection : Protection_Options;
      Mapping    : Mapping_Options;
      Location   : Location_Options;
      File       : POSIX.IO.File_Descriptor;
      Offset     : POSIX.IO.IO_Offset)
      return System.Address is
      Result : System.Address;
   begin
      Result := mmap
       (First,
        size_t (Length),
        int (Option_Set (Protection).Option),
        int (Option_Set (Mapping).Option or Option_Set (Location).Option
           or MAP_FILE),
        int (File),
        off_t (Offset));
      if Result = Failure then
         Raise_POSIX_Error;
      end if;
      return Result;
   end Map_Memory;

   ------------------
   --  Map_Memory  --
   ------------------

   function Map_Memory
     (Length     : System.Storage_Elements.Storage_Offset;
      Protection : Protection_Options;
      Mapping    : Mapping_Options;
      File       : POSIX.IO.File_Descriptor;
      Offset     : POSIX.IO.IO_Offset)
      return System.Address is
      Result : System.Address;
   begin
      Result := mmap
        (Zero_Address,
         size_t (Length),
         int (Option_Set (Protection).Option),
         int (Option_Set (Mapping).Option or MAP_FILE),
         int (File),
         off_t (Offset));
      if Result = Failure then
         Raise_POSIX_Error;
      end if;
      return Result;
   end Map_Memory;

   --------------------
   --  Unmap_Memory  --
   --------------------

   procedure Unmap_Memory
     (First  : System.Address;
      Length : System.Storage_Elements.Storage_Offset) is
      function munmap
        (addr : System.Address;
         len : size_t) return int;
      pragma Import (C, munmap, munmap_LINKNAME);
   begin
      Check (munmap (First, size_t (Length)));
   end Unmap_Memory;

   -------------------------
   --  Change_Protection  --
   -------------------------

   procedure Change_Protection
     (First      : System.Address;
      Length     : System.Storage_Elements.Storage_Offset;
      Protection : Protection_Options) is
      function mprotect
        (addr : System.Address;
         len : size_t;
         prot : int) return int;
      pragma Import (C, mprotect, mprotect_LINKNAME);
   begin
      Check (mprotect (First, size_t (Length),
        int (Option_Set (Protection).Option)));
   end Change_Protection;

   --------------------------
   --  Synchronize_Memory  --
   --------------------------

   procedure Synchronize_Memory
     (First   : System.Address;
      Length  : System.Storage_Elements.Storage_Offset;
      Options : Synchronize_Memory_Options := Empty_Set) is
      function msync
        (address : System.Address;
         len : size_t;
         flags : int) return int;
      pragma Import (C, msync, msync_LINKNAME);
   begin
      Check (msync
        (First, size_t (Length), int (Option_Set (Options).Option)));
   end Synchronize_Memory;

end POSIX.Memory_Mapping;