]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
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 /*{{{*/
22 #include <apt-pkg/mmap.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/fileutl.h>
37 // MMap::MMap - Constructor /*{{{*/
38 // ---------------------------------------------------------------------
40 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Flags(Flags
), iSize(0),
41 Base(0), SyncToFd(NULL
)
43 if ((Flags
& NoImmMap
) != NoImmMap
)
47 // MMap::MMap - Constructor /*{{{*/
48 // ---------------------------------------------------------------------
50 MMap::MMap(unsigned long Flags
) : Flags(Flags
), iSize(0),
51 Base(0), SyncToFd(NULL
)
55 // MMap::~MMap - Destructor /*{{{*/
56 // ---------------------------------------------------------------------
63 // MMap::Map - Perform the mapping /*{{{*/
64 // ---------------------------------------------------------------------
66 bool MMap::Map(FileFd
&Fd
)
70 // Set the permissions.
73 if ((Flags
& ReadOnly
) != ReadOnly
)
75 if ((Flags
& Public
) != Public
)
79 return _error
->Error(_("Can't mmap an empty file"));
82 Base
= mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
83 if (Base
== (void *)-1)
85 if (errno
== ENODEV
|| errno
== EINVAL
)
87 // The filesystem doesn't support this particular kind of mmap.
88 // So we allocate a buffer and read the whole file into it.
89 int const dupped_fd
= dup(Fd
.Fd());
91 return _error
->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd
.Fd());
93 Base
= new unsigned char[iSize
];
94 SyncToFd
= new FileFd (dupped_fd
);
95 if (!SyncToFd
->Seek(0L) || !SyncToFd
->Read(Base
, iSize
))
99 return _error
->Errno("mmap",_("Couldn't make mmap of %llu bytes"),
106 // MMap::Close - Close the map /*{{{*/
107 // ---------------------------------------------------------------------
109 bool MMap::Close(bool DoSync
)
111 if ((Flags
& UnMapped
) == UnMapped
|| validData() == false || iSize
== 0)
117 if (SyncToFd
!= NULL
)
119 delete[] (char *)Base
;
125 if (munmap((char *)Base
, iSize
) != 0)
126 _error
->WarningE("mmap", _("Unable to close mmap"));
134 // MMap::Sync - Syncronize the map with the disk /*{{{*/
135 // ---------------------------------------------------------------------
136 /* This is done in syncronous mode - the docs indicate that this will
137 not return till all IO is complete */
140 if ((Flags
& UnMapped
) == UnMapped
)
143 #ifdef _POSIX_SYNCHRONIZED_IO
144 if ((Flags
& ReadOnly
) != ReadOnly
)
146 if (SyncToFd
!= NULL
)
148 if (!SyncToFd
->Seek(0) || !SyncToFd
->Write(Base
, iSize
))
153 if (msync((char *)Base
, iSize
, MS_SYNC
) < 0)
154 return _error
->Errno("msync", _("Unable to synchronize mmap"));
161 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
162 // ---------------------------------------------------------------------
164 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
166 if ((Flags
& UnMapped
) == UnMapped
)
169 #ifdef _POSIX_SYNCHRONIZED_IO
170 unsigned long long PSize
= sysconf(_SC_PAGESIZE
);
171 if ((Flags
& ReadOnly
) != ReadOnly
)
175 if (!SyncToFd
->Seek(0) ||
176 !SyncToFd
->Write (((char *)Base
)+Start
, Stop
-Start
))
181 if (msync((char *)Base
+(unsigned long long)(Start
/PSize
)*PSize
,Stop
- Start
,MS_SYNC
) < 0)
182 return _error
->Errno("msync", _("Unable to synchronize mmap"));
190 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
191 // ---------------------------------------------------------------------
193 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long const &Workspace
,
194 unsigned long const &Grow
, unsigned long const &Limit
) :
195 MMap(F
,Flags
| NoImmMap
), Fd(&F
), WorkSpace(Workspace
),
196 GrowFactor(Grow
), Limit(Limit
)
198 if (_error
->PendingError() == true)
201 unsigned long long EndOfFile
= Fd
->Size();
202 if (EndOfFile
> WorkSpace
)
203 WorkSpace
= EndOfFile
;
204 else if(WorkSpace
> 0)
206 Fd
->Seek(WorkSpace
- 1);
208 Fd
->Write(&C
,sizeof(C
));
215 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
216 // ---------------------------------------------------------------------
217 /* We try here to use mmap to reserve some space - this is much more
218 cooler than the fallback solution to simply allocate a char array
219 and could come in handy later than we are able to grow such an mmap */
220 DynamicMMap::DynamicMMap(unsigned long Flags
,unsigned long const &WorkSpace
,
221 unsigned long const &Grow
, unsigned long const &Limit
) :
222 MMap(Flags
| NoImmMap
| UnMapped
), Fd(0), WorkSpace(WorkSpace
),
223 GrowFactor(Grow
), Limit(Limit
)
225 if (_error
->PendingError() == true)
228 // disable Moveable if we don't grow
230 this->Flags
&= ~Moveable
;
233 // kfreebsd doesn't have mremap, so we use the fallback
234 if ((this->Flags
& Moveable
) == Moveable
)
235 this->Flags
|= Fallback
;
238 #ifdef _POSIX_MAPPED_FILES
239 if ((this->Flags
& Fallback
) != Fallback
) {
240 // Set the permissions.
241 int Prot
= PROT_READ
;
243 int Map
= MAP_PRIVATE
| MAP_ANONYMOUS
;
245 int Map
= MAP_PRIVATE
| MAP_ANON
;
247 if ((this->Flags
& ReadOnly
) != ReadOnly
)
249 if ((this->Flags
& Public
) == Public
)
251 Map
= MAP_SHARED
| MAP_ANONYMOUS
;
253 Map
= MAP_SHARED
| MAP_ANON
;
256 // use anonymous mmap() to get the memory
257 Base
= (unsigned char*) mmap(0, WorkSpace
, Prot
, Map
, -1, 0);
259 if(Base
== MAP_FAILED
)
260 _error
->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace
);
266 // fallback to a static allocated space
267 Base
= new unsigned char[WorkSpace
];
268 memset(Base
,0,WorkSpace
);
272 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
273 // ---------------------------------------------------------------------
274 /* We truncate the file to the size of the memory data set */
275 DynamicMMap::~DynamicMMap()
279 if (validData() == false)
281 #ifdef _POSIX_MAPPED_FILES
282 munmap(Base
, WorkSpace
);
284 delete [] (unsigned char *)Base
;
289 unsigned long long EndOfFile
= iSize
;
292 if(ftruncate(Fd
->Fd(),EndOfFile
) < 0)
293 _error
->Errno("ftruncate", _("Failed to truncate file"));
296 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This allocates a block of memory aligned to the given size */
299 unsigned long DynamicMMap::RawAllocate(unsigned long long Size
,unsigned long Aln
)
301 unsigned long long Result
= iSize
;
303 Result
+= Aln
- (iSize%Aln
);
305 iSize
= Result
+ Size
;
307 // try to grow the buffer
308 while(Result
+ Size
> WorkSpace
)
312 _error
->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
313 "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace
);
320 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
321 // ---------------------------------------------------------------------
322 /* This allocates an Item of size ItemSize so that it is aligned to its
324 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
326 // Look for a matching pool entry
329 for (I
= Pools
; I
!= Pools
+ PoolCount
; ++I
)
331 if (I
->ItemSize
== 0)
333 if (I
->ItemSize
== ItemSize
)
336 // No pool is allocated, use an unallocated one
337 if (I
== Pools
+ PoolCount
)
339 // Woops, we ran out, the calling code should allocate more.
342 _error
->Error("Ran out of allocation pools");
347 I
->ItemSize
= ItemSize
;
351 unsigned long Result
= 0;
352 // Out of space, allocate some more
355 const unsigned long size
= 20*1024;
356 I
->Count
= size
/ItemSize
;
357 Pool
* oldPools
= Pools
;
358 Result
= RawAllocate(size
,ItemSize
);
359 if (Pools
!= oldPools
)
360 I
+= Pools
- oldPools
;
362 // Does the allocation failed ?
363 if (Result
== 0 && _error
->PendingError())
371 I
->Start
+= ItemSize
;
372 return Result
/ItemSize
;
375 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
376 // ---------------------------------------------------------------------
377 /* Strings are not aligned to anything */
378 unsigned long DynamicMMap::WriteString(const char *String
,
381 if (Len
== (unsigned long)-1)
382 Len
= strlen(String
);
384 unsigned long const Result
= RawAllocate(Len
+1,0);
386 if (Result
== 0 && _error
->PendingError())
389 memcpy((char *)Base
+ Result
,String
,Len
);
390 ((char *)Base
)[Result
+ Len
] = 0;
394 // DynamicMMap::Grow - Grow the mmap /*{{{*/
395 // ---------------------------------------------------------------------
396 /* This method is a wrapper around different methods to (try to) grow
397 a mmap (or our char[]-fallback). Encounterable environments:
398 1. Moveable + !Fallback + linux -> mremap with MREMAP_MAYMOVE
399 2. Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
400 3. Moveable + Fallback -> realloc
401 4. !Moveable + !Fallback + linux -> mremap alone - which will fail in 99,9%
402 5. !Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
403 6. !Moveable + Fallback -> not possible
404 [ While Moveable and Fallback stands for the equally named flags and
405 "linux" indicates a linux kernel instead of a freebsd kernel. ]
406 So what you can see here is, that a MMAP which want to be growable need
407 to be moveable to have a real chance but that this method will at least try
408 the nearly impossible 4 to grow it before it finally give up: Never say never. */
409 bool DynamicMMap::Grow() {
410 if (Limit
!= 0 && WorkSpace
>= Limit
)
411 return _error
->Error(_("Unable to increase the size of the MMap as the "
412 "limit of %lu bytes is already reached."), Limit
);
414 return _error
->Error(_("Unable to increase size of the MMap as automatic growing is disabled by user."));
416 unsigned long long const newSize
= WorkSpace
+ GrowFactor
;
419 Fd
->Seek(newSize
- 1);
421 Fd
->Write(&C
,sizeof(C
));
424 unsigned long const poolOffset
= Pools
- ((Pool
*) Base
);
426 if ((Flags
& Fallback
) != Fallback
) {
427 #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
428 #ifdef MREMAP_MAYMOVE
430 if ((Flags
& Moveable
) == Moveable
)
431 Base
= mremap(Base
, WorkSpace
, newSize
, MREMAP_MAYMOVE
);
434 Base
= mremap(Base
, WorkSpace
, newSize
, 0);
436 if(Base
== MAP_FAILED
)
442 if ((Flags
& Moveable
) != Moveable
)
445 Base
= realloc(Base
, newSize
);
450 Pools
=(Pool
*) Base
+ poolOffset
;