annotate gcc/ada/libgnat/s-mmap.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S Y S T E M . M M A P --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2007-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- This library is free software; you can redistribute it and/or modify it --
kono
parents:
diff changeset
12 -- under terms of the GNU General Public License as published by the Free --
kono
parents:
diff changeset
13 -- Software Foundation; either version 3, or (at your option) any later --
kono
parents:
diff changeset
14 -- version. This library is distributed in the hope that it will be useful, --
kono
parents:
diff changeset
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
kono
parents:
diff changeset
16 -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package provides memory mapping of files. Depending on your operating
kono
parents:
diff changeset
33 -- system, this might provide a more efficient method for accessing the
kono
parents:
diff changeset
34 -- contents of files.
kono
parents:
diff changeset
35 -- A description of memory-mapping is available on the sqlite page, at:
kono
parents:
diff changeset
36 -- http://www.sqlite.org/mmap.html
kono
parents:
diff changeset
37 --
kono
parents:
diff changeset
38 -- The traditional method for reading a file is to allocate a buffer in the
kono
parents:
diff changeset
39 -- application address space, then open the file and copy its contents. When
kono
parents:
diff changeset
40 -- memory mapping is available though, the application asks the operating
kono
parents:
diff changeset
41 -- system to return a pointer to the requested page, if possible. If the
kono
parents:
diff changeset
42 -- requested page has been or can be mapped into the application address
kono
parents:
diff changeset
43 -- space, the system returns a pointer to that page for the application to
kono
parents:
diff changeset
44 -- use without having to copy anything. Skipping the copy step is what makes
kono
parents:
diff changeset
45 -- memory mapped I/O faster.
kono
parents:
diff changeset
46 --
kono
parents:
diff changeset
47 -- When memory mapping is not available, this package automatically falls
kono
parents:
diff changeset
48 -- back to the traditional copy method.
kono
parents:
diff changeset
49 --
kono
parents:
diff changeset
50 -- Example of use for this package, when reading a file that can be fully
kono
parents:
diff changeset
51 -- mapped
kono
parents:
diff changeset
52 --
kono
parents:
diff changeset
53 -- declare
kono
parents:
diff changeset
54 -- File : Mapped_File;
kono
parents:
diff changeset
55 -- Str : Str_Access;
kono
parents:
diff changeset
56 -- begin
kono
parents:
diff changeset
57 -- File := Open_Read ("/tmp/file_on_disk");
kono
parents:
diff changeset
58 -- Read (File); -- read the whole file
kono
parents:
diff changeset
59 -- Str := Data (File);
kono
parents:
diff changeset
60 -- for S in 1 .. Last (File) loop
kono
parents:
diff changeset
61 -- Put (Str (S));
kono
parents:
diff changeset
62 -- end loop;
kono
parents:
diff changeset
63 -- Close (File);
kono
parents:
diff changeset
64 -- end;
kono
parents:
diff changeset
65 --
kono
parents:
diff changeset
66 -- When the file is big, or you only want to access part of it at a given
kono
parents:
diff changeset
67 -- time, you can use the following type of code.
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 -- declare
kono
parents:
diff changeset
70 -- File : Mapped_File;
kono
parents:
diff changeset
71 -- Str : Str_Access;
kono
parents:
diff changeset
72 -- Offs : File_Size := 0;
kono
parents:
diff changeset
73 -- Page : constant Integer := Get_Page_Size;
kono
parents:
diff changeset
74 -- begin
kono
parents:
diff changeset
75 -- File := Open_Read ("/tmp/file_on_disk");
kono
parents:
diff changeset
76 -- while Offs < Length (File) loop
kono
parents:
diff changeset
77 -- Read (File, Offs, Length => Long_Integer (Page) * 4);
kono
parents:
diff changeset
78 -- Str := Data (File);
kono
parents:
diff changeset
79 --
kono
parents:
diff changeset
80 -- -- Print characters for this chunk:
kono
parents:
diff changeset
81 -- for S in Integer (Offs - Offset (File)) + 1 .. Last (File) loop
kono
parents:
diff changeset
82 -- Put (Str (S));
kono
parents:
diff changeset
83 -- end loop;
kono
parents:
diff changeset
84 --
kono
parents:
diff changeset
85 -- -- Since we are reading multiples of Get_Page_Size, we can simplify
kono
parents:
diff changeset
86 -- -- with
kono
parents:
diff changeset
87 -- -- for S in 1 .. Last (File) loop ...
kono
parents:
diff changeset
88 --
kono
parents:
diff changeset
89 -- Offs := Offs + Long_Integer (Last (File));
kono
parents:
diff changeset
90 -- end loop;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 with Interfaces.C;
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 with System.Strings;
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 package System.Mmap is
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 type Mapped_File is private;
kono
parents:
diff changeset
99 -- File to be mapped in memory.
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 -- This package will use the fastest possible algorithm to load the
kono
parents:
diff changeset
102 -- file in memory. On systems that support it, the file is not really
kono
parents:
diff changeset
103 -- loaded in memory. Instead, a call to the mmap() system call (or
kono
parents:
diff changeset
104 -- CreateFileMapping()) will keep the file on disk, but make it
kono
parents:
diff changeset
105 -- accessible as if it was in memory.
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 -- When the system does not support it, the file is actually loaded in
kono
parents:
diff changeset
108 -- memory through calls to read(), and written back with write() when you
kono
parents:
diff changeset
109 -- close it. This is of course much slower.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 -- Legacy: each mapped file has a "default" mapped region in it.
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 type Mapped_Region is private;
kono
parents:
diff changeset
114 -- A representation of part of a file in memory. Actual reading/writing
kono
parents:
diff changeset
115 -- is done through a mapped region. After being returned by Read, a mapped
kono
parents:
diff changeset
116 -- region must be free'd when done. If the original Mapped_File was open
kono
parents:
diff changeset
117 -- for reading, it can be closed before the mapped region is free'd.
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 Invalid_Mapped_File : constant Mapped_File;
kono
parents:
diff changeset
120 Invalid_Mapped_Region : constant Mapped_Region;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 type Unconstrained_String is new String (Positive);
kono
parents:
diff changeset
123 type Str_Access is access all Unconstrained_String;
kono
parents:
diff changeset
124 pragma No_Strict_Aliasing (Str_Access);
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 type File_Size is new Interfaces.C.size_t;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 function To_Str_Access
kono
parents:
diff changeset
129 (Str : System.Strings.String_Access) return Str_Access;
kono
parents:
diff changeset
130 -- Convert Str. The returned value points to the same memory block, but no
kono
parents:
diff changeset
131 -- longer includes the bounds, which you need to manage yourself
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 function Open_Read
kono
parents:
diff changeset
134 (Filename : String;
kono
parents:
diff changeset
135 Use_Mmap_If_Available : Boolean := True) return Mapped_File;
kono
parents:
diff changeset
136 -- Open a file for reading. The same file can be shared by multiple
kono
parents:
diff changeset
137 -- processes, that will see each others's changes as they occur.
kono
parents:
diff changeset
138 -- Any attempt to write the data might result in a segmentation fault,
kono
parents:
diff changeset
139 -- depending on how the file is open.
kono
parents:
diff changeset
140 -- Name_Error is raised if the file does not exist.
kono
parents:
diff changeset
141 -- Filename should be compatible with the filesystem.
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 function Open_Read_No_Exception
kono
parents:
diff changeset
144 (Filename : String;
kono
parents:
diff changeset
145 Use_Mmap_If_Available : Boolean := True) return Mapped_File;
kono
parents:
diff changeset
146 -- Like Open_Read but return Invalid_Mapped_File in case of error
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 function Open_Write
kono
parents:
diff changeset
149 (Filename : String;
kono
parents:
diff changeset
150 Use_Mmap_If_Available : Boolean := True) return Mapped_File;
kono
parents:
diff changeset
151 -- Open a file for writing.
kono
parents:
diff changeset
152 -- You cannot change the length of the file.
kono
parents:
diff changeset
153 -- Name_Error is raised if the file does not exist
kono
parents:
diff changeset
154 -- Filename should be compatible with the filesystem.
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 procedure Close (File : in out Mapped_File);
kono
parents:
diff changeset
157 -- Close the file, and unmap the memory that is used for the region
kono
parents:
diff changeset
158 -- contained in File. If the system does not support the unmmap() system
kono
parents:
diff changeset
159 -- call or equivalent, or these were not available for the file itself,
kono
parents:
diff changeset
160 -- then the file is written back to the disk if it was opened for writing.
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 procedure Free (Region : in out Mapped_Region);
kono
parents:
diff changeset
163 -- Unmap the memory that is used for this region and deallocate the region
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 procedure Read
kono
parents:
diff changeset
166 (File : Mapped_File;
kono
parents:
diff changeset
167 Region : in out Mapped_Region;
kono
parents:
diff changeset
168 Offset : File_Size := 0;
kono
parents:
diff changeset
169 Length : File_Size := 0;
kono
parents:
diff changeset
170 Mutable : Boolean := False);
kono
parents:
diff changeset
171 -- Read a specific part of File and set Region to the corresponding mapped
kono
parents:
diff changeset
172 -- region, or re-use it if possible.
kono
parents:
diff changeset
173 -- Offset is the number of bytes since the beginning of the file at which
kono
parents:
diff changeset
174 -- we should start reading. Length is the number of bytes that should be
kono
parents:
diff changeset
175 -- read. If set to 0, as much of the file as possible is read (presumably
kono
parents:
diff changeset
176 -- the whole file unless you are reading a _huge_ file).
kono
parents:
diff changeset
177 -- Note that no (un)mapping is is done if that part of the file is already
kono
parents:
diff changeset
178 -- available through Region.
kono
parents:
diff changeset
179 -- If the file was opened for writing, any modification you do to the
kono
parents:
diff changeset
180 -- data stored in File will be stored on disk (either immediately when the
kono
parents:
diff changeset
181 -- file is opened through a mmap() system call, or when the file is closed
kono
parents:
diff changeset
182 -- otherwise).
kono
parents:
diff changeset
183 -- Mutable is processed only for reading files. If set to True, the
kono
parents:
diff changeset
184 -- data can be modified, even through it will not be carried through the
kono
parents:
diff changeset
185 -- underlying file, nor it is guaranteed to be carried through remapping.
kono
parents:
diff changeset
186 -- This function takes care of page size alignment issues. The accessors
kono
parents:
diff changeset
187 -- below only expose the region that has been requested by this call, even
kono
parents:
diff changeset
188 -- if more bytes were actually mapped by this function.
kono
parents:
diff changeset
189 -- TODO??? Enable to have a private copy for readable files
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 function Read
kono
parents:
diff changeset
192 (File : Mapped_File;
kono
parents:
diff changeset
193 Offset : File_Size := 0;
kono
parents:
diff changeset
194 Length : File_Size := 0;
kono
parents:
diff changeset
195 Mutable : Boolean := False) return Mapped_Region;
kono
parents:
diff changeset
196 -- Likewise, return a new mapped region
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 procedure Read
kono
parents:
diff changeset
199 (File : Mapped_File;
kono
parents:
diff changeset
200 Offset : File_Size := 0;
kono
parents:
diff changeset
201 Length : File_Size := 0;
kono
parents:
diff changeset
202 Mutable : Boolean := False);
kono
parents:
diff changeset
203 -- Likewise, use the legacy "default" region in File
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 function Length (File : Mapped_File) return File_Size;
kono
parents:
diff changeset
206 -- Size of the file on the disk
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 function Offset (Region : Mapped_Region) return File_Size;
kono
parents:
diff changeset
209 -- Return the offset, in the physical file on disk, corresponding to the
kono
parents:
diff changeset
210 -- requested mapped region. The first byte in the file has offest 0.
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 function Offset (File : Mapped_File) return File_Size;
kono
parents:
diff changeset
213 -- Likewise for the region contained in File
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 function Last (Region : Mapped_Region) return Integer;
kono
parents:
diff changeset
216 -- Return the number of requested bytes mapped in this region. It is
kono
parents:
diff changeset
217 -- erroneous to access Data for indices outside 1 .. Last (Region).
kono
parents:
diff changeset
218 -- Such accesses may cause Storage_Error to be raised.
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 function Last (File : Mapped_File) return Integer;
kono
parents:
diff changeset
221 -- Return the number of requested bytes mapped in the region contained in
kono
parents:
diff changeset
222 -- File. It is erroneous to access Data for indices outside of 1 .. Last
kono
parents:
diff changeset
223 -- (File); such accesses may cause Storage_Error to be raised.
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 function Data (Region : Mapped_Region) return Str_Access;
kono
parents:
diff changeset
226 pragma Inline (Data);
kono
parents:
diff changeset
227 -- The data mapped in Region as requested. The result is an unconstrained
kono
parents:
diff changeset
228 -- string, so you cannot use the usual 'First and 'Last attributes.
kono
parents:
diff changeset
229 -- Instead, these are respectively 1 and Size.
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 function Data (File : Mapped_File) return Str_Access;
kono
parents:
diff changeset
232 pragma Inline (Data);
kono
parents:
diff changeset
233 -- Likewise for the region contained in File
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 function Is_Mutable (Region : Mapped_Region) return Boolean;
kono
parents:
diff changeset
236 -- Return whether it is safe to change bytes in Data (Region). This is true
kono
parents:
diff changeset
237 -- for regions from writeable files, for regions mapped with the "Mutable"
kono
parents:
diff changeset
238 -- flag set, and for regions that are copied in a buffer. Note that it is
kono
parents:
diff changeset
239 -- not specified whether empty regions are mutable or not, since there is
kono
parents:
diff changeset
240 -- no byte no modify.
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 function Is_Mmapped (File : Mapped_File) return Boolean;
kono
parents:
diff changeset
243 -- Whether regions for this file are opened through an mmap() system call
kono
parents:
diff changeset
244 -- or equivalent. This is in general irrelevant to your application, unless
kono
parents:
diff changeset
245 -- the file can be accessed by multiple concurrent processes or tasks. In
kono
parents:
diff changeset
246 -- such a case, and if the file is indeed mmap-ed, then the various parts
kono
parents:
diff changeset
247 -- of the file can be written simulatenously, and thus you cannot ensure
kono
parents:
diff changeset
248 -- the integrity of the file. If the file is not mmapped, the latest
kono
parents:
diff changeset
249 -- process to Close it overwrite what other processes have done.
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 function Get_Page_Size return Integer;
kono
parents:
diff changeset
252 -- Returns the number of bytes in a page. Once a file is mapped from the
kono
parents:
diff changeset
253 -- disk, its offset and Length should be multiples of this page size (which
kono
parents:
diff changeset
254 -- is ensured by this package in any case). Knowing this page size allows
kono
parents:
diff changeset
255 -- you to map as much memory as possible at once, thus potentially reducing
kono
parents:
diff changeset
256 -- the number of system calls to read the file by chunks.
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 function Read_Whole_File
kono
parents:
diff changeset
259 (Filename : String;
kono
parents:
diff changeset
260 Empty_If_Not_Found : Boolean := False)
kono
parents:
diff changeset
261 return System.Strings.String_Access;
kono
parents:
diff changeset
262 -- Returns the whole contents of the file.
kono
parents:
diff changeset
263 -- The returned string must be freed by the user.
kono
parents:
diff changeset
264 -- This is a convenience function, which is of course slower than the ones
kono
parents:
diff changeset
265 -- above since we also need to allocate some memory, actually read the file
kono
parents:
diff changeset
266 -- and copy the bytes.
kono
parents:
diff changeset
267 -- If the file does not exist, null is returned. However, if
kono
parents:
diff changeset
268 -- Empty_If_Not_Found is True, then the empty string is returned instead.
kono
parents:
diff changeset
269 -- Filename should be compatible with the filesystem.
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 private
kono
parents:
diff changeset
272 pragma Inline (Data, Length, Last, Offset, Is_Mmapped, To_Str_Access);
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 type Mapped_File_Record;
kono
parents:
diff changeset
275 type Mapped_File is access Mapped_File_Record;
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 type Mapped_Region_Record;
kono
parents:
diff changeset
278 type Mapped_Region is access Mapped_Region_Record;
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 Invalid_Mapped_File : constant Mapped_File := null;
kono
parents:
diff changeset
281 Invalid_Mapped_Region : constant Mapped_Region := null;
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 end System.Mmap;