projects
/
apt.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
implement MarkAndSweep in cc instead of header
[apt.git]
/
apt-pkg
/
contrib
/
mmap.cc
diff --git
a/apt-pkg/contrib/mmap.cc
b/apt-pkg/contrib/mmap.cc
index 2d12b6fe9091417440d5769a6dbe4839e8e795db..b2a53a6cb3a342b647f91b474b88705fa89152e4 100644
(file)
--- a/
apt-pkg/contrib/mmap.cc
+++ b/
apt-pkg/contrib/mmap.cc
@@
-22,11
+22,11
@@
#include <apt-pkg/mmap.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/mmap.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
+#include <apt-pkg/macros.h>
+#include <string>
#include <sys/mman.h>
#include <sys/mman.h>
-#include <sys/stat.h>
#include <unistd.h>
#include <unistd.h>
-#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <cstring>
#include <stdlib.h>
#include <errno.h>
#include <cstring>
@@
-83,7
+83,7
@@
bool MMap::Map(FileFd &Fd)
{
if ((Flags & ReadOnly) != ReadOnly)
return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
{
if ((Flags & ReadOnly) != ReadOnly)
return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
- Base =
new unsigned char[iSize]
;
+ Base =
malloc(iSize)
;
SyncToFd = new FileFd();
if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false)
return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str());
SyncToFd = new FileFd();
if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false)
return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str());
@@
-91,17
+91,18
@@
bool MMap::Map(FileFd &Fd)
}
// Map it.
}
// Map it.
- Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
+ Base =
(Flags & Fallback) ? MAP_FAILED :
mmap(0,iSize,Prot,Map,Fd.Fd(),0);
if (Base == (void *)-1)
{
if (Base == (void *)-1)
{
- if (errno == ENODEV || errno == EINVAL)
+ if (errno == ENODEV || errno == EINVAL
|| (Flags & Fallback)
)
{
// The filesystem doesn't support this particular kind of mmap.
// So we allocate a buffer and read the whole file into it.
if ((Flags & ReadOnly) == ReadOnly)
{
// for readonly, we don't need sync, so make it simple
{
// The filesystem doesn't support this particular kind of mmap.
// So we allocate a buffer and read the whole file into it.
if ((Flags & ReadOnly) == ReadOnly)
{
// for readonly, we don't need sync, so make it simple
- Base = new unsigned char[iSize];
+ Base = malloc(iSize);
+ SyncToFd = new FileFd();
return Fd.Read(Base, iSize);
}
// FIXME: Writing to compressed fd's ?
return Fd.Read(Base, iSize);
}
// FIXME: Writing to compressed fd's ?
@@
-109,7
+110,7
@@
bool MMap::Map(FileFd &Fd)
if (dupped_fd == -1)
return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
if (dupped_fd == -1)
return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
- Base =
new unsigned char[iSize]
;
+ Base =
calloc(iSize, 1)
;
SyncToFd = new FileFd (dupped_fd);
if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
return false;
SyncToFd = new FileFd (dupped_fd);
if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
return false;
@@
-135,7
+136,7
@@
bool MMap::Close(bool DoSync)
if (SyncToFd != NULL)
{
if (SyncToFd != NULL)
{
-
delete[] (char *)Base
;
+
free(Base)
;
delete SyncToFd;
SyncToFd = NULL;
}
delete SyncToFd;
SyncToFd = NULL;
}
@@
-155,11
+156,10
@@
bool MMap::Close(bool DoSync)
/* This is done in syncronous mode - the docs indicate that this will
not return till all IO is complete */
bool MMap::Sync()
/* This is done in syncronous mode - the docs indicate that this will
not return till all IO is complete */
bool MMap::Sync()
-{
+{
if ((Flags & UnMapped) == UnMapped)
return true;
if ((Flags & UnMapped) == UnMapped)
return true;
-
-#ifdef _POSIX_SYNCHRONIZED_IO
+
if ((Flags & ReadOnly) != ReadOnly)
{
if (SyncToFd != NULL)
if ((Flags & ReadOnly) != ReadOnly)
{
if (SyncToFd != NULL)
@@
-169,11
+169,12
@@
bool MMap::Sync()
}
else
{
}
else
{
+#ifdef _POSIX_SYNCHRONIZED_IO
if (msync((char *)Base, iSize, MS_SYNC) < 0)
return _error->Errno("msync", _("Unable to synchronize mmap"));
if (msync((char *)Base, iSize, MS_SYNC) < 0)
return _error->Errno("msync", _("Unable to synchronize mmap"));
+#endif
}
}
}
}
-#endif
return true;
}
/*}}}*/
return true;
}
/*}}}*/
@@
-184,9
+185,7
@@
bool MMap::Sync(unsigned long Start,unsigned long Stop)
{
if ((Flags & UnMapped) == UnMapped)
return true;
{
if ((Flags & UnMapped) == UnMapped)
return true;
-
-#ifdef _POSIX_SYNCHRONIZED_IO
- unsigned long long PSize = sysconf(_SC_PAGESIZE);
+
if ((Flags & ReadOnly) != ReadOnly)
{
if (SyncToFd != 0)
if ((Flags & ReadOnly) != ReadOnly)
{
if (SyncToFd != 0)
@@
-197,11
+196,13
@@
bool MMap::Sync(unsigned long Start,unsigned long Stop)
}
else
{
}
else
{
- if (msync((char *)Base+(unsigned long long)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0)
+#ifdef _POSIX_SYNCHRONIZED_IO
+ unsigned long long const PSize = sysconf(_SC_PAGESIZE);
+ if (msync((char *)Base+(Start/PSize)*PSize, Stop - Start, MS_SYNC) < 0)
return _error->Errno("msync", _("Unable to synchronize mmap"));
return _error->Errno("msync", _("Unable to synchronize mmap"));
+#endif
}
}
}
}
-#endif
return true;
}
/*}}}*/
return true;
}
/*}}}*/
@@
-216,7
+217,17
@@
DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Work
{
if (_error->PendingError() == true)
return;
{
if (_error->PendingError() == true)
return;
-
+
+ // disable Moveable if we don't grow
+ if (Grow == 0)
+ this->Flags &= ~Moveable;
+
+#ifndef __linux__
+ // kfreebsd doesn't have mremap, so we use the fallback
+ if ((this->Flags & Moveable) == Moveable)
+ this->Flags |= Fallback;
+#endif
+
unsigned long long EndOfFile = Fd->Size();
if (EndOfFile > WorkSpace)
WorkSpace = EndOfFile;
unsigned long long EndOfFile = Fd->Size();
if (EndOfFile > WorkSpace)
WorkSpace = EndOfFile;
@@
-283,8
+294,7
@@
DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace,
}
#endif
// fallback to a static allocated space
}
#endif
// fallback to a static allocated space
- Base = new unsigned char[WorkSpace];
- memset(Base,0,WorkSpace);
+ Base = calloc(WorkSpace, 1);
iSize = 0;
}
/*}}}*/
iSize = 0;
}
/*}}}*/
@@
-300,7
+310,7
@@
DynamicMMap::~DynamicMMap()
#ifdef _POSIX_MAPPED_FILES
munmap(Base, WorkSpace);
#else
#ifdef _POSIX_MAPPED_FILES
munmap(Base, WorkSpace);
#else
-
delete [] (unsigned char *)Base
;
+
free(Base)
;
#endif
return;
}
#endif
return;
}
@@
-329,7
+339,7
@@
unsigned long DynamicMMap::RawAllocate(unsigned long long Size,unsigned long Aln
if(!Grow())
{
_error->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
if(!Grow())
{
_error->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
- "of APT::Cache-
Limi
t. Current value: %lu. (man 5 apt.conf)"), WorkSpace);
+ "of APT::Cache-
Star
t. Current value: %lu. (man 5 apt.conf)"), WorkSpace);
return 0;
}
}
return 0;
}
}
@@
-342,6
+352,12
@@
unsigned long DynamicMMap::RawAllocate(unsigned long long Size,unsigned long Aln
size in the file. */
unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
{
size in the file. */
unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
{
+ if (unlikely(ItemSize == 0))
+ {
+ _error->Fatal("Can't allocate an item of size zero");
+ return 0;
+ }
+
// Look for a matching pool entry
Pool *I;
Pool *Empty = 0;
// Look for a matching pool entry
Pool *I;
Pool *Empty = 0;
@@
-402,7
+418,7
@@
unsigned long DynamicMMap::WriteString(const char *String,
unsigned long const Result = RawAllocate(Len+1,0);
unsigned long const Result = RawAllocate(Len+1,0);
- if (
Result == 0 && _error->PendingError(
))
+ if (
Base == NULL || (Result == 0 && _error->PendingError()
))
return 0;
memcpy((char *)Base + Result,String,Len);
return 0;
memcpy((char *)Base + Result,String,Len);
@@
-464,6
+480,9
@@
bool DynamicMMap::Grow() {
Base = realloc(Base, newSize);
if (Base == NULL)
return false;
Base = realloc(Base, newSize);
if (Base == NULL)
return false;
+ else
+ /* Set new memory to 0 */
+ memset((char*)Base + WorkSpace, 0, newSize - WorkSpace);
}
Pools =(Pool*) Base + poolOffset;
}
Pools =(Pool*) Base + poolOffset;