]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 jgg Exp $
4 /* ######################################################################
6 MMap Class - Provides 'real' mmap or a faked mmap using read().
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
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.
21 ##################################################################### */
23 // Include Files /*{{{*/
25 #include <apt-pkg/mmap.h>
26 #include <apt-pkg/error.h>
38 // MMap::MMap - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
41 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Flags(Flags
), iSize(0),
44 if ((Flags
& NoImmMap
) != NoImmMap
)
48 // MMap::MMap - Constructor /*{{{*/
49 // ---------------------------------------------------------------------
51 MMap::MMap(unsigned long Flags
) : Flags(Flags
), iSize(0),
56 // MMap::~MMap - Destructor /*{{{*/
57 // ---------------------------------------------------------------------
64 // MMap::Map - Perform the mapping /*{{{*/
65 // ---------------------------------------------------------------------
67 bool MMap::Map(FileFd
&Fd
)
71 // Set the permissions.
74 if ((Flags
& ReadOnly
) != ReadOnly
)
76 if ((Flags
& Public
) != Public
)
80 return _error
->Error(_("Can't mmap an empty file"));
83 Base
= mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
84 if (Base
== (void *)-1)
85 return _error
->Errno("mmap",_("Couldn't make mmap of %lu bytes"),iSize
);
90 // MMap::Close - Close the map /*{{{*/
91 // ---------------------------------------------------------------------
93 bool MMap::Close(bool DoSync
)
95 if ((Flags
& UnMapped
) == UnMapped
|| Base
== 0 || iSize
== 0)
101 if (munmap((char *)Base
,iSize
) != 0)
102 _error
->Warning("Unable to munmap");
109 // MMap::Sync - Syncronize the map with the disk /*{{{*/
110 // ---------------------------------------------------------------------
111 /* This is done in syncronous mode - the docs indicate that this will
112 not return till all IO is complete */
115 if ((Flags
& UnMapped
) == UnMapped
)
118 #ifdef _POSIX_SYNCHRONIZED_IO
119 if ((Flags
& ReadOnly
) != ReadOnly
)
120 if (msync((char *)Base
,iSize
,MS_SYNC
) < 0)
121 return _error
->Errno("msync","Unable to write mmap");
126 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
127 // ---------------------------------------------------------------------
129 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
131 if ((Flags
& UnMapped
) == UnMapped
)
134 #ifdef _POSIX_SYNCHRONIZED_IO
135 unsigned long PSize
= sysconf(_SC_PAGESIZE
);
136 if ((Flags
& ReadOnly
) != ReadOnly
)
137 if (msync((char *)Base
+(int)(Start
/PSize
)*PSize
,Stop
- Start
,MS_SYNC
) < 0)
138 return _error
->Errno("msync","Unable to write mmap");
144 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
145 // ---------------------------------------------------------------------
147 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long WorkSpace
) :
148 MMap(F
,Flags
| NoImmMap
), Fd(&F
), WorkSpace(WorkSpace
)
150 if (_error
->PendingError() == true)
153 unsigned long EndOfFile
= Fd
->Size();
154 if (EndOfFile
> WorkSpace
)
155 WorkSpace
= EndOfFile
;
156 else if(WorkSpace
> 0)
158 Fd
->Seek(WorkSpace
- 1);
160 Fd
->Write(&C
,sizeof(C
));
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
)
173 if (_error
->PendingError() == true)
176 Base
= new unsigned char[WorkSpace
];
177 memset(Base
,0,WorkSpace
);
181 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
182 // ---------------------------------------------------------------------
183 /* We truncate the file to the size of the memory data set */
184 DynamicMMap::~DynamicMMap()
188 delete [] (unsigned char *)Base
;
192 unsigned long EndOfFile
= iSize
;
195 ftruncate(Fd
->Fd(),EndOfFile
);
198 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This allocates a block of memory aligned to the given size */
201 unsigned long DynamicMMap::RawAllocate(unsigned long Size
,unsigned long Aln
)
203 unsigned long Result
= iSize
;
205 Result
+= Aln
- (iSize%Aln
);
207 iSize
= Result
+ Size
;
209 // Just in case error check
210 if (Result
+ Size
> WorkSpace
)
212 _error
->Error("Dynamic MMap ran out of room");
219 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
220 // ---------------------------------------------------------------------
221 /* This allocates an Item of size ItemSize so that it is aligned to its
223 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
225 // Look for a matching pool entry
228 for (I
= Pools
; I
!= Pools
+ PoolCount
; I
++)
230 if (I
->ItemSize
== 0)
232 if (I
->ItemSize
== ItemSize
)
236 // No pool is allocated, use an unallocated one
237 if (I
== Pools
+ PoolCount
)
239 // Woops, we ran out, the calling code should allocate more.
242 _error
->Error("Ran out of allocation pools");
247 I
->ItemSize
= ItemSize
;
251 // Out of space, allocate some more
254 I
->Count
= 20*1024/ItemSize
;
255 I
->Start
= RawAllocate(I
->Count
*ItemSize
,ItemSize
);
259 unsigned long Result
= I
->Start
;
260 I
->Start
+= ItemSize
;
261 return Result
/ItemSize
;
264 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
265 // ---------------------------------------------------------------------
266 /* Strings are not aligned to anything */
267 unsigned long DynamicMMap::WriteString(const char *String
,
270 unsigned long Result
= iSize
;
271 // Just in case error check
272 if (Result
+ Len
> WorkSpace
)
274 _error
->Error("Dynamic MMap ran out of room");
278 if (Len
== (unsigned long)-1)
279 Len
= strlen(String
);
281 memcpy((char *)Base
+ Result
,String
,Len
);
282 ((char *)Base
)[Result
+ Len
] = 0;