1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.cc,v 1.3 1999/10/24 06:53:12 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 "dsync/mmap.h"
29 #include <dsync/mmap.h>
30 #include <dsync/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 %u 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");
108 // MMap::Sync - Syncronize the map with the disk /*{{{*/
109 // ---------------------------------------------------------------------
110 /* This is done in syncronous mode - the docs indicate that this will
111 not return till all IO is complete */
114 if ((Flags & UnMapped) == UnMapped)
117 #ifdef _POSIX_SYNCHRONIZED_IO
118 if ((Flags & ReadOnly) != ReadOnly)
119 if (msync((char *)Base,iSize,MS_SYNC) != 0)
120 return _error->Errno("msync","Unable to write mmap");
125 // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
126 // ---------------------------------------------------------------------
128 bool MMap::Sync(unsigned long Start,unsigned long Stop)
130 if ((Flags & UnMapped) == UnMapped)
133 #ifdef _POSIX_SYNCHRONIZED_IO
134 unsigned long PSize = sysconf(_SC_PAGESIZE);
135 if ((Flags & ReadOnly) != ReadOnly)
136 if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) != 0)
137 return _error->Errno("msync","Unable to write mmap");
143 // DynamicMMap::DynamicMMap - Constructor /*{{{*/
144 // ---------------------------------------------------------------------
146 DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
147 MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(WorkSpace)
149 if (_error->PendingError() == true)
152 unsigned long EndOfFile = Fd->Size();
155 Fd->Write(&C,sizeof(C));
160 // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
161 // ---------------------------------------------------------------------
162 /* This is just a fancy malloc really.. */
163 DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) :
164 MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace)
166 if (_error->PendingError() == true)
169 Base = new unsigned char[WorkSpace];
173 // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
174 // ---------------------------------------------------------------------
175 /* We truncate the file to the size of the memory data set */
176 DynamicMMap::~DynamicMMap()
180 delete [] (unsigned char *)Base;
184 unsigned long EndOfFile = iSize;
188 ftruncate(Fd->Fd(),EndOfFile);
192 // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
193 // ---------------------------------------------------------------------
194 /* This allocates a block of memory aligned to the given size */
195 unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
197 unsigned long Result = iSize;
199 Result += Aln - (iSize%Aln);
201 iSize = Result + Size;
203 // Just in case error check
204 if (Result + Size > WorkSpace)
206 _error->Error("Dynamic MMap ran out of room");
213 // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
214 // ---------------------------------------------------------------------
215 /* This allocates an Item of size ItemSize so that it is aligned to its
217 unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
219 // Look for a matching pool entry
222 for (I = Pools; I != Pools + PoolCount; I++)
224 if (I->ItemSize == 0)
226 if (I->ItemSize == ItemSize)
230 // No pool is allocated, use an unallocated one
231 if (I == Pools + PoolCount)
233 // Woops, we ran out, the calling code should allocate more.
236 _error->Error("Ran out of allocation pools");
241 I->ItemSize = ItemSize;
245 // Out of space, allocate some more
248 I->Count = 20*1024/ItemSize;
249 I->Start = RawAllocate(I->Count*ItemSize,ItemSize);
253 unsigned long Result = I->Start;
254 I->Start += ItemSize;
255 return Result/ItemSize;
258 // DynamicMMap::WriteString - Write a string to the file /*{{{*/
259 // ---------------------------------------------------------------------
260 /* Strings are not aligned to anything */
261 unsigned long DynamicMMap::WriteString(const char *String,
264 unsigned long Result = iSize;
265 // Just in case error check
266 if (Result + Len > WorkSpace)
268 _error->Error("Dynamic MMap ran out of room");
272 if (Len == (unsigned long)-1)
273 Len = strlen(String);
275 memcpy((char *)Base + Result,String,Len);
276 ((char *)Base)[Result + Len] = 0;