]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/mmap.cc
Fixed or handling bug
[apt.git] / apt-pkg / contrib / mmap.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
4b1b89c5 3// $Id: mmap.cc,v 1.20 1999/10/02 04:14:54 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
16 The configure test notes that some OS's have broken private mmap's
17 so on those OS's we can't use mmap. This means we have to use
18 configure to test mmap and can't rely on the POSIX
19 _POSIX_MAPPED_FILES test.
20
21 ##################################################################### */
22 /*}}}*/
23// Include Files /*{{{*/
6c139d6e 24#ifdef __GNUG__
094a497d 25#pragma implementation "apt-pkg/mmap.h"
6c139d6e
AL
26#endif
27
578bfd0a 28#define _BSD_SOURCE
094a497d
AL
29#include <apt-pkg/mmap.h>
30#include <apt-pkg/error.h>
578bfd0a
AL
31
32#include <sys/mman.h>
33#include <sys/stat.h>
578bfd0a
AL
34#include <unistd.h>
35#include <fcntl.h>
36 /*}}}*/
37
38// MMap::MMap - Constructor /*{{{*/
39// ---------------------------------------------------------------------
40/* */
2d11135a 41MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
578bfd0a
AL
42 Base(0)
43{
44 if ((Flags & NoImmMap) != NoImmMap)
2d11135a
AL
45 Map(F);
46}
47 /*}}}*/
48// MMap::MMap - Constructor /*{{{*/
49// ---------------------------------------------------------------------
50/* */
51MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
52 Base(0)
53{
578bfd0a
AL
54}
55 /*}}}*/
56// MMap::~MMap - Destructor /*{{{*/
57// ---------------------------------------------------------------------
58/* */
59MMap::~MMap()
60{
2d11135a 61 Close();
578bfd0a
AL
62}
63 /*}}}*/
64// MMap::Map - Perform the mapping /*{{{*/
65// ---------------------------------------------------------------------
66/* */
2d11135a 67bool MMap::Map(FileFd &Fd)
578bfd0a
AL
68{
69 iSize = Fd.Size();
70
71 // Set the permissions.
72 int Prot = PROT_READ;
73 int Map = MAP_SHARED;
74 if ((Flags & ReadOnly) != ReadOnly)
75 Prot |= PROT_WRITE;
76 if ((Flags & Public) != Public)
77 Map = MAP_PRIVATE;
78
b35d2f5f
AL
79 if (iSize == 0)
80 return _error->Error("Can't mmap an empty file");
81
578bfd0a
AL
82 // Map it.
83 Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
84 if (Base == (void *)-1)
85 return _error->Errno("mmap","Couldn't make mmap of %u bytes",iSize);
86
87 return true;
88}
89 /*}}}*/
90// MMap::Close - Close the map /*{{{*/
91// ---------------------------------------------------------------------
92/* */
2d11135a 93bool MMap::Close(bool DoSync)
578bfd0a 94{
2d11135a 95 if ((Flags & UnMapped) == UnMapped || Base == 0 || iSize == 0)
578bfd0a 96 return true;
2d11135a 97
1164783d
AL
98 if (DoSync == true)
99 Sync();
578bfd0a
AL
100
101 if (munmap((char *)Base,iSize) != 0)
102 _error->Warning("Unable to munmap");
103
104 iSize = 0;
578bfd0a
AL
105 return true;
106}
107 /*}}}*/
108// MMap::Sync - Syncronize the map with the disk /*{{{*/
109// ---------------------------------------------------------------------
0149949b
AL
110/* This is done in syncronous mode - the docs indicate that this will
111 not return till all IO is complete */
578bfd0a
AL
112bool MMap::Sync()
113{
2d11135a
AL
114 if ((Flags & UnMapped) == UnMapped)
115 return true;
116
de3c15ea 117#ifdef _POSIX_SYNCHRONIZED_IO
1164783d 118 if ((Flags & ReadOnly) != ReadOnly)
578bfd0a 119 if (msync((char *)Base,iSize,MS_SYNC) != 0)
4b1b89c5 120 return _error->Errno("msync","Unable to write mmap");
de3c15ea 121#endif
578bfd0a
AL
122 return true;
123}
124 /*}}}*/
125// MMap::Sync - Syncronize a section of the file to disk /*{{{*/
126// ---------------------------------------------------------------------
127/* */
128bool MMap::Sync(unsigned long Start,unsigned long Stop)
129{
2d11135a
AL
130 if ((Flags & UnMapped) == UnMapped)
131 return true;
132
35c22def
AL
133#ifdef _POSIX_SYNCHRONIZED_IO
134 unsigned long PSize = sysconf(_SC_PAGESIZE);
1164783d 135 if ((Flags & ReadOnly) != ReadOnly)
35c22def 136 if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) != 0)
4b1b89c5 137 return _error->Errno("msync","Unable to write mmap");
de3c15ea 138#endif
578bfd0a
AL
139 return true;
140}
141 /*}}}*/
142
143// DynamicMMap::DynamicMMap - Constructor /*{{{*/
144// ---------------------------------------------------------------------
145/* */
8e06abb2 146DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
2d11135a 147 MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(WorkSpace)
578bfd0a 148{
d38b7b3d
AL
149 if (_error->PendingError() == true)
150 return;
151
2d11135a
AL
152 unsigned long EndOfFile = Fd->Size();
153 Fd->Seek(WorkSpace);
578bfd0a 154 char C = 0;
2d11135a
AL
155 Fd->Write(&C,sizeof(C));
156 Map(F);
578bfd0a
AL
157 iSize = EndOfFile;
158}
159 /*}}}*/
2d11135a
AL
160// DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
161// ---------------------------------------------------------------------
162/* This is just a fancy malloc really.. */
163DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) :
164 MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace)
165{
166 if (_error->PendingError() == true)
167 return;
168
169 Base = new unsigned char[WorkSpace];
170 iSize = 0;
171}
172 /*}}}*/
578bfd0a
AL
173// DynamicMMap::~DynamicMMap - Destructor /*{{{*/
174// ---------------------------------------------------------------------
175/* We truncate the file to the size of the memory data set */
176DynamicMMap::~DynamicMMap()
177{
2d11135a
AL
178 if (Fd == 0)
179 {
180 delete [] (unsigned char *)Base;
181 return;
182 }
183
578bfd0a 184 unsigned long EndOfFile = iSize;
1164783d
AL
185 Sync();
186 iSize = WorkSpace;
2d11135a
AL
187 Close(false);
188 ftruncate(Fd->Fd(),EndOfFile);
189 Fd->Close();
578bfd0a
AL
190}
191 /*}}}*/
192// DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
193// ---------------------------------------------------------------------
f55a958f
AL
194/* This allocates a block of memory aligned to the given size */
195unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
578bfd0a
AL
196{
197 unsigned long Result = iSize;
f55a958f
AL
198 if (Aln != 0)
199 Result += Aln - (iSize%Aln);
200
201 iSize = Result + Size;
578bfd0a
AL
202
203 // Just in case error check
e5eebd12 204 if (Result + Size > WorkSpace)
578bfd0a
AL
205 {
206 _error->Error("Dynamic MMap ran out of room");
207 return 0;
208 }
470773ca 209
578bfd0a
AL
210 return Result;
211}
212 /*}}}*/
213// DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
214// ---------------------------------------------------------------------
215/* This allocates an Item of size ItemSize so that it is aligned to its
216 size in the file. */
217unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
218{
219 // Look for a matching pool entry
220 Pool *I;
221 Pool *Empty = 0;
222 for (I = Pools; I != Pools + PoolCount; I++)
223 {
224 if (I->ItemSize == 0)
225 Empty = I;
226 if (I->ItemSize == ItemSize)
227 break;
228 }
229
230 // No pool is allocated, use an unallocated one
231 if (I == Pools + PoolCount)
232 {
233 // Woops, we ran out, the calling code should allocate more.
234 if (Empty == 0)
235 {
236 _error->Error("Ran out of allocation pools");
237 return 0;
238 }
239
240 I = Empty;
241 I->ItemSize = ItemSize;
242 I->Count = 0;
243 }
244
245 // Out of space, allocate some more
246 if (I->Count == 0)
247 {
248 I->Count = 20*1024/ItemSize;
f55a958f 249 I->Start = RawAllocate(I->Count*ItemSize,ItemSize);
578bfd0a 250 }
f55a958f 251
578bfd0a
AL
252 I->Count--;
253 unsigned long Result = I->Start;
470773ca 254 I->Start += ItemSize;
578bfd0a
AL
255 return Result/ItemSize;
256}
257 /*}}}*/
258// DynamicMMap::WriteString - Write a string to the file /*{{{*/
259// ---------------------------------------------------------------------
260/* Strings are not aligned to anything */
261unsigned long DynamicMMap::WriteString(const char *String,
262 unsigned long Len)
263{
264 unsigned long Result = iSize;
265 // Just in case error check
470773ca 266 if (Result + Len > WorkSpace)
578bfd0a
AL
267 {
268 _error->Error("Dynamic MMap ran out of room");
269 return 0;
270 }
271
6e52073f 272 if (Len == (unsigned long)-1)
578bfd0a
AL
273 Len = strlen(String);
274 iSize += Len + 1;
275 memcpy((char *)Base + Result,String,Len);
276 ((char *)Base)[Result + Len] = 0;
277 return Result;
278}
279 /*}}}*/