]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.cc,v 1.11 1999/02/05 02:26:13 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 #pragma implementation "apt-pkg/mmap.h"
29 #include <apt-pkg/mmap.h>
30 #include <apt-pkg/error.h>
39 // MMap::MMap - Constructor /*{{{*/
40 // ---------------------------------------------------------------------
42 MMap::MMap(FileFd
&F
,unsigned long Flags
) : Fd(F
), Flags(Flags
), iSize(0),
45 if ((Flags
& NoImmMap
) != NoImmMap
)
49 // MMap::~MMap - Destructor /*{{{*/
50 // ---------------------------------------------------------------------
57 // MMap::Map - Perform the mapping /*{{{*/
58 // ---------------------------------------------------------------------
64 // Set the permissions.
67 if ((Flags
& ReadOnly
) != ReadOnly
)
69 if ((Flags
& Public
) != Public
)
73 return _error
->Error("Can't mmap an empty file");
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
);
83 // MMap::Close - Close the map /*{{{*/
84 // ---------------------------------------------------------------------
86 bool MMap::Close(bool DoClose
, bool DoSync
)
88 if (Fd
.IsOpen() == false)
94 if (munmap((char *)Base
,iSize
) != 0)
95 _error
->Warning("Unable to munmap");
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 */
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");
117 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
118 // ---------------------------------------------------------------------
120 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
122 #ifdef _POSIX_SYNCHRONIZED_IO
123 if ((Flags
& ReadOnly
) != ReadOnly
)
124 if (msync((char *)Base
+(int)(Start
/PAGE_SIZE
)*PAGE_SIZE
,Stop
- Start
,MS_SYNC
) != 0)
125 return _error
->Error("msync","Unable to write mmap");
131 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
132 // ---------------------------------------------------------------------
134 DynamicMMap::DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long WorkSpace
) :
135 MMap(F
,Flags
| NoImmMap
), WorkSpace(WorkSpace
)
137 if (_error
->PendingError() == true)
140 unsigned long EndOfFile
= Fd
.Size();
143 Fd
.Write(&C
,sizeof(C
));
148 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
149 // ---------------------------------------------------------------------
150 /* We truncate the file to the size of the memory data set */
151 DynamicMMap::~DynamicMMap()
153 unsigned long EndOfFile
= iSize
;
157 ftruncate(Fd
.Fd(),EndOfFile
);
161 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
162 // ---------------------------------------------------------------------
163 /* This allocates a block of memory aligned to the given size */
164 unsigned long DynamicMMap::RawAllocate(unsigned long Size
,unsigned long Aln
)
166 unsigned long Result
= iSize
;
168 Result
+= Aln
- (iSize%Aln
);
170 iSize
= Result
+ Size
;
172 // Just in case error check
173 if (Result
+ Size
> WorkSpace
)
175 _error
->Error("Dynamic MMap ran out of room");
181 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
182 // ---------------------------------------------------------------------
183 /* This allocates an Item of size ItemSize so that it is aligned to its
185 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
187 // Look for a matching pool entry
190 for (I
= Pools
; I
!= Pools
+ PoolCount
; I
++)
192 if (I
->ItemSize
== 0)
194 if (I
->ItemSize
== ItemSize
)
198 // No pool is allocated, use an unallocated one
199 if (I
== Pools
+ PoolCount
)
201 // Woops, we ran out, the calling code should allocate more.
204 _error
->Error("Ran out of allocation pools");
209 I
->ItemSize
= ItemSize
;
213 // Out of space, allocate some more
216 I
->Count
= 20*1024/ItemSize
;
217 I
->Start
= RawAllocate(I
->Count
*ItemSize
,ItemSize
);
221 unsigned long Result
= I
->Start
;
222 I
->Start
+= ItemSize
;
223 return Result
/ItemSize
;
226 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
227 // ---------------------------------------------------------------------
228 /* Strings are not aligned to anything */
229 unsigned long DynamicMMap::WriteString(const char *String
,
232 unsigned long Result
= iSize
;
233 // Just in case error check
234 if (Result
> WorkSpace
)
236 _error
->Error("Dynamic MMap ran out of room");
241 Len
= strlen(String
);
243 memcpy((char *)Base
+ Result
,String
,Len
);
244 ((char *)Base
)[Result
+ Len
] = 0;