]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
0db4a45b | 3 | // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | MMap Class - Provides 'real' mmap or a faked mmap using read(). | |
7 | ||
8 | MMap cover class. | |
9 | ||
10 | Some broken versions of glibc2 (libc6) have a broken definition | |
11 | of mmap that accepts a char * -- all other systems (and libc5) use | |
12 | void *. We can't safely do anything here that would be portable, so | |
13 | libc6 generates warnings -- which should be errors, g++ isn't properly | |
14 | strict. | |
15 | ||
578bfd0a AL |
16 | ##################################################################### */ |
17 | /*}}}*/ | |
18 | // Include Files /*{{{*/ | |
19 | #define _BSD_SOURCE | |
094a497d AL |
20 | #include <apt-pkg/mmap.h> |
21 | #include <apt-pkg/error.h> | |
578bfd0a | 22 | |
b2e465d6 AL |
23 | #include <apti18n.h> |
24 | ||
578bfd0a AL |
25 | #include <sys/mman.h> |
26 | #include <sys/stat.h> | |
578bfd0a AL |
27 | #include <unistd.h> |
28 | #include <fcntl.h> | |
13eb93fc | 29 | #include <stdlib.h> |
06afffcc | 30 | #include <errno.h> |
4f333a8b MV |
31 | |
32 | #include <cstring> | |
578bfd0a AL |
33 | /*}}}*/ |
34 | ||
35 | // MMap::MMap - Constructor /*{{{*/ | |
36 | // --------------------------------------------------------------------- | |
37 | /* */ | |
2d11135a | 38 | MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0), |
06afffcc | 39 | Base(0), SyncToFd(NULL) |
578bfd0a AL |
40 | { |
41 | if ((Flags & NoImmMap) != NoImmMap) | |
2d11135a AL |
42 | Map(F); |
43 | } | |
44 | /*}}}*/ | |
45 | // MMap::MMap - Constructor /*{{{*/ | |
46 | // --------------------------------------------------------------------- | |
47 | /* */ | |
48 | MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0), | |
06afffcc | 49 | Base(0), SyncToFd(NULL) |
2d11135a | 50 | { |
578bfd0a AL |
51 | } |
52 | /*}}}*/ | |
53 | // MMap::~MMap - Destructor /*{{{*/ | |
54 | // --------------------------------------------------------------------- | |
55 | /* */ | |
56 | MMap::~MMap() | |
57 | { | |
2d11135a | 58 | Close(); |
578bfd0a AL |
59 | } |
60 | /*}}}*/ | |
61 | // MMap::Map - Perform the mapping /*{{{*/ | |
62 | // --------------------------------------------------------------------- | |
63 | /* */ | |
2d11135a | 64 | bool MMap::Map(FileFd &Fd) |
578bfd0a AL |
65 | { |
66 | iSize = Fd.Size(); | |
67 | ||
68 | // Set the permissions. | |
69 | int Prot = PROT_READ; | |
70 | int Map = MAP_SHARED; | |
71 | if ((Flags & ReadOnly) != ReadOnly) | |
72 | Prot |= PROT_WRITE; | |
73 | if ((Flags & Public) != Public) | |
74 | Map = MAP_PRIVATE; | |
75 | ||
b35d2f5f | 76 | if (iSize == 0) |
b2e465d6 | 77 | return _error->Error(_("Can't mmap an empty file")); |
b35d2f5f | 78 | |
578bfd0a AL |
79 | // Map it. |
80 | Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0); | |
81 | if (Base == (void *)-1) | |
06afffcc DK |
82 | { |
83 | if (errno == ENODEV || errno == EINVAL) | |
84 | { | |
85 | // The filesystem doesn't support this particular kind of mmap. | |
86 | // So we allocate a buffer and read the whole file into it. | |
87 | int const dupped_fd = dup(Fd.Fd()); | |
88 | if (dupped_fd == -1) | |
89 | return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd()); | |
90 | ||
91 | Base = new unsigned char[iSize]; | |
92 | SyncToFd = new FileFd (dupped_fd); | |
93 | if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize)) | |
94 | return false; | |
95 | } | |
96 | else | |
97 | return _error->Errno("mmap",_("Couldn't make mmap of %lu bytes"), | |
98 | iSize); | |
99 | } | |
578bfd0a AL |
100 | |
101 | return true; | |
102 | } | |
103 | /*}}}*/ | |
104 | // MMap::Close - Close the map /*{{{*/ | |
105 | // --------------------------------------------------------------------- | |
106 | /* */ | |
2d11135a | 107 | bool MMap::Close(bool DoSync) |
578bfd0a | 108 | { |
2d11135a | 109 | if ((Flags & UnMapped) == UnMapped || Base == 0 || iSize == 0) |
578bfd0a | 110 | return true; |
2d11135a | 111 | |
1164783d AL |
112 | if (DoSync == true) |
113 | Sync(); | |
06afffcc DK |
114 | |
115 | if (SyncToFd != NULL) | |
116 | { | |
117 | delete[] (char *)Base; | |
118 | delete SyncToFd; | |
119 | SyncToFd = NULL; | |
120 | } | |
121 | else | |
122 | { | |
123 | if (munmap((char *)Base, iSize) != 0) | |
124 | _error->WarningE("mmap", _("Unable to close mmap")); | |
125 | } | |
126 | ||
578bfd0a | 127 | iSize = 0; |
b2e465d6 | 128 | Base = 0; |
578bfd0a AL |
129 | return true; |
130 | } | |
131 | /*}}}*/ | |
132 | // MMap::Sync - Syncronize the map with the disk /*{{{*/ | |
133 | // --------------------------------------------------------------------- | |
0149949b AL |
134 | /* This is done in syncronous mode - the docs indicate that this will |
135 | not return till all IO is complete */ | |
578bfd0a AL |
136 | bool MMap::Sync() |
137 | { | |
2d11135a AL |
138 | if ((Flags & UnMapped) == UnMapped) |
139 | return true; | |
140 | ||
de3c15ea | 141 | #ifdef _POSIX_SYNCHRONIZED_IO |
1164783d | 142 | if ((Flags & ReadOnly) != ReadOnly) |
06afffcc DK |
143 | { |
144 | if (SyncToFd != NULL) | |
145 | { | |
146 | if (!SyncToFd->Seek(0) || !SyncToFd->Write(Base, iSize)) | |
147 | return false; | |
148 | } | |
149 | else | |
150 | { | |
151 | if (msync((char *)Base, iSize, MS_SYNC) < 0) | |
152 | return _error->Errno("msync", _("Unable to synchronize mmap")); | |
153 | } | |
154 | } | |
de3c15ea | 155 | #endif |
578bfd0a AL |
156 | return true; |
157 | } | |
158 | /*}}}*/ | |
159 | // MMap::Sync - Syncronize a section of the file to disk /*{{{*/ | |
160 | // --------------------------------------------------------------------- | |
161 | /* */ | |
162 | bool MMap::Sync(unsigned long Start,unsigned long Stop) | |
163 | { | |
2d11135a AL |
164 | if ((Flags & UnMapped) == UnMapped) |
165 | return true; | |
166 | ||
35c22def AL |
167 | #ifdef _POSIX_SYNCHRONIZED_IO |
168 | unsigned long PSize = sysconf(_SC_PAGESIZE); | |
1164783d | 169 | if ((Flags & ReadOnly) != ReadOnly) |
06afffcc DK |
170 | { |
171 | if (SyncToFd != 0) | |
172 | { | |
173 | if (!SyncToFd->Seek(0) || | |
174 | !SyncToFd->Write (((char *)Base)+Start, Stop-Start)) | |
175 | return false; | |
176 | } | |
177 | else | |
178 | { | |
179 | if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0) | |
180 | return _error->Errno("msync", _("Unable to synchronize mmap")); | |
181 | } | |
182 | } | |
de3c15ea | 183 | #endif |
578bfd0a AL |
184 | return true; |
185 | } | |
186 | /*}}}*/ | |
187 | ||
188 | // DynamicMMap::DynamicMMap - Constructor /*{{{*/ | |
189 | // --------------------------------------------------------------------- | |
190 | /* */ | |
d6c4a976 DK |
191 | DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Workspace, |
192 | unsigned long const &Grow, unsigned long const &Limit) : | |
193 | MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(Workspace), | |
194 | GrowFactor(Grow), Limit(Limit) | |
578bfd0a | 195 | { |
d38b7b3d AL |
196 | if (_error->PendingError() == true) |
197 | return; | |
198 | ||
2d11135a | 199 | unsigned long EndOfFile = Fd->Size(); |
b2e465d6 AL |
200 | if (EndOfFile > WorkSpace) |
201 | WorkSpace = EndOfFile; | |
ea92d036 | 202 | else if(WorkSpace > 0) |
b2e465d6 | 203 | { |
ea92d036 | 204 | Fd->Seek(WorkSpace - 1); |
b2e465d6 AL |
205 | char C = 0; |
206 | Fd->Write(&C,sizeof(C)); | |
207 | } | |
208 | ||
2d11135a | 209 | Map(F); |
578bfd0a AL |
210 | iSize = EndOfFile; |
211 | } | |
212 | /*}}}*/ | |
2d11135a AL |
213 | // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/ |
214 | // --------------------------------------------------------------------- | |
f1c6a8ca DK |
215 | /* We try here to use mmap to reserve some space - this is much more |
216 | cooler than the fallback solution to simply allocate a char array | |
217 | and could come in handy later than we are able to grow such an mmap */ | |
d6c4a976 DK |
218 | DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace, |
219 | unsigned long const &Grow, unsigned long const &Limit) : | |
220 | MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace), | |
221 | GrowFactor(Grow), Limit(Limit) | |
2d11135a | 222 | { |
d6c4a976 DK |
223 | if (_error->PendingError() == true) |
224 | return; | |
225 | ||
226 | // disable Moveable if we don't grow | |
227 | if (Grow == 0) | |
a9fe5928 | 228 | this->Flags &= ~Moveable; |
d6c4a976 DK |
229 | |
230 | #ifndef __linux__ | |
231 | // kfreebsd doesn't have mremap, so we use the fallback | |
a9fe5928 DK |
232 | if ((this->Flags & Moveable) == Moveable) |
233 | this->Flags |= Fallback; | |
d6c4a976 | 234 | #endif |
f1c6a8ca DK |
235 | |
236 | #ifdef _POSIX_MAPPED_FILES | |
a9fe5928 | 237 | if ((this->Flags & Fallback) != Fallback) { |
d6c4a976 DK |
238 | // Set the permissions. |
239 | int Prot = PROT_READ; | |
240 | int Map = MAP_PRIVATE | MAP_ANONYMOUS; | |
a9fe5928 | 241 | if ((this->Flags & ReadOnly) != ReadOnly) |
d6c4a976 | 242 | Prot |= PROT_WRITE; |
a9fe5928 | 243 | if ((this->Flags & Public) == Public) |
d6c4a976 | 244 | Map = MAP_SHARED | MAP_ANONYMOUS; |
eb162ff7 | 245 | |
d6c4a976 DK |
246 | // use anonymous mmap() to get the memory |
247 | Base = (unsigned char*) mmap(0, WorkSpace, Prot, Map, -1, 0); | |
eb162ff7 | 248 | |
d6c4a976 DK |
249 | if(Base == MAP_FAILED) |
250 | _error->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace); | |
251 | ||
252 | iSize = 0; | |
253 | return; | |
254 | } | |
d5972534 | 255 | #endif |
d6c4a976 DK |
256 | // fallback to a static allocated space |
257 | Base = new unsigned char[WorkSpace]; | |
258 | memset(Base,0,WorkSpace); | |
259 | iSize = 0; | |
2d11135a AL |
260 | } |
261 | /*}}}*/ | |
578bfd0a AL |
262 | // DynamicMMap::~DynamicMMap - Destructor /*{{{*/ |
263 | // --------------------------------------------------------------------- | |
264 | /* We truncate the file to the size of the memory data set */ | |
265 | DynamicMMap::~DynamicMMap() | |
266 | { | |
2d11135a AL |
267 | if (Fd == 0) |
268 | { | |
26b37f95 DK |
269 | if (Base == 0) |
270 | return; | |
f1c6a8ca | 271 | #ifdef _POSIX_MAPPED_FILES |
a67de73e | 272 | munmap(Base, WorkSpace); |
f1c6a8ca | 273 | #else |
2d11135a | 274 | delete [] (unsigned char *)Base; |
f1c6a8ca | 275 | #endif |
2d11135a AL |
276 | return; |
277 | } | |
278 | ||
578bfd0a | 279 | unsigned long EndOfFile = iSize; |
1164783d | 280 | iSize = WorkSpace; |
2d11135a | 281 | Close(false); |
3c8cda8b MV |
282 | if(ftruncate(Fd->Fd(),EndOfFile) < 0) |
283 | _error->Errno("ftruncate", _("Failed to truncate file")); | |
578bfd0a AL |
284 | } |
285 | /*}}}*/ | |
286 | // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/ | |
287 | // --------------------------------------------------------------------- | |
f55a958f AL |
288 | /* This allocates a block of memory aligned to the given size */ |
289 | unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) | |
578bfd0a AL |
290 | { |
291 | unsigned long Result = iSize; | |
f55a958f AL |
292 | if (Aln != 0) |
293 | Result += Aln - (iSize%Aln); | |
c5f44afc | 294 | |
f55a958f | 295 | iSize = Result + Size; |
c5f44afc | 296 | |
f1c6a8ca DK |
297 | // try to grow the buffer |
298 | while(Result + Size > WorkSpace) | |
578bfd0a | 299 | { |
f1c6a8ca DK |
300 | if(!Grow()) |
301 | { | |
5afa55c6 | 302 | _error->Fatal(_("Dynamic MMap ran out of room. Please increase the size " |
f1c6a8ca DK |
303 | "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); |
304 | return 0; | |
305 | } | |
578bfd0a | 306 | } |
578bfd0a AL |
307 | return Result; |
308 | } | |
309 | /*}}}*/ | |
310 | // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/ | |
311 | // --------------------------------------------------------------------- | |
312 | /* This allocates an Item of size ItemSize so that it is aligned to its | |
313 | size in the file. */ | |
314 | unsigned long DynamicMMap::Allocate(unsigned long ItemSize) | |
c5f44afc | 315 | { |
578bfd0a AL |
316 | // Look for a matching pool entry |
317 | Pool *I; | |
318 | Pool *Empty = 0; | |
a9fe5928 | 319 | for (I = Pools; I != Pools + PoolCount; ++I) |
578bfd0a AL |
320 | { |
321 | if (I->ItemSize == 0) | |
322 | Empty = I; | |
323 | if (I->ItemSize == ItemSize) | |
324 | break; | |
325 | } | |
578bfd0a AL |
326 | // No pool is allocated, use an unallocated one |
327 | if (I == Pools + PoolCount) | |
328 | { | |
329 | // Woops, we ran out, the calling code should allocate more. | |
330 | if (Empty == 0) | |
331 | { | |
332 | _error->Error("Ran out of allocation pools"); | |
333 | return 0; | |
334 | } | |
335 | ||
336 | I = Empty; | |
337 | I->ItemSize = ItemSize; | |
338 | I->Count = 0; | |
339 | } | |
c5f44afc DK |
340 | |
341 | unsigned long Result = 0; | |
578bfd0a AL |
342 | // Out of space, allocate some more |
343 | if (I->Count == 0) | |
344 | { | |
c5f44afc DK |
345 | const unsigned long size = 20*1024; |
346 | I->Count = size/ItemSize; | |
a9fe5928 | 347 | Pool* oldPools = Pools; |
c5f44afc | 348 | Result = RawAllocate(size,ItemSize); |
a9fe5928 DK |
349 | if (Pools != oldPools) |
350 | I += Pools - oldPools; | |
351 | ||
c5f44afc DK |
352 | // Does the allocation failed ? |
353 | if (Result == 0 && _error->PendingError()) | |
354 | return 0; | |
355 | I->Start = Result; | |
356 | } | |
357 | else | |
358 | Result = I->Start; | |
f55a958f | 359 | |
578bfd0a | 360 | I->Count--; |
c5f44afc | 361 | I->Start += ItemSize; |
578bfd0a AL |
362 | return Result/ItemSize; |
363 | } | |
364 | /*}}}*/ | |
365 | // DynamicMMap::WriteString - Write a string to the file /*{{{*/ | |
366 | // --------------------------------------------------------------------- | |
367 | /* Strings are not aligned to anything */ | |
368 | unsigned long DynamicMMap::WriteString(const char *String, | |
369 | unsigned long Len) | |
370 | { | |
6e52073f | 371 | if (Len == (unsigned long)-1) |
578bfd0a | 372 | Len = strlen(String); |
c5f44afc | 373 | |
a9fe5928 | 374 | unsigned long const Result = RawAllocate(Len+1,0); |
c5f44afc DK |
375 | |
376 | if (Result == 0 && _error->PendingError()) | |
377 | return 0; | |
378 | ||
578bfd0a AL |
379 | memcpy((char *)Base + Result,String,Len); |
380 | ((char *)Base)[Result + Len] = 0; | |
381 | return Result; | |
382 | } | |
383 | /*}}}*/ | |
f1c6a8ca DK |
384 | // DynamicMMap::Grow - Grow the mmap /*{{{*/ |
385 | // --------------------------------------------------------------------- | |
d6c4a976 DK |
386 | /* This method is a wrapper around different methods to (try to) grow |
387 | a mmap (or our char[]-fallback). Encounterable environments: | |
388 | 1. Moveable + !Fallback + linux -> mremap with MREMAP_MAYMOVE | |
389 | 2. Moveable + !Fallback + !linux -> not possible (forbidden by constructor) | |
390 | 3. Moveable + Fallback -> realloc | |
391 | 4. !Moveable + !Fallback + linux -> mremap alone - which will fail in 99,9% | |
392 | 5. !Moveable + !Fallback + !linux -> not possible (forbidden by constructor) | |
393 | 6. !Moveable + Fallback -> not possible | |
394 | [ While Moveable and Fallback stands for the equally named flags and | |
395 | "linux" indicates a linux kernel instead of a freebsd kernel. ] | |
396 | So what you can see here is, that a MMAP which want to be growable need | |
397 | to be moveable to have a real chance but that this method will at least try | |
398 | the nearly impossible 4 to grow it before it finally give up: Never say never. */ | |
399 | bool DynamicMMap::Grow() { | |
400 | if (Limit != 0 && WorkSpace >= Limit) | |
e3ac3b46 DK |
401 | return _error->Error(_("Unable to increase the size of the MMap as the " |
402 | "limit of %lu bytes is already reached."), Limit); | |
dcdf1ef1 DK |
403 | if (GrowFactor <= 0) |
404 | return _error->Error(_("Unable to increase size of the MMap as automatic growing is disabled by user.")); | |
f1c6a8ca | 405 | |
a9fe5928 | 406 | unsigned long const newSize = WorkSpace + GrowFactor; |
f1c6a8ca | 407 | |
d6c4a976 DK |
408 | if(Fd != 0) { |
409 | Fd->Seek(newSize - 1); | |
410 | char C = 0; | |
411 | Fd->Write(&C,sizeof(C)); | |
412 | } | |
a9fe5928 DK |
413 | |
414 | unsigned long const poolOffset = Pools - ((Pool*) Base); | |
415 | ||
d6c4a976 DK |
416 | if ((Flags & Fallback) != Fallback) { |
417 | #if defined(_POSIX_MAPPED_FILES) && defined(__linux__) | |
418 | #ifdef MREMAP_MAYMOVE | |
a9fe5928 | 419 | |
d6c4a976 DK |
420 | if ((Flags & Moveable) == Moveable) |
421 | Base = mremap(Base, WorkSpace, newSize, MREMAP_MAYMOVE); | |
422 | else | |
423 | #endif | |
424 | Base = mremap(Base, WorkSpace, newSize, 0); | |
f1c6a8ca | 425 | |
d6c4a976 DK |
426 | if(Base == MAP_FAILED) |
427 | return false; | |
f1c6a8ca | 428 | #else |
d6c4a976 | 429 | return false; |
f1c6a8ca | 430 | #endif |
d6c4a976 DK |
431 | } else { |
432 | if ((Flags & Moveable) != Moveable) | |
433 | return false; | |
434 | ||
435 | Base = realloc(Base, newSize); | |
436 | if (Base == NULL) | |
437 | return false; | |
438 | } | |
439 | ||
a9fe5928 | 440 | Pools =(Pool*) Base + poolOffset; |
d6c4a976 DK |
441 | WorkSpace = newSize; |
442 | return true; | |
f1c6a8ca DK |
443 | } |
444 | /*}}}*/ |