From e335ca10a6c6a1e37b155e2155f5c5908c0272ae Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 19 Nov 2024 09:59:04 +0100 Subject: memory/string support cleanup and additions (#220) * removed unused memory classes * added align.h alignment helpers used in upcoming changes * added char16_t StringLength * avoid memory alloc in SetCurrentThreadName * added command line parsing helpers to zencore/commandline.h * removed IoBuffer direct VirtualAlloc path --- src/zencore/include/zencore/commandline.h | 39 +++++++++++++++++ src/zencore/include/zencore/iobuffer.h | 1 - src/zencore/include/zencore/memory.h | 41 ------------------ src/zencore/include/zencore/memory/align.h | 67 ++++++++++++++++++++++++++++++ src/zencore/include/zencore/string.h | 6 +++ 5 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 src/zencore/include/zencore/commandline.h create mode 100644 src/zencore/include/zencore/memory/align.h (limited to 'src/zencore/include') diff --git a/src/zencore/include/zencore/commandline.h b/src/zencore/include/zencore/commandline.h new file mode 100644 index 000000000..a4ce6b27d --- /dev/null +++ b/src/zencore/include/zencore/commandline.h @@ -0,0 +1,39 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include + +#include +#include + +namespace zen { + +void IterateCommandlineArgs(std::function& ProcessArg); + +template +void +IterateCommaSeparatedValue(std::string_view OptionArgs, Func&& ProcessArg) +{ + while (OptionArgs.size()) + { + const auto CommaPos = OptionArgs.find_first_of(','); + std::string_view OptionArg; + + if (CommaPos == std::string_view::npos) + { + // No comma or final argument + OptionArg = OptionArgs; + OptionArgs = {}; + } + else + { + OptionArg = OptionArgs.substr(0, CommaPos); + OptionArgs = OptionArgs.substr(CommaPos + 1); + } + + ProcessArg(OptionArg); + } +} + +} // namespace zen diff --git a/src/zencore/include/zencore/iobuffer.h b/src/zencore/include/zencore/iobuffer.h index 15455dbaa..493b7375e 100644 --- a/src/zencore/include/zencore/iobuffer.h +++ b/src/zencore/include/zencore/iobuffer.h @@ -243,7 +243,6 @@ protected: kIsMutable = 1 << 1, kIsExtended = 1 << 2, // Is actually a SharedBufferExtendedCore kIsMaterialized = 1 << 3, // Data pointers are valid - kLowLevelAlloc = 1 << 4, // Using direct memory allocation kIsWholeFile = 1 << 5, // References an entire file kIoBufferAlloc = 1 << 6, // Using IoBuffer allocator kIsOwnedByThis = 1 << 7, diff --git a/src/zencore/include/zencore/memory.h b/src/zencore/include/zencore/memory.h index 7a893d3ab..fdea1a5f1 100644 --- a/src/zencore/include/zencore/memory.h +++ b/src/zencore/include/zencore/memory.h @@ -24,18 +24,6 @@ concept ContiguousRange = true; struct MemoryView; -class MemoryArena -{ -public: - ZENCORE_API MemoryArena(); - ZENCORE_API ~MemoryArena(); - - ZENCORE_API void* Alloc(size_t Size, size_t Alignment); - ZENCORE_API void Free(void* Ptr); - -private: -}; - class Memory { public: @@ -43,35 +31,6 @@ public: ZENCORE_API static void Free(void* Ptr); }; -/** Allocator which claims fixed-size blocks from the underlying allocator. - - There is no way to free individual memory blocks. - - \note This is not thread-safe, you will need to provide synchronization yourself -*/ - -class ChunkingLinearAllocator -{ -public: - explicit ChunkingLinearAllocator(uint64_t ChunkSize, uint64_t ChunkAlignment = sizeof(std::max_align_t)); - ~ChunkingLinearAllocator(); - - ZENCORE_API void Reset(); - - ZENCORE_API void* Alloc(size_t Size, size_t Alignment = sizeof(void*)); - inline void Free(void* Ptr) { ZEN_UNUSED(Ptr); /* no-op */ } - - ChunkingLinearAllocator(const ChunkingLinearAllocator&) = delete; - ChunkingLinearAllocator& operator=(const ChunkingLinearAllocator&) = delete; - -private: - uint8_t* m_ChunkCursor = nullptr; - uint64_t m_ChunkBytesRemain = 0; - const uint64_t m_ChunkSize = 0; - const uint64_t m_ChunkAlignment = 0; - std::vector m_ChunkList; -}; - ////////////////////////////////////////////////////////////////////////// struct MutableMemoryView diff --git a/src/zencore/include/zencore/memory/align.h b/src/zencore/include/zencore/memory/align.h new file mode 100644 index 000000000..acf4157c4 --- /dev/null +++ b/src/zencore/include/zencore/memory/align.h @@ -0,0 +1,67 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include + +namespace zen { + +/** + * Aligns a value to the nearest higher multiple of 'Alignment', which must be a power of two. + * + * @param Val The value to align. + * @param Alignment The alignment value, must be a power of two. + * + * @return The value aligned up to the specified alignment. + */ +template +constexpr T +Align(T Val, uint64_t Alignment) +{ + return (T)(((uint64_t)Val + Alignment - 1) & ~(Alignment - 1)); +} + +/** + * Aligns a value to the nearest lower multiple of 'Alignment', which must be a power of two. + * + * @param Val The value to align. + * @param Alignment The alignment value, must be a power of two. + * + * @return The value aligned down to the specified alignment. + */ +template +constexpr T +AlignDown(T Val, uint64_t Alignment) +{ + return (T)(((uint64_t)Val) & ~(Alignment - 1)); +} + +/** + * Checks if a pointer is aligned to the specified alignment. + * + * @param Val The value to align. + * @param Alignment The alignment value, must be a power of two. + * + * @return true if the pointer is aligned to the specified alignment, false otherwise. + */ +template +constexpr bool +IsAligned(T Val, uint64_t Alignment) +{ + return !((uint64_t)Val & (Alignment - 1)); +} + +/** + * Aligns a value to the nearest higher multiple of 'Alignment'. + * + * @param Val The value to align. + * @param Alignment The alignment value, can be any arbitrary value. + * + * @return The value aligned up to the specified alignment. + */ +template +constexpr T +AlignArbitrary(T Val, uint64_t Alignment) +{ + return (T)((((uint64_t)Val + Alignment - 1) / Alignment) * Alignment); +} + +} // namespace zen diff --git a/src/zencore/include/zencore/string.h b/src/zencore/include/zencore/string.h index b0232d883..b10b6a2ba 100644 --- a/src/zencore/include/zencore/string.h +++ b/src/zencore/include/zencore/string.h @@ -51,6 +51,12 @@ StringLength(const wchar_t* str) return wcslen(str); } +inline size_t +StringLength(const char16_t* str) +{ + return std::char_traits::length(str); +} + ////////////////////////////////////////////////////////////////////////// // File name helpers // -- cgit v1.2.3