diff options
| author | Stefan Boberg <[email protected]> | 2021-10-01 22:13:06 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-10-01 22:13:06 +0200 |
| commit | e783450cabd5ce23c40b85d4095592ed4a26677d (patch) | |
| tree | af74e5af1799e8b5e65dba5ab0955bd8f02ac4ab /zencore/iobuffer.cpp | |
| parent | structured cache: fixed how HEAD requests are handled (diff) | |
| download | zen-e783450cabd5ce23c40b85d4095592ed4a26677d.tar.xz zen-e783450cabd5ce23c40b85d4095592ed4a26677d.zip | |
Added explicit mimalloc IoBuffer allocation path
Diffstat (limited to 'zencore/iobuffer.cpp')
| -rw-r--r-- | zencore/iobuffer.cpp | 81 |
1 files changed, 43 insertions, 38 deletions
diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index 244425761..04685defc 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -31,26 +31,29 @@ namespace zen { ////////////////////////////////////////////////////////////////////////// -void* +void IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) { #if ZEN_PLATFORM_WINDOWS if (((InSize & 0xffFF) == 0) && (Alignment == 0x10000)) { m_Flags |= kLowLevelAlloc; - return VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE); + m_DataPtr = VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE); + + return; } #endif // ZEN_PLATFORM_WINDOWS -#if ZEN_USE_MIMALLOC && 0 +#if ZEN_USE_MIMALLOC void* Ptr = mi_aligned_alloc(Alignment, RoundUp(InSize, Alignment)); + m_Flags |= kIoBufferAlloc; #else void* Ptr = Memory::Alloc(InSize, Alignment); #endif ZEN_ASSERT(Ptr); - return Ptr; + m_DataPtr = Ptr; } void @@ -70,11 +73,14 @@ IoBufferCore::FreeBuffer() } #endif // ZEN_PLATFORM_WINDOWS -#if ZEN_USE_MIMALLOC && 0 - return mi_free(const_cast<void*>(m_DataPtr)); -#else - return Memory::Free(const_cast<void*>(m_DataPtr)); +#if ZEN_USE_MIMALLOC + if (m_Flags & kIoBufferAlloc) + { + return mi_free(const_cast<void*>(m_DataPtr)); + } #endif + + return Memory::Free(const_cast<void*>(m_DataPtr)); } ////////////////////////////////////////////////////////////////////////// @@ -85,7 +91,7 @@ IoBufferCore::IoBufferCore(size_t InSize) { ZEN_ASSERT(InSize); - m_DataPtr = AllocateBuffer(InSize, sizeof(void*)); + AllocateBuffer(InSize, sizeof(void*)); m_DataBytes = InSize; SetIsOwnedByThis(true); @@ -95,7 +101,7 @@ IoBufferCore::IoBufferCore(size_t InSize, size_t Alignment) { ZEN_ASSERT(InSize); - m_DataPtr = AllocateBuffer(InSize, Alignment); + AllocateBuffer(InSize, Alignment); m_DataBytes = InSize; SetIsOwnedByThis(true); @@ -138,10 +144,9 @@ IoBufferCore::MakeOwned(bool Immutable) { if (!IsOwned()) { - void* OwnedDataPtr = AllocateBuffer(m_DataBytes, sizeof(void*)); - memcpy(OwnedDataPtr, m_DataPtr, m_DataBytes); - - m_DataPtr = OwnedDataPtr; + const void* OldDataPtr = m_DataPtr; + AllocateBuffer(m_DataBytes, sizeof(void*)); + memcpy(const_cast<void*>(m_DataPtr), OldDataPtr, m_DataBytes); SetIsOwnedByThis(true); } @@ -183,29 +188,29 @@ IoBufferExtendedCore::~IoBufferExtendedCore() { if (m_MappedPointer) { -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS UnmapViewOfFile(m_MappedPointer); -#else +# else uint64_t MapSize = ~uint64_t(uintptr_t(m_MmapHandle)); munmap(m_MappedPointer, MapSize); -#endif +# endif } -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS if (m_Flags & kOwnsMmap) { CloseHandle(m_MmapHandle); } -#endif +# endif if (m_Flags & kOwnsFile) { -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS BOOL Success = CloseHandle(m_FileHandle); -#else +# else int Fd = int(uintptr_t(m_FileHandle)); bool Success = (close(Fd) == 0); -#endif +# endif if (!Success) { @@ -239,7 +244,7 @@ IoBufferExtendedCore::Materialize() const const uint64_t MappedOffsetDisplacement = m_FileOffset - MapOffset; const uint64_t MapSize = m_DataBytes + MappedOffsetDisplacement; -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS m_MmapHandle = CreateFileMapping(m_FileHandle, /* lpFileMappingAttributes */ nullptr, /* flProtect */ PAGE_READONLY, @@ -260,7 +265,7 @@ IoBufferExtendedCore::Materialize() const /* FileOffsetHigh */ uint32_t(MapOffset >> 32), /* FileOffsetLow */ uint32_t(MapOffset & 0xffFFffFFu), /* dwNumberOfBytesToMap */ MapSize); -#else +# else m_MmapHandle = (void*)uintptr_t(~MapSize); // ~ so it's never null (assuming MapSize >= 0) m_Flags |= kOwnsMmap; @@ -271,7 +276,7 @@ IoBufferExtendedCore::Materialize() const /* flags */ MAP_SHARED | MAP_NORESERVE, /* fd */ int(uintptr_t(m_FileHandle)), /* offset */ MapOffset); -#endif // ZEN_PLATFORM_WINDOWS +# endif // ZEN_PLATFORM_WINDOWS if (MappedBase == nullptr) { @@ -376,7 +381,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint { uint64_t FileSize; -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS CAtlFile DataFile; HRESULT hRes = DataFile.Create(FileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING); @@ -387,7 +392,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint } DataFile.GetSize((ULONGLONG&)FileSize); -#else +# else int Fd = open(FileName, O_RDONLY); if (Fd < 0) { @@ -398,7 +403,7 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint struct stat Stat; fstat(Fd, &Stat); FileSize = Stat.st_size; -#endif // ZEN_PLATFORM_WINDOWS +# endif // ZEN_PLATFORM_WINDOWS // TODO: should validate that offset is in range @@ -417,15 +422,15 @@ IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint if (Size) { -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS void* Fd = DataFile.Detach(); -#endif +# endif return IoBuffer(IoBuffer::File, (void*)uintptr_t(Fd), Offset, Size); } -#if !ZEN_PLATFORM_WINDOWS +# if !ZEN_PLATFORM_WINDOWS close(Fd); -#endif +# endif // For an empty file, we may as well just return an empty memory IoBuffer return IoBuffer(IoBuffer::Wrap, "", 0); @@ -437,7 +442,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const path_char_t* FileName) uint64_t FileSize; void* Handle; -#if ZEN_PLATFORM_WINDOWS +# if ZEN_PLATFORM_WINDOWS CAtlFile DataFile; // We need to open with DELETE since this is used for the case @@ -454,8 +459,8 @@ IoBufferBuilder::MakeFromTemporaryFile(const path_char_t* FileName) DataFile.GetSize((ULONGLONG&)FileSize); Handle = DataFile.Detach(); -#else - int Fd = open(FileName, O_RDONLY); +# else + int Fd = open(FileName, O_RDONLY); if (Fd < 0) { return {}; @@ -467,7 +472,7 @@ IoBufferBuilder::MakeFromTemporaryFile(const path_char_t* FileName) FileSize = Stat.st_size; Handle = (void*)uintptr_t(Fd); -#endif // ZEN_PLATFORM_WINDOWS +# endif // ZEN_PLATFORM_WINDOWS IoBuffer Iob(IoBuffer::File, Handle, 0, FileSize); Iob.m_Core->SetIsWholeFile(true); @@ -484,7 +489,7 @@ HashBuffer(IoBuffer& Buffer) ////////////////////////////////////////////////////////////////////////// -#if ZEN_WITH_TESTS +# if ZEN_WITH_TESTS void iobuffer_forcelink() @@ -498,6 +503,6 @@ TEST_CASE("IoBuffer") zen::IoBuffer buffer3(buffer2, 0, buffer2.Size()); } -#endif +# endif } // namespace zen |