1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . M M A P . O S _ I N T E R F A C E --
9 -- Copyright (C) 2007-2023, AdaCore --
11 -- This library is free software; you can redistribute it and/or modify it --
12 -- under terms of the GNU General Public License as published by the Free --
13 -- Software Foundation; either version 3, or (at your option) any later --
14 -- version. This library is distributed in the hope that it will be useful, --
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
16 -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- OS pecularities abstraction package for Win32 systems.
34 package System
.Mmap
.OS_Interface
is
36 -- The Win package contains copy of definition found in recent System.Win32
37 -- unit provided with the GNAT compiler. The copy is needed to be able to
38 -- compile this unit with older compilers. Note that this internal Win
39 -- package can be removed when GNAT 6.1.0 is not supported anymore.
43 subtype PVOID
is Standard
.System
.Address
;
45 type HANDLE
is new Interfaces
.C
.ptrdiff_t
;
47 type WORD
is new Interfaces
.C
.unsigned_short
;
48 type DWORD
is new Interfaces
.C
.unsigned_long
;
49 type LONG
is new Interfaces
.C
.long
;
50 type SIZE_T
is new Interfaces
.C
.size_t
;
52 type BOOL
is new Interfaces
.C
.int
;
53 for BOOL
'Size use Interfaces
.C
.int
'Size;
55 FALSE : constant := 0;
57 GENERIC_READ
: constant := 16#
80000000#
;
58 GENERIC_WRITE
: constant := 16#
40000000#
;
59 OPEN_EXISTING
: constant := 3;
61 type OVERLAPPED
is record
69 type SECURITY_ATTRIBUTES
is record
71 pSecurityDescriptor
: PVOID
;
72 bInheritHandle
: BOOL
;
75 type SYSTEM_INFO
is record
78 lpMinimumApplicationAddress
: PVOID
;
79 lpMaximumApplicationAddress
: PVOID
;
80 dwActiveProcessorMask
: PVOID
;
81 dwNumberOfProcessors
: DWORD
;
82 dwProcessorType
: DWORD
;
83 dwAllocationGranularity
: DWORD
;
84 wProcessorLevel
: WORD
;
85 wProcessorRevision
: WORD
;
87 type LP_SYSTEM_INFO
is access all SYSTEM_INFO
;
89 INVALID_HANDLE_VALUE
: constant HANDLE
:= -1;
90 FILE_BEGIN
: constant := 0;
91 FILE_SHARE_READ
: constant := 16#
00000001#
;
92 FILE_ATTRIBUTE_NORMAL
: constant := 16#
00000080#
;
93 FILE_MAP_COPY
: constant := 1;
94 FILE_MAP_READ
: constant := 4;
95 FILE_MAP_WRITE
: constant := 2;
96 PAGE_READONLY
: constant := 16#
0002#
;
97 PAGE_READWRITE
: constant := 16#
0004#
;
98 INVALID_FILE_SIZE
: constant := 16#FFFFFFFF#
;
101 (lpFileName
: Standard
.System
.Address
;
102 dwDesiredAccess
: DWORD
;
104 lpSecurityAttributes
: access SECURITY_ATTRIBUTES
;
105 dwCreationDisposition
: DWORD
;
106 dwFlagsAndAttributes
: DWORD
;
107 hTemplateFile
: HANDLE
) return HANDLE
;
108 pragma Import
(Stdcall
, CreateFile
, "CreateFileW");
112 lpBuffer
: Standard
.System
.Address
;
113 nNumberOfBytesToWrite
: DWORD
;
114 lpNumberOfBytesWritten
: access DWORD
;
115 lpOverlapped
: access OVERLAPPED
) return BOOL
;
116 pragma Import
(Stdcall
, WriteFile
, "WriteFile");
120 lpBuffer
: Standard
.System
.Address
;
121 nNumberOfBytesToRead
: DWORD
;
122 lpNumberOfBytesRead
: access DWORD
;
123 lpOverlapped
: access OVERLAPPED
) return BOOL
;
124 pragma Import
(Stdcall
, ReadFile
, "ReadFile");
126 function CloseHandle
(hObject
: HANDLE
) return BOOL
;
127 pragma Import
(Stdcall
, CloseHandle
, "CloseHandle");
130 (hFile
: HANDLE
; lpFileSizeHigh
: access DWORD
) return DWORD
;
131 pragma Import
(Stdcall
, GetFileSize
, "GetFileSize");
133 function SetFilePointer
135 lDistanceToMove
: LONG
;
136 lpDistanceToMoveHigh
: access LONG
;
137 dwMoveMethod
: DWORD
) return DWORD
;
138 pragma Import
(Stdcall
, SetFilePointer
, "SetFilePointer");
140 function CreateFileMapping
142 lpSecurityAttributes
: access SECURITY_ATTRIBUTES
;
144 dwMaximumSizeHigh
: DWORD
;
145 dwMaximumSizeLow
: DWORD
;
146 lpName
: Standard
.System
.Address
) return HANDLE
;
147 pragma Import
(Stdcall
, CreateFileMapping
, "CreateFileMappingW");
149 function MapViewOfFile
150 (hFileMappingObject
: HANDLE
;
151 dwDesiredAccess
: DWORD
;
152 dwFileOffsetHigh
: DWORD
;
153 dwFileOffsetLow
: DWORD
;
154 dwNumberOfBytesToMap
: SIZE_T
) return Standard
.System
.Address
;
155 pragma Import
(Stdcall
, MapViewOfFile
, "MapViewOfFile");
157 function UnmapViewOfFile
158 (lpBaseAddress
: Standard
.System
.Address
) return BOOL
;
159 pragma Import
(Stdcall
, UnmapViewOfFile
, "UnmapViewOfFile");
161 procedure GetSystemInfo
(lpSystemInfo
: LP_SYSTEM_INFO
);
162 pragma Import
(Stdcall
, GetSystemInfo
, "GetSystemInfo");
166 type System_File
is record
170 -- Whether mapping is requested by the user and available on the system
172 Mapping_Handle
: Win
.HANDLE
;
175 -- Whether this file can be written to
178 -- Length of the file. Used to know what can be mapped in the file
181 type System_Mapping
is record
182 Address
: Standard
.System
.Address
;
186 Invalid_System_File
: constant System_File
:=
187 (Win
.INVALID_HANDLE_VALUE
, False, Win
.INVALID_HANDLE_VALUE
, False, 0);
188 Invalid_System_Mapping
: constant System_Mapping
:=
189 (Standard
.System
.Null_Address
, 0);
193 Use_Mmap_If_Available
: Boolean := True) return System_File
;
194 -- Open a file for reading and return the corresponding System_File. Return
195 -- Invalid_System_File if unsuccessful.
199 Use_Mmap_If_Available
: Boolean := True) return System_File
;
200 -- Likewise for writing to a file
202 procedure Close
(File
: in out System_File
);
203 -- Close a system file
205 function Read_From_Disk
207 Offset
, Length
: File_Size
) return System
.Strings
.String_Access
;
208 -- Read a fragment of a file. It is up to the caller to free the result
209 -- when done with it.
211 procedure Write_To_Disk
213 Offset
, Length
: File_Size
;
214 Buffer
: System
.Strings
.String_Access
);
215 -- Write some content to a fragment of a file
217 procedure Create_Mapping
219 Offset
, Length
: in out File_Size
;
221 Mapping
: out System_Mapping
);
222 -- Create a memory mapping for the given File, for the area starting at
223 -- Offset and containing Length bytes. Store it to Mapping.
224 -- Note that Offset and Length may be modified according to the system
225 -- needs (for boudaries, for instance). The caller must cope with actually
226 -- wider mapped areas.
228 procedure Dispose_Mapping
229 (Mapping
: in out System_Mapping
);
230 -- Unmap a previously-created mapping
232 function Get_Page_Size
return File_Size
;
233 -- Return the number of bytes in a system page.
235 end System
.Mmap
.OS_Interface
;