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