]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.cc
9febc5cdde70019bc11c2b79808d632388610445
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.cc,v 1.2 1998/07/04 05:57:42 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 <pkglib/mmap.h>
26 #include <pkglib/error.h>
35 // MMap::MMap - Constructor /*{{{*/
36 // ---------------------------------------------------------------------
38 MMap::MMap(File
&F
,unsigned long Flags
) : Fd(F
), Flags(Flags
), iSize(0),
41 if ((Flags
& NoImmMap
) != NoImmMap
)
45 // MMap::~MMap - Destructor /*{{{*/
46 // ---------------------------------------------------------------------
53 // MMap::Map - Perform the mapping /*{{{*/
54 // ---------------------------------------------------------------------
60 // Set the permissions.
63 if ((Flags
& ReadOnly
) != ReadOnly
)
65 if ((Flags
& Public
) != Public
)
69 Base
= mmap(0,iSize
,Prot
,Map
,Fd
.Fd(),0);
70 if (Base
== (void *)-1)
71 return _error
->Errno("mmap","Couldn't make mmap of %u bytes",iSize
);
76 // MMap::Close - Close the map /*{{{*/
77 // ---------------------------------------------------------------------
79 bool MMap::Close(bool DoClose
)
81 if (Fd
.IsOpen() == false)
86 if (munmap((char *)Base
,iSize
) != 0)
87 _error
->Warning("Unable to munmap");
95 // MMap::Sync - Syncronize the map with the disk /*{{{*/
96 // ---------------------------------------------------------------------
100 if ((Flags
& ReadOnly
) == ReadOnly
)
101 if (msync((char *)Base
,iSize
,MS_SYNC
) != 0)
102 return _error
->Error("msync","Unable to write mmap");
106 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
107 // ---------------------------------------------------------------------
109 bool MMap::Sync(unsigned long Start
,unsigned long Stop
)
111 if ((Flags
& ReadOnly
) == ReadOnly
)
112 if (msync((char *)Base
+(int)(Start
/PAGE_SIZE
)*PAGE_SIZE
,Stop
- Start
,MS_SYNC
) != 0)
113 return _error
->Error("msync","Unable to write mmap");
118 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
119 // ---------------------------------------------------------------------
121 DynamicMMap::DynamicMMap(File
&F
,unsigned long Flags
,unsigned long WorkSpace
) :
122 MMap(F
,Flags
| NoImmMap
), WorkSpace(WorkSpace
)
124 unsigned long EndOfFile
= Fd
.Size();
127 Fd
.Write(&C
,sizeof(C
));
132 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
133 // ---------------------------------------------------------------------
134 /* We truncate the file to the size of the memory data set */
135 DynamicMMap::~DynamicMMap()
137 unsigned long EndOfFile
= iSize
;
139 ftruncate(Fd
.Fd(),EndOfFile
);
143 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
144 // ---------------------------------------------------------------------
145 /* This allocates a block of memory aligned to the given size */
146 unsigned long DynamicMMap::RawAllocate(unsigned long Size
,unsigned long Aln
)
148 unsigned long Result
= iSize
;
150 Result
+= Aln
- (iSize%Aln
);
152 iSize
= Result
+ Size
;
154 // Just in case error check
155 if (Result
> WorkSpace
)
157 _error
->Error("Dynamic MMap ran out of room");
163 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
164 // ---------------------------------------------------------------------
165 /* This allocates an Item of size ItemSize so that it is aligned to its
167 unsigned long DynamicMMap::Allocate(unsigned long ItemSize
)
169 // Look for a matching pool entry
172 for (I
= Pools
; I
!= Pools
+ PoolCount
; I
++)
174 if (I
->ItemSize
== 0)
176 if (I
->ItemSize
== ItemSize
)
180 // No pool is allocated, use an unallocated one
181 if (I
== Pools
+ PoolCount
)
183 // Woops, we ran out, the calling code should allocate more.
186 _error
->Error("Ran out of allocation pools");
191 I
->ItemSize
= ItemSize
;
195 // Out of space, allocate some more
198 I
->Count
= 20*1024/ItemSize
;
199 I
->Start
= RawAllocate(I
->Count
*ItemSize
,ItemSize
);
203 unsigned long Result
= I
->Start
;
204 I
->Start
+= ItemSize
;
205 return Result
/ItemSize
;
208 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
209 // ---------------------------------------------------------------------
210 /* Strings are not aligned to anything */
211 unsigned long DynamicMMap::WriteString(const char *String
,
214 unsigned long Result
= iSize
;
215 // Just in case error check
216 if (Result
> WorkSpace
)
218 _error
->Error("Dynamic MMap ran out of room");
223 Len
= strlen(String
);
225 memcpy((char *)Base
+ Result
,String
,Len
);
226 ((char *)Base
)[Result
+ Len
] = 0;