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