##################################################################### */
/*}}}*/
// Include Files /*{{{*/
-#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
#include <config.h>
#include <apt-pkg/mmap.h>
// ---------------------------------------------------------------------
/* */
MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
- Base(0), SyncToFd(NULL)
+ Base(nullptr), SyncToFd(nullptr)
{
- if ((Flags & NoImmMap) != NoImmMap)
Map(F);
}
/*}}}*/
// ---------------------------------------------------------------------
/* */
MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
- Base(0), SyncToFd(NULL)
+ Base(nullptr), SyncToFd(nullptr)
{
}
/*}}}*/
if ((Flags & ReadOnly) != ReadOnly)
return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
Base = malloc(iSize);
+ if (unlikely(Base == nullptr))
+ return _error->Errno("MMap-compressed-malloc", _("Couldn't make mmap of %llu bytes"), 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());
// Map it.
Base = (Flags & Fallback) ? MAP_FAILED : mmap(0,iSize,Prot,Map,Fd.Fd(),0);
- if (Base == (void *)-1)
+ if (Base == MAP_FAILED)
{
if (errno == ENODEV || errno == EINVAL || (Flags & Fallback))
{
{
// for readonly, we don't need sync, so make it simple
Base = malloc(iSize);
+ if (unlikely(Base == nullptr))
+ return _error->Errno("MMap-malloc", _("Couldn't make mmap of %llu bytes"), iSize);
SyncToFd = new FileFd();
- return Fd.Read(Base, iSize);
+ return Fd.Seek(0L) && Fd.Read(Base, iSize);
}
// FIXME: Writing to compressed fd's ?
int const dupped_fd = dup(Fd.Fd());
if (dupped_fd == -1)
return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
- Base = calloc(iSize, 1);
+ Base = malloc(iSize);
+ if (unlikely(Base == nullptr))
+ return _error->Errno("MMap-calloc", _("Couldn't make mmap of %llu bytes"), iSize);
SyncToFd = new FileFd (dupped_fd);
if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
return false;
}
else
- return _error->Errno("mmap",_("Couldn't make mmap of %llu bytes"),
- iSize);
+ return _error->Errno("MMap-mmap", _("Couldn't make mmap of %llu bytes"), iSize);
}
return true;
{
if (SyncToFd != 0)
{
- if (!SyncToFd->Seek(0) ||
+ if (!SyncToFd->Seek(Start) ||
!SyncToFd->Write (((char *)Base)+Start, Stop-Start))
return false;
}
{
#ifdef _POSIX_SYNCHRONIZED_IO
unsigned long long const PSize = sysconf(_SC_PAGESIZE);
- if (msync((char *)Base+(Start/PSize)*PSize, Stop - Start, MS_SYNC) < 0)
+ Start = (Start/PSize)*PSize;
+ if (msync((char *)Base+Start, Stop - Start, MS_SYNC) < 0)
return _error->Errno("msync", _("Unable to synchronize mmap"));
#endif
}
/* */
DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Workspace,
unsigned long const &Grow, unsigned long const &Limit) :
- MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(Workspace),
+ MMap(Flags), Fd(&F), WorkSpace(Workspace),
GrowFactor(Grow), Limit(Limit)
{
- if (_error->PendingError() == true)
- return;
-
// disable Moveable if we don't grow
if (Grow == 0)
this->Flags &= ~Moveable;
and could come in handy later than we are able to grow such an mmap */
DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace,
unsigned long const &Grow, unsigned long const &Limit) :
- MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace),
+ MMap(Flags | UnMapped), Fd(0), WorkSpace(WorkSpace),
GrowFactor(Grow), Limit(Limit)
{
- if (_error->PendingError() == true)
- return;
-
// disable Moveable if we don't grow
if (Grow == 0)
this->Flags &= ~Moveable;
if (validData() == false)
return;
#ifdef _POSIX_MAPPED_FILES
- munmap(Base, WorkSpace);
-#else
- free(Base);
+ if ((Flags & Fallback) != Fallback) {
+ munmap(Base, WorkSpace);
+ } else
#endif
+ free(Base);
return;
}
const unsigned long size = 20*1024;
I->Count = size/ItemSize;
Pool* oldPools = Pools;
+ _error->PushToStack();
Result = RawAllocate(size,ItemSize);
+ bool const newError = _error->PendingError();
+ _error->MergeWithStack();
if (Pools != oldPools)
I += Pools - oldPools;
// Does the allocation failed ?
- if (Result == 0 && _error->PendingError())
+ if (Result == 0 && newError)
return 0;
I->Start = Result;
}
/*}}}*/
// DynamicMMap::WriteString - Write a string to the file /*{{{*/
// ---------------------------------------------------------------------
-/* Strings are not aligned to anything */
+/* Strings are aligned to 16 bytes */
unsigned long DynamicMMap::WriteString(const char *String,
unsigned long Len)
{
if (Len == (unsigned long)-1)
Len = strlen(String);
- unsigned long const Result = RawAllocate(Len+1,0);
+ _error->PushToStack();
+ unsigned long Result = RawAllocate(Len+1+sizeof(uint16_t),sizeof(uint16_t));
+ bool const newError = _error->PendingError();
+ _error->MergeWithStack();
- if (Base == NULL || (Result == 0 && _error->PendingError()))
+ if (Base == NULL || (Result == 0 && newError))
return 0;
+ if (Len >= std::numeric_limits<uint16_t>::max())
+ abort();
+
+ uint16_t LenToWrite = Len;
+ memcpy((char *)Base + Result, &LenToWrite, sizeof(LenToWrite));
+ Result += + sizeof(LenToWrite);
+
memcpy((char *)Base + Result,String,Len);
((char *)Base)[Result + Len] = 0;
return Result;
if ((Flags & Moveable) != Moveable)
return false;
- Base = realloc(Base, newSize);
- if (Base == NULL)
+ auto Temp = realloc(Base, newSize);
+ if (Temp == NULL)
return false;
- else
+ else {
+ Base = Temp;
/* Set new memory to 0 */
memset((char*)Base + WorkSpace, 0, newSize - WorkSpace);
+ }
}
Pools =(Pool*) Base + poolOffset;