]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
Backed out previous apt-pkg/contrib/mmap.cc fix/kludge/...
[apt.git] / apt-pkg / contrib / mmap.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mmap.cc,v 1.13 1999/03/18 04:10:21 doogie 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 <sys/user.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 /*}}}*/
38
39 // MMap::MMap - Constructor /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 MMap::MMap(FileFd &F,unsigned long Flags) : Fd(F), Flags(Flags), iSize(0),
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
72 if (iSize == 0)
73 return _error->Error("Can't mmap an empty file");
74
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 /* */
86 bool MMap::Close(bool DoClose, bool DoSync)
87 {
88 if (Fd.IsOpen() == false)
89 return true;
90
91 if (DoSync == true)
92 Sync();
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 // ---------------------------------------------------------------------
105 /* This is done in syncronous mode - the docs indicate that this will
106 not return till all IO is complete */
107 bool MMap::Sync()
108 {
109 #ifdef _POSIX_SYNCHRONIZED_IO
110 if ((Flags & ReadOnly) != ReadOnly)
111 if (msync((char *)Base,iSize,MS_SYNC) != 0)
112 return _error->Error("msync","Unable to write mmap");
113 #endif
114 return true;
115 }
116 /*}}}*/
117 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
118 // ---------------------------------------------------------------------
119 /* */
120 bool MMap::Sync(unsigned long Start,unsigned long Stop)
121 {
122
123 #ifdef _POSIX_SYNCHRONIZED_IO
124 if ((Flags & ReadOnly) != ReadOnly)
125 if (msync((char *)Base+(int)(Start/PAGE_SIZE)*PAGE_SIZE,Stop - Start,MS_SYNC) != 0)
126 return _error->Error("msync","Unable to write mmap");
127 #endif
128 return true;
129 }
130 /*}}}*/
131
132 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
133 // ---------------------------------------------------------------------
134 /* */
135 DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
136 MMap(F,Flags | NoImmMap), WorkSpace(WorkSpace)
137 {
138 if (_error->PendingError() == true)
139 return;
140
141 unsigned long EndOfFile = Fd.Size();
142 Fd.Seek(WorkSpace);
143 char C = 0;
144 Fd.Write(&C,sizeof(C));
145 Map();
146 iSize = EndOfFile;
147 }
148 /*}}}*/
149 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
150 // ---------------------------------------------------------------------
151 /* We truncate the file to the size of the memory data set */
152 DynamicMMap::~DynamicMMap()
153 {
154 unsigned long EndOfFile = iSize;
155 Sync();
156 iSize = WorkSpace;
157 Close(false,false);
158 ftruncate(Fd.Fd(),EndOfFile);
159 Fd.Close();
160 }
161 /*}}}*/
162 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This allocates a block of memory aligned to the given size */
165 unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
166 {
167 unsigned long Result = iSize;
168 if (Aln != 0)
169 Result += Aln - (iSize%Aln);
170
171 iSize = Result + Size;
172
173 // Just in case error check
174 if (Result + Size > WorkSpace)
175 {
176 _error->Error("Dynamic MMap ran out of room");
177 return 0;
178 }
179 return Result;
180 }
181 /*}}}*/
182 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
183 // ---------------------------------------------------------------------
184 /* This allocates an Item of size ItemSize so that it is aligned to its
185 size in the file. */
186 unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
187 {
188 // Look for a matching pool entry
189 Pool *I;
190 Pool *Empty = 0;
191 for (I = Pools; I != Pools + PoolCount; I++)
192 {
193 if (I->ItemSize == 0)
194 Empty = I;
195 if (I->ItemSize == ItemSize)
196 break;
197 }
198
199 // No pool is allocated, use an unallocated one
200 if (I == Pools + PoolCount)
201 {
202 // Woops, we ran out, the calling code should allocate more.
203 if (Empty == 0)
204 {
205 _error->Error("Ran out of allocation pools");
206 return 0;
207 }
208
209 I = Empty;
210 I->ItemSize = ItemSize;
211 I->Count = 0;
212 }
213
214 // Out of space, allocate some more
215 if (I->Count == 0)
216 {
217 I->Count = 20*1024/ItemSize;
218 I->Start = RawAllocate(I->Count*ItemSize,ItemSize);
219 }
220
221 I->Count--;
222 unsigned long Result = I->Start;
223 I->Start += ItemSize;
224 return Result/ItemSize;
225 }
226 /*}}}*/
227 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
228 // ---------------------------------------------------------------------
229 /* Strings are not aligned to anything */
230 unsigned long DynamicMMap::WriteString(const char *String,
231 unsigned long Len)
232 {
233 unsigned long Result = iSize;
234 // Just in case error check
235 if (Result > WorkSpace)
236 {
237 _error->Error("Dynamic MMap ran out of room");
238 return 0;
239 }
240
241 if (Len == 0)
242 Len = strlen(String);
243 iSize += Len + 1;
244 memcpy((char *)Base + Result,String,Len);
245 ((char *)Base)[Result + Len] = 0;
246 return Result;
247 }
248 /*}}}*/