aboutsummaryrefslogtreecommitdiff
path: root/zencore/iobuffer.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2022-05-20 12:42:56 +0200
committerStefan Boberg <[email protected]>2022-05-20 12:42:56 +0200
commit5b271be0169b842cdc3d576e48bf0ddc2f122852 (patch)
tree16f501d2190f19a7281ce3f30365817464e146cb /zencore/iobuffer.cpp
parentAdded ZEN_USE_CATCH2 define (diff)
parentfix mac compilation error (diff)
downloadzen-5b271be0169b842cdc3d576e48bf0ddc2f122852.tar.xz
zen-5b271be0169b842cdc3d576e48bf0ddc2f122852.zip
Merge branch 'main' into use-catch2
Diffstat (limited to 'zencore/iobuffer.cpp')
-rw-r--r--zencore/iobuffer.cpp45
1 files changed, 43 insertions, 2 deletions
diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp
index 8a3ab8427..c4b7f7bdf 100644
--- a/zencore/iobuffer.cpp
+++ b/zencore/iobuffer.cpp
@@ -211,6 +211,18 @@ IoBufferExtendedCore::~IoBufferExtendedCore()
if (LocalFlags & kOwnsFile)
{
+ if (m_DeleteOnClose)
+ {
+#if ZEN_PLATFORM_WINDOWS
+ // Mark file for deletion when final handle is closed
+ FILE_DISPOSITION_INFO Fdi{.DeleteFile = TRUE};
+
+ SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi);
+#else
+ std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle);
+ unlink(FilePath.c_str());
+#endif
+ }
#if ZEN_PLATFORM_WINDOWS
BOOL Success = CloseHandle(m_FileHandle);
#else
@@ -226,7 +238,18 @@ IoBufferExtendedCore::~IoBufferExtendedCore()
m_DataPtr = nullptr;
}
-static RwLock g_MappingLock;
+static constexpr size_t MappingLockCount = 64;
+static_assert(IsPow2(MappingLockCount), "MappingLockCount must be power of two");
+
+static RwLock g_MappingLocks[MappingLockCount];
+
+static RwLock&
+MappingLockForInstance(const IoBufferExtendedCore* instance)
+{
+ intptr_t base = (intptr_t)instance;
+ size_t lock_index = ((base >> 8) ^ (base >> 16)) & (MappingLockCount - 1u);
+ return g_MappingLocks[lock_index];
+}
void
IoBufferExtendedCore::Materialize() const
@@ -237,7 +260,7 @@ IoBufferExtendedCore::Materialize() const
if (m_Flags.load(std::memory_order_acquire) & kIsMaterialized)
return;
- RwLock::ExclusiveLockScope _(g_MappingLock);
+ RwLock::ExclusiveLockScope _(MappingLockForInstance(this));
// Someone could have gotten here first
// We can use memory_order_relaxed on this load because the mutex has already provided the fence
@@ -287,6 +310,9 @@ IoBufferExtendedCore::Materialize() const
if (MappedBase == nullptr)
{
+#if ZEN_PLATFORM_WINDOWS
+ CloseHandle(NewMmapHandle);
+#endif // ZEN_PLATFORM_WINDOWS
throw std::system_error(std::error_code(zen::GetLastError(), std::system_category()),
fmt::format("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}'",
MapOffset,
@@ -316,6 +342,12 @@ IoBufferExtendedCore::GetFileReference(IoBufferFileReference& OutRef) const
return true;
}
+void
+IoBufferExtendedCore::MarkAsDeleteOnClose()
+{
+ m_DeleteOnClose = true;
+}
+
//////////////////////////////////////////////////////////////////////////
IoBuffer::IoBuffer(size_t InSize) : m_Core(new IoBufferCore(InSize))
@@ -378,6 +410,15 @@ IoBuffer::GetFileReference(IoBufferFileReference& OutRef) const
return false;
}
+void
+IoBuffer::MarkAsDeleteOnClose()
+{
+ if (IoBufferExtendedCore* ExtCore = m_Core->ExtendedCore())
+ {
+ ExtCore->MarkAsDeleteOnClose();
+ }
+}
+
//////////////////////////////////////////////////////////////////////////
IoBuffer