// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: mmap.cc,v 1.1 1998/07/02 02:58:13 jgg Exp $
+// $Id: mmap.cc,v 1.8 1998/07/26 04:49:34 jgg Exp $
/* ######################################################################
MMap Class - Provides 'real' mmap or a faked mmap using read().
##################################################################### */
/*}}}*/
// Include Files /*{{{*/
+#ifdef __GNUG__
+#pragma implementation "apt-pkg/mmap.h"
+#endif
+
#define _BSD_SOURCE
-#include <pkglib/mmap.h>
-#include <pkglib/error.h>
+#include <apt-pkg/mmap.h>
+#include <apt-pkg/error.h>
#include <sys/mman.h>
#include <sys/stat.h>
// MMap::MMap - Constructor /*{{{*/
// ---------------------------------------------------------------------
/* */
-MMap::MMap(File &F,unsigned long Flags) : Fd(F), Flags(Flags), iSize(0),
+MMap::MMap(FileFd &F,unsigned long Flags) : Fd(F), Flags(Flags), iSize(0),
Base(0)
{
if ((Flags & NoImmMap) != NoImmMap)
if ((Flags & Public) != Public)
Map = MAP_PRIVATE;
+ if (iSize == 0)
+ return _error->Error("Can't mmap an empty file");
+
// Map it.
Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
if (Base == (void *)-1)
// MMap::Close - Close the map /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool MMap::Close(bool DoClose)
+bool MMap::Close(bool DoClose, bool DoSync)
{
if (Fd.IsOpen() == false)
return true;
- Sync();
+ if (DoSync == true)
+ Sync();
if (munmap((char *)Base,iSize) != 0)
_error->Warning("Unable to munmap");
/*}}}*/
// MMap::Sync - Syncronize the map with the disk /*{{{*/
// ---------------------------------------------------------------------
-/* */
+/* This is done in syncronous mode - the docs indicate that this will
+ not return till all IO is complete */
bool MMap::Sync()
{
- if ((Flags & ReadOnly) == ReadOnly)
+ if ((Flags & ReadOnly) != ReadOnly)
if (msync((char *)Base,iSize,MS_SYNC) != 0)
return _error->Error("msync","Unable to write mmap");
return true;
/* */
bool MMap::Sync(unsigned long Start,unsigned long Stop)
{
- if ((Flags & ReadOnly) == ReadOnly)
+ if ((Flags & ReadOnly) != ReadOnly)
if (msync((char *)Base+(int)(Start/PAGE_SIZE)*PAGE_SIZE,Stop - Start,MS_SYNC) != 0)
return _error->Error("msync","Unable to write mmap");
return true;
// DynamicMMap::DynamicMMap - Constructor /*{{{*/
// ---------------------------------------------------------------------
/* */
-DynamicMMap::DynamicMMap(File &F,unsigned long Flags,unsigned long WorkSpace) :
+DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
MMap(F,Flags | NoImmMap), WorkSpace(WorkSpace)
{
unsigned long EndOfFile = Fd.Size();
DynamicMMap::~DynamicMMap()
{
unsigned long EndOfFile = iSize;
- Close(false);
+ Sync();
+ iSize = WorkSpace;
+ Close(false,false);
ftruncate(Fd.Fd(),EndOfFile);
Fd.Close();
}
/*}}}*/
// DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
// ---------------------------------------------------------------------
-/* */
-unsigned long DynamicMMap::RawAllocate(unsigned long Size)
+/* This allocates a block of memory aligned to the given size */
+unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
{
unsigned long Result = iSize;
- iSize += Size;
+ if (Aln != 0)
+ Result += Aln - (iSize%Aln);
+
+ iSize = Result + Size;
// Just in case error check
if (Result > WorkSpace)
_error->Error("Dynamic MMap ran out of room");
return 0;
}
-
return Result;
}
/*}}}*/
if (I->Count == 0)
{
I->Count = 20*1024/ItemSize;
- I->Start = RawAllocate(I->Count*ItemSize);
+ I->Start = RawAllocate(I->Count*ItemSize,ItemSize);
}
-
+
I->Count--;
unsigned long Result = I->Start;
I->Start += ItemSize;