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