]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
d38b7b3d | 3 | // $Id: mmap.cc,v 1.10 1998/11/23 07:03:08 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> | |
34 | #include <sys/user.h> | |
35 | #include <unistd.h> | |
36 | #include <fcntl.h> | |
37 | /*}}}*/ | |
38 | ||
39 | // MMap::MMap - Constructor /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* */ | |
8e06abb2 | 42 | MMap::MMap(FileFd &F,unsigned long Flags) : Fd(F), Flags(Flags), iSize(0), |
578bfd0a AL |
43 | Base(0) |
44 | { | |
45 | if ((Flags & NoImmMap) != NoImmMap) | |
46 | Map(); | |
47 | } | |
48 | /*}}}*/ | |
49 | // MMap::~MMap - Destructor /*{{{*/ | |
50 | // --------------------------------------------------------------------- | |
51 | /* */ | |
52 | MMap::~MMap() | |
53 | { | |
54 | Close(true); | |
55 | } | |
56 | /*}}}*/ | |
57 | // MMap::Map - Perform the mapping /*{{{*/ | |
58 | // --------------------------------------------------------------------- | |
59 | /* */ | |
60 | bool MMap::Map() | |
61 | { | |
62 | iSize = Fd.Size(); | |
63 | ||
64 | // Set the permissions. | |
65 | int Prot = PROT_READ; | |
66 | int Map = MAP_SHARED; | |
67 | if ((Flags & ReadOnly) != ReadOnly) | |
68 | Prot |= PROT_WRITE; | |
69 | if ((Flags & Public) != Public) | |
70 | Map = MAP_PRIVATE; | |
71 | ||
b35d2f5f AL |
72 | if (iSize == 0) |
73 | return _error->Error("Can't mmap an empty file"); | |
74 | ||
578bfd0a AL |
75 | // Map it. |
76 | Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0); | |
77 | if (Base == (void *)-1) | |
78 | return _error->Errno("mmap","Couldn't make mmap of %u bytes",iSize); | |
79 | ||
80 | return true; | |
81 | } | |
82 | /*}}}*/ | |
83 | // MMap::Close - Close the map /*{{{*/ | |
84 | // --------------------------------------------------------------------- | |
85 | /* */ | |
1164783d | 86 | bool MMap::Close(bool DoClose, bool DoSync) |
578bfd0a AL |
87 | { |
88 | if (Fd.IsOpen() == false) | |
89 | return true; | |
90 | ||
1164783d AL |
91 | if (DoSync == true) |
92 | Sync(); | |
578bfd0a AL |
93 | |
94 | if (munmap((char *)Base,iSize) != 0) | |
95 | _error->Warning("Unable to munmap"); | |
96 | ||
97 | iSize = 0; | |
98 | if (DoClose == true) | |
99 | Fd.Close(); | |
100 | return true; | |
101 | } | |
102 | /*}}}*/ | |
103 | // MMap::Sync - Syncronize the map with the disk /*{{{*/ | |
104 | // --------------------------------------------------------------------- | |
0149949b AL |
105 | /* This is done in syncronous mode - the docs indicate that this will |
106 | not return till all IO is complete */ | |
578bfd0a AL |
107 | bool MMap::Sync() |
108 | { | |
1164783d | 109 | if ((Flags & ReadOnly) != ReadOnly) |
578bfd0a AL |
110 | if (msync((char *)Base,iSize,MS_SYNC) != 0) |
111 | return _error->Error("msync","Unable to write mmap"); | |
112 | return true; | |
113 | } | |
114 | /*}}}*/ | |
115 | // MMap::Sync - Syncronize a section of the file to disk /*{{{*/ | |
116 | // --------------------------------------------------------------------- | |
117 | /* */ | |
118 | bool MMap::Sync(unsigned long Start,unsigned long Stop) | |
119 | { | |
1164783d | 120 | if ((Flags & ReadOnly) != ReadOnly) |
578bfd0a AL |
121 | if (msync((char *)Base+(int)(Start/PAGE_SIZE)*PAGE_SIZE,Stop - Start,MS_SYNC) != 0) |
122 | return _error->Error("msync","Unable to write mmap"); | |
123 | return true; | |
124 | } | |
125 | /*}}}*/ | |
126 | ||
127 | // DynamicMMap::DynamicMMap - Constructor /*{{{*/ | |
128 | // --------------------------------------------------------------------- | |
129 | /* */ | |
8e06abb2 | 130 | DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) : |
578bfd0a AL |
131 | MMap(F,Flags | NoImmMap), WorkSpace(WorkSpace) |
132 | { | |
d38b7b3d AL |
133 | if (_error->PendingError() == true) |
134 | return; | |
135 | ||
578bfd0a AL |
136 | unsigned long EndOfFile = Fd.Size(); |
137 | Fd.Seek(WorkSpace); | |
138 | char C = 0; | |
139 | Fd.Write(&C,sizeof(C)); | |
140 | Map(); | |
141 | iSize = EndOfFile; | |
142 | } | |
143 | /*}}}*/ | |
144 | // DynamicMMap::~DynamicMMap - Destructor /*{{{*/ | |
145 | // --------------------------------------------------------------------- | |
146 | /* We truncate the file to the size of the memory data set */ | |
147 | DynamicMMap::~DynamicMMap() | |
148 | { | |
149 | unsigned long EndOfFile = iSize; | |
1164783d AL |
150 | Sync(); |
151 | iSize = WorkSpace; | |
152 | Close(false,false); | |
578bfd0a AL |
153 | ftruncate(Fd.Fd(),EndOfFile); |
154 | Fd.Close(); | |
155 | } | |
156 | /*}}}*/ | |
157 | // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/ | |
158 | // --------------------------------------------------------------------- | |
f55a958f AL |
159 | /* This allocates a block of memory aligned to the given size */ |
160 | unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) | |
578bfd0a AL |
161 | { |
162 | unsigned long Result = iSize; | |
f55a958f AL |
163 | if (Aln != 0) |
164 | Result += Aln - (iSize%Aln); | |
165 | ||
166 | iSize = Result + Size; | |
578bfd0a AL |
167 | |
168 | // Just in case error check | |
e5eebd12 | 169 | if (Result + Size > WorkSpace) |
578bfd0a AL |
170 | { |
171 | _error->Error("Dynamic MMap ran out of room"); | |
172 | return 0; | |
173 | } | |
578bfd0a AL |
174 | return Result; |
175 | } | |
176 | /*}}}*/ | |
177 | // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/ | |
178 | // --------------------------------------------------------------------- | |
179 | /* This allocates an Item of size ItemSize so that it is aligned to its | |
180 | size in the file. */ | |
181 | unsigned long DynamicMMap::Allocate(unsigned long ItemSize) | |
182 | { | |
183 | // Look for a matching pool entry | |
184 | Pool *I; | |
185 | Pool *Empty = 0; | |
186 | for (I = Pools; I != Pools + PoolCount; I++) | |
187 | { | |
188 | if (I->ItemSize == 0) | |
189 | Empty = I; | |
190 | if (I->ItemSize == ItemSize) | |
191 | break; | |
192 | } | |
193 | ||
194 | // No pool is allocated, use an unallocated one | |
195 | if (I == Pools + PoolCount) | |
196 | { | |
197 | // Woops, we ran out, the calling code should allocate more. | |
198 | if (Empty == 0) | |
199 | { | |
200 | _error->Error("Ran out of allocation pools"); | |
201 | return 0; | |
202 | } | |
203 | ||
204 | I = Empty; | |
205 | I->ItemSize = ItemSize; | |
206 | I->Count = 0; | |
207 | } | |
208 | ||
209 | // Out of space, allocate some more | |
210 | if (I->Count == 0) | |
211 | { | |
212 | I->Count = 20*1024/ItemSize; | |
f55a958f | 213 | I->Start = RawAllocate(I->Count*ItemSize,ItemSize); |
578bfd0a | 214 | } |
f55a958f | 215 | |
578bfd0a AL |
216 | I->Count--; |
217 | unsigned long Result = I->Start; | |
218 | I->Start += ItemSize; | |
219 | return Result/ItemSize; | |
220 | } | |
221 | /*}}}*/ | |
222 | // DynamicMMap::WriteString - Write a string to the file /*{{{*/ | |
223 | // --------------------------------------------------------------------- | |
224 | /* Strings are not aligned to anything */ | |
225 | unsigned long DynamicMMap::WriteString(const char *String, | |
226 | unsigned long Len) | |
227 | { | |
228 | unsigned long Result = iSize; | |
229 | // Just in case error check | |
230 | if (Result > WorkSpace) | |
231 | { | |
232 | _error->Error("Dynamic MMap ran out of room"); | |
233 | return 0; | |
234 | } | |
235 | ||
236 | if (Len == 0) | |
237 | Len = strlen(String); | |
238 | iSize += Len + 1; | |
239 | memcpy((char *)Base + Result,String,Len); | |
240 | ((char *)Base)[Result + Len] = 0; | |
241 | return Result; | |
242 | } | |
243 | /*}}}*/ |