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