X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/578bfd0aed2ec993f4ad85fa6a7094a852261422..93bf083d699c60f1ac40297bfa6783fb0cb800d8:/apt-pkg/contrib/mmap.cc diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 85cac1cca..e12e077c6 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -1,6 +1,6 @@ // -*- 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(). @@ -21,9 +21,13 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#ifdef __GNUG__ +#pragma implementation "apt-pkg/mmap.h" +#endif + #define _BSD_SOURCE -#include -#include +#include +#include #include #include @@ -35,7 +39,7 @@ // 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) @@ -65,6 +69,9 @@ bool MMap::Map() 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) @@ -76,12 +83,13 @@ bool MMap::Map() // 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"); @@ -94,10 +102,11 @@ bool MMap::Close(bool DoClose) /*}}}*/ // 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; @@ -108,7 +117,7 @@ bool MMap::Sync() /* */ 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; @@ -118,7 +127,7 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) // 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(); @@ -135,18 +144,23 @@ DynamicMMap::DynamicMMap(File &F,unsigned long Flags,unsigned long WorkSpace) : 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) @@ -154,7 +168,6 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size) _error->Error("Dynamic MMap ran out of room"); return 0; } - return Result; } /*}}}*/ @@ -194,9 +207,9 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) 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;