]>
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 /*{{{*/
20 #include <apt-pkg/mmap.h>
21 #include <apt-pkg/error.h>
34 // MMap::MMap - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
37 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Flags(Flags
), iSize(0),
40 if ((Flags
& NoImmMap
) != NoImmMap
)
44 // MMap::MMap - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
47 MMap::MMap(unsigned long Flags
) : Flags(Flags
), iSize(0),
52 // MMap::~MMap - Destructor /*{{{*/
53 // ---------------------------------------------------------------------
60 // MMap::Map - Perform the mapping /*{{{*/
61 // ---------------------------------------------------------------------
63 bool MMap::Map(FileFd
&Fd
)
67 // Set the permissions.
70 if ((Flags
& ReadOnly
) != ReadOnly
)
72 if ((Flags
& Public
) != Public
)
76 return _error
->Error(_("Can't mmap an empty file"));
79 Base
= mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
80 if (Base
== (void *)-1)
81 return _error
->Errno("mmap",_("Couldn't make mmap of %lu bytes"),iSize
);
86 // MMap::Close - Close the map /*{{{*/
87 // ---------------------------------------------------------------------
89 bool MMap::Close(bool DoSync
)
91 if ((Flags
& UnMapped
) == UnMapped
|| Base
== 0 || iSize
== 0)
97 if (munmap((char *)Base
,iSize
) != 0)
98 _error
->Warning("Unable to munmap");
105 // MMap::Sync - Syncronize the map with the disk /*{{{*/
106 // ---------------------------------------------------------------------
107 /* This is done in syncronous mode - the docs indicate that this will
108 not return till all IO is complete */
111 if ((Flags
& UnMapped
) == UnMapped
)
114 #ifdef _POSIX_SYNCHRONIZED_IO
115 if ((Flags
& ReadOnly
) != ReadOnly
)
116 if (msync((char *)Base
,iSize
,MS_SYNC
) < 0)
117 return _error
->Errno("msync","Unable to write mmap");
122 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
123 // ---------------------------------------------------------------------
125 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
127 if ((Flags
& UnMapped
) == UnMapped
)
130 #ifdef _POSIX_SYNCHRONIZED_IO
131 unsigned long PSize
= sysconf(_SC_PAGESIZE
);
132 if ((Flags
& ReadOnly
) != ReadOnly
)
133 if (msync((char *)Base
+(int)(Start
/PSize
)*PSize
,Stop
- Start
,MS_SYNC
) < 0)
134 return _error
->Errno("msync","Unable to write mmap");
140 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
141 // ---------------------------------------------------------------------
143 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long WorkSpace
) :
144 MMap(F
,Flags
| NoImmMap
), Fd(&F
), WorkSpace(WorkSpace
)
146 if (_error
->PendingError() == true)
149 unsigned long EndOfFile
= Fd
->Size();
150 if (EndOfFile
> WorkSpace
)
151 WorkSpace
= EndOfFile
;
152 else if(WorkSpace
> 0)
154 Fd
->Seek(WorkSpace
- 1);
156 Fd
->Write(&C
,sizeof(C
));
163 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
164 // ---------------------------------------------------------------------
165 /* We try here to use mmap to reserve some space - this is much more
166 cooler than the fallback solution to simply allocate a char array
167 and could come in handy later than we are able to grow such an mmap */
168 DynamicMMap::DynamicMMap(unsigned long Flags
,unsigned long WorkSpace
) :
169 MMap(Flags
| NoImmMap
| UnMapped
), Fd(0), WorkSpace(WorkSpace
)
171 if (_error
->PendingError() == true)
174 #ifdef _POSIX_MAPPED_FILES
175 // Set the permissions.
176 int Prot
= PROT_READ
;
177 int Map
= MAP_PRIVATE
| MAP_ANONYMOUS
;
178 if ((Flags
& ReadOnly
) != ReadOnly
)
180 if ((Flags
& Public
) == Public
)
181 Map
= MAP_SHARED
| MAP_ANONYMOUS
;
183 // use anonymous mmap() to get the memory
184 Base
= (unsigned char*) mmap(0, WorkSpace
, Prot
, Map
, -1, 0);
186 if(Base
== MAP_FAILED
)
187 _error
->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace
);
189 // fallback to a static allocated space
190 Base
= new unsigned char[WorkSpace
];
191 memset(Base
,0,WorkSpace
);
196 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
197 // ---------------------------------------------------------------------
198 /* We truncate the file to the size of the memory data set */
199 DynamicMMap::~DynamicMMap()
203 #ifdef _POSIX_MAPPED_FILES
204 munmap(Base
, WorkSpace
);
206 delete [] (unsigned char *)Base
;
211 unsigned long EndOfFile
= iSize
;
214 if(ftruncate(Fd
->Fd(),EndOfFile
) < 0)
215 _error
->Errno("ftruncate", _("Failed to truncate file"));
218 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This allocates a block of memory aligned to the given size */
221 unsigned long DynamicMMap::RawAllocate(unsigned long Size
,unsigned long Aln
)
223 unsigned long Result
= iSize
;
225 Result
+= Aln
- (iSize%Aln
);
227 iSize
= Result
+ Size
;
229 // try to grow the buffer
230 while(Result
+ Size
> WorkSpace
)
234 _error
->Error(_("Dynamic MMap ran out of room. Please increase the size "
235 "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace
);
242 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
243 // ---------------------------------------------------------------------
244 /* This allocates an Item of size ItemSize so that it is aligned to its
246 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
248 // Look for a matching pool entry
251 for (I
= Pools
; I
!= Pools
+ PoolCount
; I
++)
253 if (I
->ItemSize
== 0)
255 if (I
->ItemSize
== ItemSize
)
258 // No pool is allocated, use an unallocated one
259 if (I
== Pools
+ PoolCount
)
261 // Woops, we ran out, the calling code should allocate more.
264 _error
->Error("Ran out of allocation pools");
269 I
->ItemSize
= ItemSize
;
273 unsigned long Result
= 0;
274 // Out of space, allocate some more
277 const unsigned long size
= 20*1024;
278 I
->Count
= size
/ItemSize
;
279 Result
= RawAllocate(size
,ItemSize
);
280 // Does the allocation failed ?
281 if (Result
== 0 && _error
->PendingError())
289 I
->Start
+= ItemSize
;
290 return Result
/ItemSize
;
293 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
294 // ---------------------------------------------------------------------
295 /* Strings are not aligned to anything */
296 unsigned long DynamicMMap::WriteString(const char *String
,
299 if (Len
== (unsigned long)-1)
300 Len
= strlen(String
);
302 unsigned long Result
= RawAllocate(Len
+1,0);
304 if (Result
== 0 && _error
->PendingError())
307 memcpy((char *)Base
+ Result
,String
,Len
);
308 ((char *)Base
)[Result
+ Len
] = 0;
312 // DynamicMMap::Grow - Grow the mmap /*{{{*/
313 // ---------------------------------------------------------------------
314 /* This method will try to grow the mmap we currently use. This doesn't
315 work most of the time because we can't move the mmap around in the
316 memory for now as this would require to adjust quite a lot of pointers
317 but why we should not at least try to grow it before we give up? */
318 bool DynamicMMap::Grow()
320 #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
321 unsigned long newSize
= WorkSpace
+ 1024*1024;
325 Fd
->Seek(newSize
- 1);
327 Fd
->Write(&C
,sizeof(C
));
330 Base
= mremap(Base
, WorkSpace
, newSize
, 0);
331 if(Base
== MAP_FAILED
)