]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
073bc9eb3e877f036f9910cf6c1860b138714cb6
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>
33 // MMap::MMap - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
36 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Flags(Flags
), iSize(0),
39 if ((Flags
& NoImmMap
) != NoImmMap
)
43 // MMap::MMap - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
46 MMap::MMap(unsigned long Flags
) : Flags(Flags
), iSize(0),
51 // MMap::~MMap - Destructor /*{{{*/
52 // ---------------------------------------------------------------------
59 // MMap::Map - Perform the mapping /*{{{*/
60 // ---------------------------------------------------------------------
62 bool MMap::Map(FileFd
&Fd
)
66 // Set the permissions.
69 if ((Flags
& ReadOnly
) != ReadOnly
)
71 if ((Flags
& Public
) != Public
)
75 return _error
->Error(_("Can't mmap an empty file"));
78 Base
= mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
79 if (Base
== (void *)-1)
80 return _error
->Errno("mmap",_("Couldn't make mmap of %lu bytes"),iSize
);
85 // MMap::Close - Close the map /*{{{*/
86 // ---------------------------------------------------------------------
88 bool MMap::Close(bool DoSync
)
90 if ((Flags
& UnMapped
) == UnMapped
|| Base
== 0 || iSize
== 0)
96 if (munmap((char *)Base
,iSize
) != 0)
97 _error
->Warning("Unable to munmap");
104 // MMap::Sync - Syncronize the map with the disk /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This is done in syncronous mode - the docs indicate that this will
107 not return till all IO is complete */
110 if ((Flags
& UnMapped
) == UnMapped
)
113 #ifdef _POSIX_SYNCHRONIZED_IO
114 if ((Flags
& ReadOnly
) != ReadOnly
)
115 if (msync((char *)Base
,iSize
,MS_SYNC
) < 0)
116 return _error
->Errno("msync","Unable to write mmap");
121 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
122 // ---------------------------------------------------------------------
124 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
126 if ((Flags
& UnMapped
) == UnMapped
)
129 #ifdef _POSIX_SYNCHRONIZED_IO
130 unsigned long PSize
= sysconf(_SC_PAGESIZE
);
131 if ((Flags
& ReadOnly
) != ReadOnly
)
132 if (msync((char *)Base
+(int)(Start
/PSize
)*PSize
,Stop
- Start
,MS_SYNC
) < 0)
133 return _error
->Errno("msync","Unable to write mmap");
139 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
140 // ---------------------------------------------------------------------
142 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long WorkSpace
) :
143 MMap(F
,Flags
| NoImmMap
), Fd(&F
), WorkSpace(WorkSpace
)
145 if (_error
->PendingError() == true)
148 unsigned long EndOfFile
= Fd
->Size();
149 if (EndOfFile
> WorkSpace
)
150 WorkSpace
= EndOfFile
;
151 else if(WorkSpace
> 0)
153 Fd
->Seek(WorkSpace
- 1);
155 Fd
->Write(&C
,sizeof(C
));
162 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
163 // ---------------------------------------------------------------------
164 /* We try here to use mmap to reserve some space - this is much more
165 cooler than the fallback solution to simply allocate a char array
166 and could come in handy later than we are able to grow such an mmap */
167 DynamicMMap::DynamicMMap(unsigned long Flags
,unsigned long WorkSpace
) :
168 MMap(Flags
| NoImmMap
| UnMapped
), Fd(0), WorkSpace(WorkSpace
)
170 if (_error
->PendingError() == true)
173 #ifdef _POSIX_MAPPED_FILES
174 // use anonymous mmap() to get the memory
175 Base
= (unsigned char*) mmap(0, WorkSpace
, PROT_READ
|PROT_WRITE
,
176 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
177 if(Base
!= MAP_FAILED
)
180 // fallback to a static allocated space
181 Base
= new unsigned char[WorkSpace
];
182 memset(Base
,0,WorkSpace
);
186 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
187 // ---------------------------------------------------------------------
188 /* We truncate the file to the size of the memory data set */
189 DynamicMMap::~DynamicMMap()
193 #ifdef _POSIX_MAPPED_FILES
194 munmap(Base
, WorkSpace
);
196 delete [] (unsigned char *)Base
;
201 unsigned long EndOfFile
= iSize
;
204 if(ftruncate(Fd
->Fd(),EndOfFile
) < 0)
205 _error
->Errno("ftruncate", _("Failed to truncate file"));
208 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This allocates a block of memory aligned to the given size */
211 unsigned long DynamicMMap::RawAllocate(unsigned long Size
,unsigned long Aln
)
213 unsigned long Result
= iSize
;
215 Result
+= Aln
- (iSize%Aln
);
217 iSize
= Result
+ Size
;
219 // try to grow the buffer
220 while(Result
+ Size
> WorkSpace
)
224 _error
->Error(_("Dynamic MMap ran out of room. Please increase the size "
225 "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace
);
232 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
233 // ---------------------------------------------------------------------
234 /* This allocates an Item of size ItemSize so that it is aligned to its
236 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
238 // Look for a matching pool entry
241 for (I
= Pools
; I
!= Pools
+ PoolCount
; I
++)
243 if (I
->ItemSize
== 0)
245 if (I
->ItemSize
== ItemSize
)
248 // No pool is allocated, use an unallocated one
249 if (I
== Pools
+ PoolCount
)
251 // Woops, we ran out, the calling code should allocate more.
254 _error
->Error("Ran out of allocation pools");
259 I
->ItemSize
= ItemSize
;
263 // Out of space, allocate some more
266 I
->Count
= 20*1024/ItemSize
;
267 I
->Start
= RawAllocate(I
->Count
*ItemSize
,ItemSize
);
271 unsigned long Result
= I
->Start
;
272 I
->Start
+= ItemSize
;
273 return Result
/ItemSize
;
276 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
277 // ---------------------------------------------------------------------
278 /* Strings are not aligned to anything */
279 unsigned long DynamicMMap::WriteString(const char *String
,
282 unsigned long Result
= iSize
;
283 // try to grow the buffer
284 while(Result
+ Len
> WorkSpace
)
288 _error
->Error(_("Dynamic MMap ran out of room. Please increase the size "
289 "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace
);
294 if (Len
== (unsigned long)-1)
295 Len
= strlen(String
);
297 memcpy((char *)Base
+ Result
,String
,Len
);
298 ((char *)Base
)[Result
+ Len
] = 0;
302 // DynamicMMap::Grow - Grow the mmap /*{{{*/
303 // ---------------------------------------------------------------------
304 /* This method will try to grow the mmap we currently use. This doesn't
305 work most of the time because we can't move the mmap around in the
306 memory for now as this would require to adjust quite a lot of pointers
307 but why we should not at least try to grow it before we give up? */
308 bool DynamicMMap::Grow()
310 #ifdef _POSIX_MAPPED_FILES
311 unsigned long newSize
= WorkSpace
+ 1024*1024;
315 Fd
->Seek(newSize
- 1);
317 Fd
->Write(&C
,sizeof(C
));
320 Base
= mremap(Base
, WorkSpace
, newSize
, 0);
321 if(Base
== MAP_FAILED
)