From 7d2afedda3318df90d75974e9e040a2547383203 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 1 Aug 2023 18:11:02 -0300 Subject: [PATCH] Add functions to alloc/free aligned memory --- base/memory.cpp | 18 ++++++++++++++++++ base/memory.h | 25 +++++++++++++++++++++++++ base/memory_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 base/memory_tests.cpp diff --git a/base/memory.cpp b/base/memory.cpp index c157c1235..15435ec4f 100644 --- a/base/memory.cpp +++ b/base/memory.cpp @@ -298,3 +298,21 @@ void operator delete[](void* ptr) LAF_NOEXCEPT } #endif + +void* base_aligned_alloc(std::size_t bytes, std::size_t alignment) +{ +#if LAF_WINDOWS + return _aligned_malloc(bytes, alignment); +#else + return aligned_alloc(alignment, bytes); +#endif +} + +void base_aligned_free(void* mem) +{ +#if LAF_WINDOWS + _aligned_free(mem); +#else + free(mem); +#endif +} diff --git a/base/memory.h b/base/memory.h index 0fb71dea0..449121520 100644 --- a/base/memory.h +++ b/base/memory.h @@ -10,12 +10,37 @@ #include +#ifdef __cplusplus + #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ + static constexpr size_t base_alignment = __STDCPP_DEFAULT_NEW_ALIGNMENT__; + #else + static constexpr size_t base_alignment = 1; + #endif + + constexpr size_t base_align_size(const size_t n, + const size_t alignment = base_alignment) { + size_t remaining = (n % alignment); + size_t aligned_n = n + (remaining ? (alignment - remaining): 0); + if (aligned_n > alignment) + return aligned_n; + else + return alignment; + } +#endif + void* base_malloc (std::size_t bytes); void* base_malloc0(std::size_t bytes); void* base_realloc(void* mem, std::size_t bytes); void base_free (void* mem); char* base_strdup (const char* string); +#ifdef __cplusplus + void* base_aligned_alloc(std::size_t bytes, std::size_t alignment = base_alignment); +#else + void* base_aligned_alloc(std::size_t bytes, std::size_t alignment); +#endif +void base_aligned_free(void* mem); + #ifdef LAF_MEMLEAK void base_memleak_init(); void base_memleak_exit(); diff --git a/base/memory_tests.cpp b/base/memory_tests.cpp new file mode 100644 index 000000000..687c09c3e --- /dev/null +++ b/base/memory_tests.cpp @@ -0,0 +1,34 @@ +// LAF Base Library +// Copyright (c) 2023 Igara Studio S.A. +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#include + +#include "base/memory.h" + +TEST(Memory, AlignedAlloc) +{ + void* a = base_aligned_alloc(9, 8); + void* b = base_aligned_alloc(1, 16); + void* c = base_aligned_alloc(16, 16); + void* d = base_aligned_alloc(17, 16); + void* e = base_aligned_alloc(33, 32); + EXPECT_EQ(0, (((size_t)a) % 8)); + EXPECT_EQ(0, (((size_t)b) % 16)); + EXPECT_EQ(0, (((size_t)c) % 16)); + EXPECT_EQ(0, (((size_t)d) % 16)); + EXPECT_EQ(0, (((size_t)e) % 32)); + base_aligned_free(a); + base_aligned_free(b); + base_aligned_free(c); + base_aligned_free(d); + base_aligned_free(e); +} + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}