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