]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
4c58a096dc389895f3dba6f080d73baef7b61d48
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 jgg Exp $
4 /* ######################################################################
6 MMap Class - Provides 'real' mmap or a faked mmap using read().
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
16 ##################################################################### */
18 // Include Files /*{{{*/
19 #define _DEFAULT_SOURCE
22 #include <apt-pkg/mmap.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/fileutl.h>
25 #include <apt-pkg/macros.h>
37 // MMap::MMap - Constructor /*{{{*/
38 // ---------------------------------------------------------------------
40 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Flags(Flags
), iSize(0),
41 Base(nullptr), SyncToFd(nullptr)
46 // MMap::MMap - Constructor /*{{{*/
47 // ---------------------------------------------------------------------
49 MMap::MMap(unsigned long Flags
) : Flags(Flags
), iSize(0),
50 Base(nullptr), SyncToFd(nullptr)
54 // MMap::~MMap - Destructor /*{{{*/
55 // ---------------------------------------------------------------------
62 // MMap::Map - Perform the mapping /*{{{*/
63 // ---------------------------------------------------------------------
65 bool MMap::Map(FileFd
&Fd
)
69 // Set the permissions.
72 if ((Flags
& ReadOnly
) != ReadOnly
)
74 if ((Flags
& Public
) != Public
)
78 return _error
->Error(_("Can't mmap an empty file"));
80 // We can't mmap compressed fd's directly, so we need to read it completely
81 if (Fd
.IsCompressed() == true)
83 if ((Flags
& ReadOnly
) != ReadOnly
)
84 return _error
->Error("Compressed file %s can only be mapped readonly", Fd
.Name().c_str());
86 if (unlikely(Base
== nullptr))
87 return _error
->Errno("MMap-compressed-malloc", _("Couldn't make mmap of %llu bytes"), iSize
);
88 SyncToFd
= new FileFd();
89 if (Fd
.Seek(0L) == false || Fd
.Read(Base
, iSize
) == false)
90 return _error
->Error("Compressed file %s can't be read into mmap", Fd
.Name().c_str());
95 Base
= (Flags
& Fallback
) ? MAP_FAILED
: mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
96 if (Base
== MAP_FAILED
)
98 if (errno
== ENODEV
|| errno
== EINVAL
|| (Flags
& Fallback
))
100 // The filesystem doesn't support this particular kind of mmap.
101 // So we allocate a buffer and read the whole file into it.
102 if ((Flags
& ReadOnly
) == ReadOnly
)
104 // for readonly, we don't need sync, so make it simple
105 Base
= malloc(iSize
);
106 if (unlikely(Base
== nullptr))
107 return _error
->Errno("MMap-malloc", _("Couldn't make mmap of %llu bytes"), iSize
);
108 SyncToFd
= new FileFd();
109 return Fd
.Read(Base
, iSize
);
111 // FIXME: Writing to compressed fd's ?
112 int const dupped_fd
= dup(Fd
.Fd());
114 return _error
->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd
.Fd());
116 Base
= calloc(iSize
, 1);
117 if (unlikely(Base
== nullptr))
118 return _error
->Errno("MMap-calloc", _("Couldn't make mmap of %llu bytes"), iSize
);
119 SyncToFd
= new FileFd (dupped_fd
);
120 if (!SyncToFd
->Seek(0L) || !SyncToFd
->Read(Base
, iSize
))
124 return _error
->Errno("MMap-mmap", _("Couldn't make mmap of %llu bytes"), iSize
);
130 // MMap::Close - Close the map /*{{{*/
131 // ---------------------------------------------------------------------
133 bool MMap::Close(bool DoSync
)
135 if ((Flags
& UnMapped
) == UnMapped
|| validData() == false || iSize
== 0)
141 if (SyncToFd
!= NULL
)
149 if (munmap((char *)Base
, iSize
) != 0)
150 _error
->WarningE("mmap", _("Unable to close mmap"));
158 // MMap::Sync - Syncronize the map with the disk /*{{{*/
159 // ---------------------------------------------------------------------
160 /* This is done in syncronous mode - the docs indicate that this will
161 not return till all IO is complete */
164 if ((Flags
& UnMapped
) == UnMapped
)
167 if ((Flags
& ReadOnly
) != ReadOnly
)
169 if (SyncToFd
!= NULL
)
171 if (!SyncToFd
->Seek(0) || !SyncToFd
->Write(Base
, iSize
))
176 #ifdef _POSIX_SYNCHRONIZED_IO
177 if (msync((char *)Base
, iSize
, MS_SYNC
) < 0)
178 return _error
->Errno("msync", _("Unable to synchronize mmap"));
185 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
186 // ---------------------------------------------------------------------
188 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
190 if ((Flags
& UnMapped
) == UnMapped
)
193 if ((Flags
& ReadOnly
) != ReadOnly
)
197 if (!SyncToFd
->Seek(0) ||
198 !SyncToFd
->Write (((char *)Base
)+Start
, Stop
-Start
))
203 #ifdef _POSIX_SYNCHRONIZED_IO
204 unsigned long long const PSize
= sysconf(_SC_PAGESIZE
);
205 if (msync((char *)Base
+(Start
/PSize
)*PSize
, Stop
- Start
, MS_SYNC
) < 0)
206 return _error
->Errno("msync", _("Unable to synchronize mmap"));
214 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
215 // ---------------------------------------------------------------------
217 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long const &Workspace
,
218 unsigned long const &Grow
, unsigned long const &Limit
) :
219 MMap(Flags
), Fd(&F
), WorkSpace(Workspace
),
220 GrowFactor(Grow
), Limit(Limit
)
222 // disable Moveable if we don't grow
224 this->Flags
&= ~Moveable
;
227 // kfreebsd doesn't have mremap, so we use the fallback
228 if ((this->Flags
& Moveable
) == Moveable
)
229 this->Flags
|= Fallback
;
232 unsigned long long EndOfFile
= Fd
->Size();
233 if (EndOfFile
> WorkSpace
)
234 WorkSpace
= EndOfFile
;
235 else if(WorkSpace
> 0)
237 Fd
->Seek(WorkSpace
- 1);
239 Fd
->Write(&C
,sizeof(C
));
246 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
247 // ---------------------------------------------------------------------
248 /* We try here to use mmap to reserve some space - this is much more
249 cooler than the fallback solution to simply allocate a char array
250 and could come in handy later than we are able to grow such an mmap */
251 DynamicMMap::DynamicMMap(unsigned long Flags
,unsigned long const &WorkSpace
,
252 unsigned long const &Grow
, unsigned long const &Limit
) :
253 MMap(Flags
| UnMapped
), Fd(0), WorkSpace(WorkSpace
),
254 GrowFactor(Grow
), Limit(Limit
)
256 // disable Moveable if we don't grow
258 this->Flags
&= ~Moveable
;
261 // kfreebsd doesn't have mremap, so we use the fallback
262 if ((this->Flags
& Moveable
) == Moveable
)
263 this->Flags
|= Fallback
;
266 #ifdef _POSIX_MAPPED_FILES
267 if ((this->Flags
& Fallback
) != Fallback
) {
268 // Set the permissions.
269 int Prot
= PROT_READ
;
271 int Map
= MAP_PRIVATE
| MAP_ANONYMOUS
;
273 int Map
= MAP_PRIVATE
| MAP_ANON
;
275 if ((this->Flags
& ReadOnly
) != ReadOnly
)
277 if ((this->Flags
& Public
) == Public
)
279 Map
= MAP_SHARED
| MAP_ANONYMOUS
;
281 Map
= MAP_SHARED
| MAP_ANON
;
284 // use anonymous mmap() to get the memory
285 Base
= (unsigned char*) mmap(0, WorkSpace
, Prot
, Map
, -1, 0);
287 if(Base
== MAP_FAILED
)
288 _error
->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace
);
294 // fallback to a static allocated space
295 Base
= calloc(WorkSpace
, 1);
299 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
300 // ---------------------------------------------------------------------
301 /* We truncate the file to the size of the memory data set */
302 DynamicMMap::~DynamicMMap()
306 if (validData() == false)
308 #ifdef _POSIX_MAPPED_FILES
309 munmap(Base
, WorkSpace
);
316 unsigned long long EndOfFile
= iSize
;
319 if(ftruncate(Fd
->Fd(),EndOfFile
) < 0)
320 _error
->Errno("ftruncate", _("Failed to truncate file"));
323 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
324 // ---------------------------------------------------------------------
325 /* This allocates a block of memory aligned to the given size */
326 unsigned long DynamicMMap::RawAllocate(unsigned long long Size
,unsigned long Aln
)
328 unsigned long long Result
= iSize
;
330 Result
+= Aln
- (iSize%Aln
);
332 iSize
= Result
+ Size
;
334 // try to grow the buffer
335 while(Result
+ Size
> WorkSpace
)
339 _error
->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
340 "of APT::Cache-Start. Current value: %lu. (man 5 apt.conf)"), WorkSpace
);
347 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
348 // ---------------------------------------------------------------------
349 /* This allocates an Item of size ItemSize so that it is aligned to its
351 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
353 if (unlikely(ItemSize
== 0))
355 _error
->Fatal("Can't allocate an item of size zero");
359 // Look for a matching pool entry
362 for (I
= Pools
; I
!= Pools
+ PoolCount
; ++I
)
364 if (I
->ItemSize
== 0)
366 if (I
->ItemSize
== ItemSize
)
369 // No pool is allocated, use an unallocated one
370 if (I
== Pools
+ PoolCount
)
372 // Woops, we ran out, the calling code should allocate more.
375 _error
->Error("Ran out of allocation pools");
380 I
->ItemSize
= ItemSize
;
384 unsigned long Result
= 0;
385 // Out of space, allocate some more
388 const unsigned long size
= 20*1024;
389 I
->Count
= size
/ItemSize
;
390 Pool
* oldPools
= Pools
;
391 _error
->PushToStack();
392 Result
= RawAllocate(size
,ItemSize
);
393 bool const newError
= _error
->PendingError();
394 _error
->MergeWithStack();
395 if (Pools
!= oldPools
)
396 I
+= Pools
- oldPools
;
398 // Does the allocation failed ?
399 if (Result
== 0 && newError
)
407 I
->Start
+= ItemSize
;
408 return Result
/ItemSize
;
411 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
412 // ---------------------------------------------------------------------
413 /* Strings are aligned to 16 bytes */
414 unsigned long DynamicMMap::WriteString(const char *String
,
417 if (Len
== (unsigned long)-1)
418 Len
= strlen(String
);
420 _error
->PushToStack();
421 unsigned long Result
= RawAllocate(Len
+1+sizeof(uint16_t),sizeof(uint16_t));
422 bool const newError
= _error
->PendingError();
423 _error
->MergeWithStack();
425 if (Base
== NULL
|| (Result
== 0 && newError
))
428 if (Len
>= std::numeric_limits
<uint16_t>::max())
431 uint16_t LenToWrite
= Len
;
432 memcpy((char *)Base
+ Result
, &LenToWrite
, sizeof(LenToWrite
));
433 Result
+= + sizeof(LenToWrite
);
435 memcpy((char *)Base
+ Result
,String
,Len
);
436 ((char *)Base
)[Result
+ Len
] = 0;
440 // DynamicMMap::Grow - Grow the mmap /*{{{*/
441 // ---------------------------------------------------------------------
442 /* This method is a wrapper around different methods to (try to) grow
443 a mmap (or our char[]-fallback). Encounterable environments:
444 1. Moveable + !Fallback + linux -> mremap with MREMAP_MAYMOVE
445 2. Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
446 3. Moveable + Fallback -> realloc
447 4. !Moveable + !Fallback + linux -> mremap alone - which will fail in 99,9%
448 5. !Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
449 6. !Moveable + Fallback -> not possible
450 [ While Moveable and Fallback stands for the equally named flags and
451 "linux" indicates a linux kernel instead of a freebsd kernel. ]
452 So what you can see here is, that a MMAP which want to be growable need
453 to be moveable to have a real chance but that this method will at least try
454 the nearly impossible 4 to grow it before it finally give up: Never say never. */
455 bool DynamicMMap::Grow() {
456 if (Limit
!= 0 && WorkSpace
>= Limit
)
457 return _error
->Error(_("Unable to increase the size of the MMap as the "
458 "limit of %lu bytes is already reached."), Limit
);
460 return _error
->Error(_("Unable to increase size of the MMap as automatic growing is disabled by user."));
462 unsigned long long const newSize
= WorkSpace
+ GrowFactor
;
465 Fd
->Seek(newSize
- 1);
467 Fd
->Write(&C
,sizeof(C
));
470 unsigned long const poolOffset
= Pools
- ((Pool
*) Base
);
472 if ((Flags
& Fallback
) != Fallback
) {
473 #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
474 #ifdef MREMAP_MAYMOVE
476 if ((Flags
& Moveable
) == Moveable
)
477 Base
= mremap(Base
, WorkSpace
, newSize
, MREMAP_MAYMOVE
);
480 Base
= mremap(Base
, WorkSpace
, newSize
, 0);
482 if(Base
== MAP_FAILED
)
488 if ((Flags
& Moveable
) != Moveable
)
491 Base
= realloc(Base
, newSize
);
495 /* Set new memory to 0 */
496 memset((char*)Base
+ WorkSpace
, 0, newSize
- WorkSpace
);
499 Pools
=(Pool
*) Base
+ poolOffset
;