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