diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 65fe607..9cf21df 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Create Build Environment run: cmake -E make_directory build diff --git a/.github/workflows/feature_ci.yml b/.github/workflows/feature_ci.yml index c1041cb..6c6a2ab 100644 --- a/.github/workflows/feature_ci.yml +++ b/.github/workflows/feature_ci.yml @@ -2,7 +2,7 @@ name: Feature CI on: push: - branches-ignore: ['main'] + branches-ignore: ['main', 'fix/*'] pull_request: jobs: @@ -10,54 +10,48 @@ jobs: strategy: fail-fast: false matrix: - image: - # List: https://github.com/conan-io/conan-docker-tools - - gcc10 - - gcc5 - - clang10 - - clang40 - sharedlibs: [OFF, ON] + image: ["gcc:7", "gcc:14", "clang:6", "clang:18"] runs-on: ubuntu-latest container: - image: conanio/${{matrix.image}} - options: --user root + image: ghcr.io/foonathan/${{matrix.image}} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Create Build Environment run: cmake -E make_directory build - name: Configure working-directory: build/ - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake -GNinja $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=Debug - name: Build working-directory: build/ - run: cmake --build . --config Debug + run: cmake --build . - name: Test working-directory: build/ - run: ctest -C Debug --output-on-failure + run: ctest --output-on-failure macos: strategy: fail-fast: false matrix: - xcode: ['11', '13'] - sharedlibs: [OFF, ON] + xcode: ['15', '16'] - runs-on: macos-11 + runs-on: macos-14 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{matrix.xcode}} - name: Create Build Environment run: cmake -E make_directory build + - name: Install ninja + run: brew install ninja - name: Configure working-directory: build/ - run: cmake $GITHUB_WORKSPACE -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake -GNinja $GITHUB_WORKSPACE - name: Build working-directory: build/ run: cmake --build . @@ -69,21 +63,22 @@ jobs: strategy: fail-fast: false matrix: - sharedlibs: [OFF, ON] + arch: [Win32, x64] - runs-on: windows-2019 + runs-on: windows-2022 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Create Build Environment run: cmake -E make_directory build + - name: Configure shell: bash working-directory: build/ - run: cmake $GITHUB_WORKSPACE -G"Visual Studio 16 2019" -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake $GITHUB_WORKSPACE -G"Visual Studio 17 2022" -A ${{matrix.arch}} - name: Build working-directory: build/ - run: cmake --build . --config Debug + run: cmake --build . --config Debug -j 2 - name: Test working-directory: build/ run: ctest -C Debug --output-on-failure diff --git a/.github/workflows/main_ci.yml b/.github/workflows/main_ci.yml index 65c7be5..f0cbc24 100644 --- a/.github/workflows/main_ci.yml +++ b/.github/workflows/main_ci.yml @@ -2,7 +2,7 @@ name: Main CI on: push: - branches: [main] + branches: [main, fix/*] jobs: linux: @@ -10,64 +10,70 @@ jobs: fail-fast: false matrix: image: - # List: https://github.com/conan-io/conan-docker-tools - - gcc10 - - gcc9 - - gcc8 - - gcc7 - - gcc6 - - gcc5 - - clang10 - - clang9 - - clang8 - - clang7 - - clang60 - - clang50 - - clang40 + - "gcc:7" + - "gcc:8" + - "gcc:9" + - "gcc:10" + - "gcc:11" + - "gcc:12" + - "gcc:13" + - "gcc:14" + - "clang:6" + - "clang:7" + - "clang:8" + - "clang:9" + - "clang:10" + - "clang:11" + - "clang:12" + - "clang:13" + - "clang:14" + - "clang:15" + - "clang:16" + - "clang:17" + - "clang:18" build_type: [Debug, Release] - sharedlibs: [OFF, ON] runs-on: ubuntu-latest container: - image: conanio/${{matrix.image}} - options: --user root + image: ghcr.io/foonathan/${{matrix.image}} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Create Build Environment run: cmake -E make_directory build + - name: Configure working-directory: build/ - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake -GNinja $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.build_type}} - name: Build working-directory: build/ - run: cmake --build . --config ${{matrix.build_type}} + run: cmake --build . - name: Test working-directory: build/ - run: ctest -C ${{matrix.build_type}} --output-on-failure + run: ctest --output-on-failure macos: strategy: fail-fast: false matrix: - xcode: - - '11' - - '12' - - '13' - sharedlibs: [OFF, ON] + xcode: ['15', '16'] + build_type: [Debug, Release] - runs-on: macos-11 + runs-on: macos-14 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{matrix.xcode}} - name: Create Build Environment run: cmake -E make_directory build + - name: Install ninja + run: brew install ninja + - name: Configure working-directory: build/ - run: cmake $GITHUB_WORKSPACE -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake -GNinja $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.build_type}} - name: Build working-directory: build/ run: cmake --build . @@ -79,23 +85,24 @@ jobs: strategy: fail-fast: false matrix: + toolset: ['v142', 'v143', 'ClangCL'] build_type: [Debug, Release] - sharedlibs: [OFF, ON] - std: [14, 17, 20] + arch: [Win32, x64] runs-on: windows-2022 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Create Build Environment run: cmake -E make_directory build + - name: Configure shell: bash working-directory: build/ - run: cmake $GITHUB_WORKSPACE -G"Visual Studio 17 2022" -DCMAKE_CXX_STANDARD=${{matrix.std}} -DBUILD_SHARED_LIBS=${{matrix.sharedlibs}} + run: cmake $GITHUB_WORKSPACE -G"Visual Studio 17 2022" -T ${{matrix.toolset}} -A ${{matrix.arch}} - name: Build working-directory: build/ - run: cmake --build . --config ${{matrix.build_type}} + run: cmake --build . --config ${{matrix.build_type}} -j 2 - name: Test working-directory: build/ run: ctest -C ${{matrix.build_type}} --output-on-failure diff --git a/CHANGELOG.md b/CHANGELOG.md index aead972..604900a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,29 @@ # Upcoming Changes +# 0.7-4 + +CMake Improvements: + +* Fix deprecation warnings, update doctest, and CI. (#188) +* Remove static linking for cross compilation. (#162, #164) +* Fix include paths, cache variables. (#165, #190) +* Assume `thread_local` is supported. (#167) + +Bugfixes: + +* Support `fixed_block_allocator` in `memory_pool_collection`. (#173) +* Fix `virtual_block_allocator` deallocation. (#175) +* Fix `std::shared_ptr` node size alignment mismatch. (#171) +* Fix unit tests on android. (#176) +* Don't use undefined preprocessor macros. (#186) +* Fix unused variable warnings. (#183, #184, #189) + +Improvements: + +* Allow incomplete types in `allocator_deallocator`. (#153) +* Give `allocator_polymorphic_deallocator/deleter` a default constructor. (#155) +* Expose `memory_pool::owns`. (#174) + # 0.7-3 CMake improvements: diff --git a/CMakeLists.txt b/CMakeLists.txt index ddbc459..89e3e90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,19 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # root CMakeLists.txt, specifies option and interface library cmake_minimum_required(VERSION 3.14) +if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") + cmake_policy(SET CMP0135 NEW) +endif() + project(FOONATHAN_MEMORY) set(FOONATHAN_MEMORY_VERSION_MAJOR 0 CACHE STRING "major version of memory" FORCE) set(FOONATHAN_MEMORY_VERSION_MINOR 7 CACHE STRING "minor version of memory" FORCE) -set(FOONATHAN_MEMORY_VERSION_PATCH 3 CACHE STRING "patch version of memory" FORCE) -set(FOONATHAN_MEMORY_VERSION "${FOONATHAN_MEMORY_VERSION_MAJOR}.${FOONATHAN_MEMORY_VERSION_MINOR}.${FOONATHAN_MEMORY_VERSION_PATCH}" +set(FOONATHAN_MEMORY_VERSION_PATCH 4 CACHE STRING "patch version of memory" FORCE) +set(FOONATHAN_MEMORY_VERSION "$CACHE{FOONATHAN_MEMORY_VERSION_MAJOR}.$CACHE{FOONATHAN_MEMORY_VERSION_MINOR}.$CACHE{FOONATHAN_MEMORY_VERSION_PATCH}" CACHE STRING "version of memory" FORCE) @@ -20,7 +24,7 @@ set(CMAKE_DEBUG_POSTFIX "-dbg") if(UNIX OR VXWORKS) include(GNUInstallDirs) - set(FOONATHAN_MEMORY_INC_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/foonathan_memory") + set(FOONATHAN_MEMORY_INC_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") set(FOONATHAN_MEMORY_RUNTIME_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}") set(FOONATHAN_MEMORY_LIBRARY_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}") set(FOONATHAN_MEMORY_ARCHIVE_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}") @@ -29,7 +33,7 @@ if(UNIX OR VXWORKS) set(FOONATHAN_MEMORY_CMAKE_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/foonathan_memory/cmake") set(FOONATHAN_MEMORY_ADDITIONAL_FILES_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/foonathan_memory") elseif(WIN32) - set(FOONATHAN_MEMORY_INC_INSTALL_DIR "include/foonathan_memory") + set(FOONATHAN_MEMORY_INC_INSTALL_DIR "include") set(FOONATHAN_MEMORY_RUNTIME_INSTALL_DIR "bin") set(FOONATHAN_MEMORY_LIBRARY_INSTALL_DIR "bin") set(FOONATHAN_MEMORY_ARCHIVE_INSTALL_DIR "lib") diff --git a/README.md b/README.md index ea44f14..2d11968 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ int main() // list_node_size::value is the size of each node of a std::list memory::memory_pool<> pool(memory::list_node_size::value, 4_KiB); - // just an alias for std::list> + // just an alias for std::list>> // a std::list using a memory_pool // std_allocator stores a reference to a RawAllocator and provides the Allocator interface memory::list> list(pool); @@ -238,11 +238,8 @@ And big thanks to the contributors as well: * @j-carl * @kaidokert * @maksqwe -* @maksqwe -* @moazzamak * @moazzamak * @myd7349 -* @myd7349 * @nicolastagliani * @quattrinili * @razr diff --git a/cmake/configuration.cmake b/cmake/configuration.cmake index 59f1d67..1145b44 100644 --- a/cmake/configuration.cmake +++ b/cmake/configuration.cmake @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # defines configuration options diff --git a/cmake/get_container_node_sizes.cmake b/cmake/get_container_node_sizes.cmake index 90f220a..b314e5c 100644 --- a/cmake/get_container_node_sizes.cmake +++ b/cmake/get_container_node_sizes.cmake @@ -181,7 +181,7 @@ namespace detail template struct ${container}_node_size : std::integral_constant::value + sizeof(T)> + detail::round_up_to_multiple_of_alignment(detail::${container}_node_size::value + sizeof(T), alignof(void*))> {}; ") endforeach() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e1442b3..47ab345 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # builds examples diff --git a/example/allocator_storage.cpp b/example/allocator_storage.cpp index fafde52..0337566 100644 --- a/example/allocator_storage.cpp +++ b/example/allocator_storage.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // this example shows how to store allocators by reference and type-erased diff --git a/example/joint_allocation.cpp b/example/joint_allocation.cpp index b78f7bb..70f712b 100644 --- a/example/joint_allocation.cpp +++ b/example/joint_allocation.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // this examples shows how to use the joint memory facilities diff --git a/example/taking_allocators.cpp b/example/taking_allocators.cpp index 542d5ce..439c8d3 100644 --- a/example/taking_allocators.cpp +++ b/example/taking_allocators.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // this example provides two implementation of a deep_copy_ptr that performs a copy when copying the pointer diff --git a/example/tracking.cpp b/example/tracking.cpp index c8e7814..8bc54b6 100644 --- a/example/tracking.cpp +++ b/example/tracking.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // this example shows how to track allocations diff --git a/example/using_allocators.cpp b/example/using_allocators.cpp index 5ac5760..5e70aab 100644 --- a/example/using_allocators.cpp +++ b/example/using_allocators.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // this examples shows the basic usage of RawAllocator classes with containers and smart pointers diff --git a/include/foonathan/memory/aligned_allocator.hpp b/include/foonathan/memory/aligned_allocator.hpp index fe724aa..69c79af 100644 --- a/include/foonathan/memory/aligned_allocator.hpp +++ b/include/foonathan/memory/aligned_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_ALIGNED_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/allocator_storage.hpp b/include/foonathan/memory/allocator_storage.hpp index 57f128a..0fe37fa 100644 --- a/include/foonathan/memory/allocator_storage.hpp +++ b/include/foonathan/memory/allocator_storage.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_ALLOCATOR_STORAGE_HPP_INCLUDED @@ -156,7 +156,7 @@ namespace foonathan allocator_storage& operator=(allocator_storage&& other) noexcept { - storage_policy:: operator=(detail::move(other)); + storage_policy::operator=(detail::move(other)); detail::mutex_storage>::operator=(detail::move(other)); return *this; @@ -803,9 +803,9 @@ namespace foonathan using traits = allocator_traits; using composable = is_composable_allocator; using storage = detail::reference_storage_impl< - typename allocator_traits::allocator_type, - decltype(detail::reference_type(typename allocator_traits< - RawAllocator>::is_stateful{}, + typename allocator_traits::allocator_type, + decltype(detail::reference_type(typename allocator_traits< + RawAllocator>::is_stateful{}, is_shared_allocator{}))>; public: diff --git a/include/foonathan/memory/allocator_traits.hpp b/include/foonathan/memory/allocator_traits.hpp index 78f5ccf..3cdda93 100644 --- a/include/foonathan/memory/allocator_traits.hpp +++ b/include/foonathan/memory/allocator_traits.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_ALLOCATOR_TRAITS_HPP_INCLUDED diff --git a/include/foonathan/memory/config.hpp b/include/foonathan/memory/config.hpp index 233caf6..1754756 100644 --- a/include/foonathan/memory/config.hpp +++ b/include/foonathan/memory/config.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib /// \file @@ -35,7 +35,7 @@ // hosted implementation #ifndef FOONATHAN_HOSTED_IMPLEMENTATION -#if !_MSC_VER && !__STDC_HOSTED__ +#if !defined(_MSC_VER) && !defined(__STDC_HOSTED__) #define FOONATHAN_HOSTED_IMPLEMENTATION 0 #else #define FOONATHAN_HOSTED_IMPLEMENTATION 1 diff --git a/include/foonathan/memory/container.hpp b/include/foonathan/memory/container.hpp index 5c77ddb..d5e3112 100644 --- a/include/foonathan/memory/container.hpp +++ b/include/foonathan/memory/container.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_CONTAINER_HPP_INCLUDED diff --git a/include/foonathan/memory/debugging.hpp b/include/foonathan/memory/debugging.hpp index bb9af98..b04166b 100644 --- a/include/foonathan/memory/debugging.hpp +++ b/include/foonathan/memory/debugging.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DEBUGGING_HPP_INCLUDED diff --git a/include/foonathan/memory/default_allocator.hpp b/include/foonathan/memory/default_allocator.hpp index 29a2139..f09f0e1 100644 --- a/include/foonathan/memory/default_allocator.hpp +++ b/include/foonathan/memory/default_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DEFAULT_ALLOCATOR_HPP_INCLUDED @@ -25,7 +25,7 @@ namespace foonathan /// Arena allocators like \ref memory_stack or \ref memory_pool allocate memory by subdividing a huge block. /// They get a \concept{concept_blockallocator,BlockAllocator} that will be used for their internal allocation, /// this type is the default value. - /// \requiredbe Its type can be changed via the CMake option \c FOONATHAN_MEMORY_DEFAULT_ALLCOATOR, + /// \requiredbe Its type can be changed via the CMake option \c FOONATHAN_MEMORY_DEFAULT_ALLOCATOR, /// but it must be one of the following: \ref heap_allocator, \ref new_allocator, \ref malloc_allocator, \ref static_allocator, \ref virtual_memory_allocator. /// \defaultbe The default is \ref heap_allocator. /// \ingroup allocator diff --git a/include/foonathan/memory/deleter.hpp b/include/foonathan/memory/deleter.hpp index 6c9cf5f..64e1f97 100644 --- a/include/foonathan/memory/deleter.hpp +++ b/include/foonathan/memory/deleter.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DELETER_HPP_INCLUDED @@ -24,9 +24,6 @@ namespace foonathan template class allocator_deallocator : FOONATHAN_EBO(allocator_reference) { - static_assert(!std::is_abstract::value, - "use allocator_polymorphic_deallocator for storing base classes"); - public: using allocator_type = typename allocator_reference::allocator_type; using value_type = Type; @@ -48,6 +45,8 @@ namespace foonathan /// \requires The deallocator must not have been created by the default constructor. void operator()(value_type* pointer) noexcept { + static_assert(!std::is_abstract::value, + "use allocator_polymorphic_deallocator for storing base classes"); this->deallocate_node(pointer, sizeof(value_type), alignof(value_type)); } @@ -68,8 +67,6 @@ namespace foonathan class allocator_deallocator : FOONATHAN_EBO(allocator_reference) { - static_assert(!std::is_abstract::value, "must not create polymorphic arrays"); - public: using allocator_type = typename allocator_reference::allocator_type; using value_type = Type; @@ -93,6 +90,8 @@ namespace foonathan /// \requires The deallocator must not have been created by the default constructor. void operator()(value_type* pointer) noexcept { + static_assert(!std::is_abstract::value, + "must not create polymorphic arrays"); this->deallocate_array(pointer, size_, sizeof(value_type), alignof(value_type)); } @@ -127,6 +126,11 @@ namespace foonathan using allocator_type = typename allocator_reference::allocator_type; using value_type = BaseType; + /// \effects Creates it without any associated allocator. + /// The deallocator must not be used if that is the case. + /// \notes This functions is useful if you have want to create an empty smart pointer without giving it an allocator. + allocator_polymorphic_deallocator() noexcept = default; + /// \effects Creates it from a deallocator for a derived type. /// It will deallocate the memory as if done by the derived type. template ::value))> @@ -154,7 +158,7 @@ namespace foonathan } private: - std::size_t derived_size_, derived_alignment_; + std::size_t derived_size_ = 0, derived_alignment_ = 0; }; /// Similar to \ref allocator_deallocator but calls the destructors of the object. @@ -163,9 +167,6 @@ namespace foonathan template class allocator_deleter : FOONATHAN_EBO(allocator_reference) { - static_assert(!std::is_abstract::value, - "use allocator_polymorphic_deleter for storing base classes"); - public: using allocator_type = typename allocator_reference::allocator_type; using value_type = Type; @@ -188,6 +189,8 @@ namespace foonathan /// \requires The deleter must not have been created by the default constructor. void operator()(value_type* pointer) noexcept { + static_assert(!std::is_abstract::value, + "use allocator_polymorphic_deleter for storing base classes"); pointer->~value_type(); this->deallocate_node(pointer, sizeof(value_type), alignof(value_type)); } @@ -208,8 +211,6 @@ namespace foonathan class allocator_deleter : FOONATHAN_EBO(allocator_reference) { - static_assert(!std::is_abstract::value, "must not create polymorphic arrays"); - public: using allocator_type = typename allocator_reference::allocator_type; using value_type = Type; @@ -232,6 +233,8 @@ namespace foonathan /// \requires The deleter must not have been created by the default constructor. void operator()(value_type* pointer) noexcept { + static_assert(!std::is_abstract::value, + "must not create polymorphic arrays"); for (auto cur = pointer; cur != pointer + size_; ++cur) cur->~value_type(); this->deallocate_array(pointer, size_, sizeof(value_type), alignof(value_type)); @@ -268,6 +271,11 @@ namespace foonathan using allocator_type = typename allocator_reference::allocator_type; using value_type = BaseType; + /// \effects Creates it without any associated allocator. + /// The deleter must not be used if that is the case. + /// \notes This functions is useful if you have want to create an empty smart pointer without giving it an allocator. + allocator_polymorphic_deleter() noexcept = default; + /// \effects Creates it from a deleter for a derived type. /// It will deallocate the memory as if done by the derived type. template ::value))> @@ -298,10 +306,11 @@ namespace foonathan } private: - unsigned short derived_size_, - derived_alignment_; // use unsigned short here to save space + unsigned short derived_size_ = 0, + derived_alignment_ = 0; // use unsigned short here to save space }; } // namespace memory } // namespace foonathan -#endif //FOONATHAN_MEMORY_DELETER_HPP_INCLUDED +#endif // FOONATHAN_MEMORY_DELETER_HPP_INCLUDED + diff --git a/include/foonathan/memory/detail/align.hpp b/include/foonathan/memory/detail/align.hpp index a9c0fd5..8fcf426 100644 --- a/include/foonathan/memory/detail/align.hpp +++ b/include/foonathan/memory/detail/align.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_ALIGN_HPP_INCLUDED @@ -21,6 +21,13 @@ namespace foonathan return alignment && (alignment & (alignment - 1)) == 0u; } + constexpr std::size_t round_up_to_multiple_of_alignment(std::size_t size, + std::size_t alignment) noexcept + { + FOONATHAN_MEMORY_ASSERT(is_valid_alignment(alignment)); + return (size + alignment - 1) & ~(alignment - 1); + } + // returns the offset needed to align ptr for given alignment // alignment must be valid inline std::size_t align_offset(std::uintptr_t address, std::size_t alignment) noexcept @@ -45,7 +52,7 @@ namespace foonathan // returns the minimum alignment required for a node of given size std::size_t alignment_for(std::size_t size) noexcept; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_ALIGN_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/assert.hpp b/include/foonathan/memory/detail/assert.hpp index dddbfbe..7317503 100644 --- a/include/foonathan/memory/detail/assert.hpp +++ b/include/foonathan/memory/detail/assert.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_ASSERT_HPP_INCLUDED @@ -49,7 +49,7 @@ namespace foonathan #define FOONATHAN_MEMORY_WARNING(Msg) #endif } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_ASSERT_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/container_node_sizes.hpp b/include/foonathan/memory/detail/container_node_sizes.hpp index f2eef03..d561a46 100644 --- a/include/foonathan/memory/detail/container_node_sizes.hpp +++ b/include/foonathan/memory/detail/container_node_sizes.hpp @@ -1,9 +1,10 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_CONTAINER_NODE_SIZES_HPP_INCLUDED #define FOONATHAN_MEMORY_DETAIL_CONTAINER_NODE_SIZES_HPP_INCLUDED +#include "align.hpp" #include "container_node_sizes_impl.hpp" #endif //FOONATHAN_MEMORY_DETAIL_CONTAINER_NODE_SIZES_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/debug_helpers.hpp b/include/foonathan/memory/detail/debug_helpers.hpp index 3bdadb3..83e9e68 100644 --- a/include/foonathan/memory/detail/debug_helpers.hpp +++ b/include/foonathan/memory/detail/debug_helpers.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DEBUG_HELPERS_HPP_INCLUDED @@ -228,7 +228,7 @@ namespace foonathan using default_leak_checker = no_leak_checker; #endif } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DEBUG_HELPERS_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/ebo_storage.hpp b/include/foonathan/memory/detail/ebo_storage.hpp index 35ccf1e..79c4316 100644 --- a/include/foonathan/memory/detail/ebo_storage.hpp +++ b/include/foonathan/memory/detail/ebo_storage.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_EBO_STORAGE_HPP_INCLUDED @@ -35,7 +35,7 @@ namespace foonathan } }; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_EBO_STORAGE_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/free_list.hpp b/include/foonathan/memory/detail/free_list.hpp index cd646e1..81e29ac 100644 --- a/include/foonathan/memory/detail/free_list.hpp +++ b/include/foonathan/memory/detail/free_list.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAILL_FREE_LIST_HPP_INCLUDED @@ -221,7 +221,7 @@ namespace foonathan using array_free_memory_list = ordered_free_memory_list; #endif } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAILL_FREE_LIST_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/free_list_array.hpp b/include/foonathan/memory/detail/free_list_array.hpp index c18b46b..322015e 100644 --- a/include/foonathan/memory/detail/free_list_array.hpp +++ b/include/foonathan/memory/detail/free_list_array.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_FREE_LIST_ARRAY_HPP @@ -119,7 +119,7 @@ namespace foonathan static std::size_t size_from_index(std::size_t index) noexcept; }; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif //FOONATHAN_MEMORY_DETAIL_FREE_LIST_ARRAY_HPP diff --git a/include/foonathan/memory/detail/ilog2.hpp b/include/foonathan/memory/detail/ilog2.hpp index 0d98e46..88f117e 100644 --- a/include/foonathan/memory/detail/ilog2.hpp +++ b/include/foonathan/memory/detail/ilog2.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_ILOG2_HPP_INCLUDED @@ -62,7 +62,7 @@ namespace foonathan return ilog2_base(x) - std::size_t(is_power_of_two(x)); } } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif diff --git a/include/foonathan/memory/detail/lowlevel_allocator.hpp b/include/foonathan/memory/detail/lowlevel_allocator.hpp index 5836219..7e339b2 100644 --- a/include/foonathan/memory/detail/lowlevel_allocator.hpp +++ b/include/foonathan/memory/detail/lowlevel_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_LOWLEVEL_ALLOCATOR_HPP_INCLUDED @@ -79,7 +79,7 @@ namespace foonathan #define FOONATHAN_MEMORY_LL_ALLOCATOR_LEAK_CHECKER(functor, var_name) \ FOONATHAN_MEMORY_GLOBAL_LEAK_CHECKER(lowlevel_allocator_leak_handler, var_name) } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_LOWLEVEL_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/memory_stack.hpp b/include/foonathan/memory/detail/memory_stack.hpp index 7fbe447..1cf22db 100644 --- a/include/foonathan/memory/detail/memory_stack.hpp +++ b/include/foonathan/memory/detail/memory_stack.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_MEMORY_STACK_HPP_INCLUDED @@ -113,7 +113,7 @@ namespace foonathan char* cur_; }; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_MEMORY_STACK_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/small_free_list.hpp b/include/foonathan/memory/detail/small_free_list.hpp index 60bde31..c2c1cd2 100644 --- a/include/foonathan/memory/detail/small_free_list.hpp +++ b/include/foonathan/memory/detail/small_free_list.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_SMALL_FREE_LIST_HPP_INCLUDED @@ -157,7 +157,7 @@ namespace foonathan // for some reason, this is required in order to define it void swap(small_free_memory_list& a, small_free_memory_list& b) noexcept; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_DETAIL_SMALL_FREE_LIST_HPP_INCLUDED diff --git a/include/foonathan/memory/detail/utility.hpp b/include/foonathan/memory/detail/utility.hpp index f76dd3c..188e9d8 100644 --- a/include/foonathan/memory/detail/utility.hpp +++ b/include/foonathan/memory/detail/utility.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_DETAIL_UTILITY_HPP @@ -111,7 +111,7 @@ namespace foonathan { }; } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif //FOONATHAN_MEMORY_DETAIL_UTILITY_HPP diff --git a/include/foonathan/memory/error.hpp b/include/foonathan/memory/error.hpp index d3152dc..90e4736 100644 --- a/include/foonathan/memory/error.hpp +++ b/include/foonathan/memory/error.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib /// \file @@ -278,11 +278,10 @@ namespace foonathan void check_allocation_size(std::size_t passed, std::size_t supported, const allocator_info& info) { - check_allocation_size( - passed, [&] { return supported; }, info); + check_allocation_size(passed, [&] { return supported; }, info); } } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_ERROR_HPP_INCLUDED diff --git a/include/foonathan/memory/fallback_allocator.hpp b/include/foonathan/memory/fallback_allocator.hpp index 7ae86ea..9a94040 100644 --- a/include/foonathan/memory/fallback_allocator.hpp +++ b/include/foonathan/memory/fallback_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_FALLBACK_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/heap_allocator.hpp b/include/foonathan/memory/heap_allocator.hpp index 87532c4..ef4fd1f 100644 --- a/include/foonathan/memory/heap_allocator.hpp +++ b/include/foonathan/memory/heap_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_HEAP_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/iteration_allocator.hpp b/include/foonathan/memory/iteration_allocator.hpp index 37cb4cc..d38cf20 100644 --- a/include/foonathan/memory/iteration_allocator.hpp +++ b/include/foonathan/memory/iteration_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_ITERATION_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/joint_allocator.hpp b/include/foonathan/memory/joint_allocator.hpp index 377bc81..18333c5 100644 --- a/include/foonathan/memory/joint_allocator.hpp +++ b/include/foonathan/memory/joint_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_JOINT_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/malloc_allocator.hpp b/include/foonathan/memory/malloc_allocator.hpp index ef8a1b0..e1a084b 100644 --- a/include/foonathan/memory/malloc_allocator.hpp +++ b/include/foonathan/memory/malloc_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MALLOC_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/memory_arena.hpp b/include/foonathan/memory/memory_arena.hpp index 4c277d8..30ddd68 100644 --- a/include/foonathan/memory/memory_arena.hpp +++ b/include/foonathan/memory/memory_arena.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_ARENA_HPP_INCLUDED @@ -686,7 +686,7 @@ namespace foonathan return std::size_t(value * 1000 * 1000 * 1000); } } // namespace literals - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_MEMORY_ARENA_HPP_INCLUDED diff --git a/include/foonathan/memory/memory_pool.hpp b/include/foonathan/memory/memory_pool.hpp index d764365..0410788 100644 --- a/include/foonathan/memory/memory_pool.hpp +++ b/include/foonathan/memory/memory_pool.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_POOL_HPP_INCLUDED @@ -222,6 +222,12 @@ namespace foonathan return arena_.get_allocator(); } + /// \returns If `ptr` is in memory owned by the underlying arena. + bool owns(const void* ptr) const noexcept + { + return arena_.owns(ptr); + } + private: allocator_info info() const noexcept { @@ -296,8 +302,8 @@ namespace foonathan { detail::check_allocation_size(size, max_node_size(state), state.info()); - detail::check_allocation_size( - alignment, [&] { return max_alignment(state); }, state.info()); + detail::check_allocation_size< + bad_alignment>(alignment, [&] { return max_alignment(state); }, state.info()); auto mem = state.allocate_node(); state.on_allocate(size); return mem; @@ -314,8 +320,8 @@ namespace foonathan { detail::check_allocation_size(size, max_node_size(state), state.info()); - detail::check_allocation_size( - alignment, [&] { return max_alignment(state); }, state.info()); + detail::check_allocation_size< + bad_alignment>(alignment, [&] { return max_alignment(state); }, state.info()); detail::check_allocation_size(count * size, max_array_size(state), state.info()); auto mem = state.allocate_array(count, size); diff --git a/include/foonathan/memory/memory_pool_collection.hpp b/include/foonathan/memory/memory_pool_collection.hpp index e6952e9..be7b7e5 100644 --- a/include/foonathan/memory/memory_pool_collection.hpp +++ b/include/foonathan/memory/memory_pool_collection.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_POOL_COLLECTION_HPP_INCLUDED @@ -125,8 +125,8 @@ namespace foonathan /// \throws Anything thrown by the \concept{concept_blockallocator,BlockAllocator} if a growth is needed or a \ref bad_node_size exception if the node size is too big. void* allocate_node(std::size_t node_size) { - detail::check_allocation_size( - node_size, [&] { return max_node_size(); }, info()); + detail::check_allocation_size< + bad_node_size>(node_size, [&] { return max_node_size(); }, info()); auto& pool = pools_.get(node_size); if (pool.empty()) { @@ -169,8 +169,8 @@ namespace foonathan /// \c node_size must be valid \concept{concept_node,node size}. void* allocate_array(std::size_t count, std::size_t node_size) { - detail::check_allocation_size( - node_size, [&] { return max_node_size(); }, info()); + detail::check_allocation_size< + bad_node_size>(node_size, [&] { return max_node_size(); }, info()); auto& pool = pools_.get(node_size); @@ -324,7 +324,7 @@ namespace foonathan std::size_t def_capacity() const noexcept { - return arena_.next_block_size() / pools_.size(); + return arena_.current_block().size / pools_.size(); } detail::fixed_memory_stack allocate_block() diff --git a/include/foonathan/memory/memory_pool_type.hpp b/include/foonathan/memory/memory_pool_type.hpp index 714edea..c5cf2e5 100644 --- a/include/foonathan/memory/memory_pool_type.hpp +++ b/include/foonathan/memory/memory_pool_type.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_POOL_TYPE_HPP_INCLUDED diff --git a/include/foonathan/memory/memory_resource_adapter.hpp b/include/foonathan/memory/memory_resource_adapter.hpp index 7ff48f1..4fad726 100644 --- a/include/foonathan/memory/memory_resource_adapter.hpp +++ b/include/foonathan/memory/memory_resource_adapter.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_RESOURCE_ADAPTER_HPP_INCLUDED diff --git a/include/foonathan/memory/memory_stack.hpp b/include/foonathan/memory/memory_stack.hpp index b3dcd0f..b4a0234 100644 --- a/include/foonathan/memory/memory_stack.hpp +++ b/include/foonathan/memory/memory_stack.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_MEMORY_STACK_HPP_INCLUDED diff --git a/include/foonathan/memory/namespace_alias.hpp b/include/foonathan/memory/namespace_alias.hpp index a3e0354..50caeca 100644 --- a/include/foonathan/memory/namespace_alias.hpp +++ b/include/foonathan/memory/namespace_alias.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_NAMESPACE_ALIAS_HPP_INCLUDED diff --git a/include/foonathan/memory/new_allocator.hpp b/include/foonathan/memory/new_allocator.hpp index 61fb5db..73c3c7c 100644 --- a/include/foonathan/memory/new_allocator.hpp +++ b/include/foonathan/memory/new_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_NEW_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/segregator.hpp b/include/foonathan/memory/segregator.hpp index 80e066b..101b82a 100644 --- a/include/foonathan/memory/segregator.hpp +++ b/include/foonathan/memory/segregator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_SEGREGATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/smart_ptr.hpp b/include/foonathan/memory/smart_ptr.hpp index 9964495..e4f3f02 100644 --- a/include/foonathan/memory/smart_ptr.hpp +++ b/include/foonathan/memory/smart_ptr.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_SMART_PTR_HPP_INCLUDED diff --git a/include/foonathan/memory/static_allocator.hpp b/include/foonathan/memory/static_allocator.hpp index 17552b4..19fc208 100644 --- a/include/foonathan/memory/static_allocator.hpp +++ b/include/foonathan/memory/static_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_STATIC_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/std_allocator.hpp b/include/foonathan/memory/std_allocator.hpp index e865915..d31d449 100644 --- a/include/foonathan/memory/std_allocator.hpp +++ b/include/foonathan/memory/std_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_STD_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/temporary_allocator.hpp b/include/foonathan/memory/temporary_allocator.hpp index 3b6269d..292a2d1 100644 --- a/include/foonathan/memory/temporary_allocator.hpp +++ b/include/foonathan/memory/temporary_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TEMPORARY_ALLOCATOR_HPP_INCLUDED diff --git a/include/foonathan/memory/threading.hpp b/include/foonathan/memory/threading.hpp index 4cf507c..059e3b4 100644 --- a/include/foonathan/memory/threading.hpp +++ b/include/foonathan/memory/threading.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_THREADING_HPP_INCLUDED @@ -148,7 +148,7 @@ namespace foonathan return {a, m}; } } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_THREADING_HPP_INCLUDED diff --git a/include/foonathan/memory/tracking.hpp b/include/foonathan/memory/tracking.hpp index 57fb0d1..f815b5d 100644 --- a/include/foonathan/memory/tracking.hpp +++ b/include/foonathan/memory/tracking.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TRACKING_HPP_INCLUDED @@ -213,7 +213,7 @@ namespace foonathan tracked_allocator& operator=(tracked_allocator&& other) noexcept { - tracker:: operator=(detail::move(other)); + tracker::operator=(detail::move(other)); allocator_type::operator=(detail::move(other)); detail::set_tracker(0, get_allocator(), &get_tracker()); return *this; diff --git a/include/foonathan/memory/virtual_memory.hpp b/include/foonathan/memory/virtual_memory.hpp index 68705e9..a5d1662 100644 --- a/include/foonathan/memory/virtual_memory.hpp +++ b/include/foonathan/memory/virtual_memory.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_VIRTUAL_MEMORY_HPP_INCLUDED @@ -137,6 +137,8 @@ namespace foonathan explicit virtual_block_allocator(std::size_t block_size, std::size_t no_blocks); /// \effects Releases the reserved virtual memory. + /// \requires All previously \ref allocate_block() committed blocks must be decommitted via + /// \ref deallocate_block(), otherwise the blocks that have not been deallocated are leaked. ~virtual_block_allocator() noexcept; /// @{ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7b26572..e736ae5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # builds actual library @@ -87,9 +87,9 @@ target_include_directories(foonathan_memory PUBLIC $(ptr); - return address % alignment == 0u; + return (address & (alignment - 1)) == 0u; } std::size_t foonathan::memory::detail::alignment_for(std::size_t size) noexcept { - return size >= max_alignment ? max_alignment : (std::size_t(1) << ilog2(size)); + auto largest_possible_alignment = size & ~(size - 1); + return largest_possible_alignment > max_alignment ? max_alignment : largest_possible_alignment; } diff --git a/src/detail/assert.cpp b/src/detail/assert.cpp index 91ddd2e..bbb973c 100644 --- a/src/detail/assert.cpp +++ b/src/detail/assert.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/assert.hpp" diff --git a/src/detail/debug_helpers.cpp b/src/detail/debug_helpers.cpp index 65b998a..0082f2d 100644 --- a/src/detail/debug_helpers.cpp +++ b/src/detail/debug_helpers.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/debug_helpers.hpp" diff --git a/src/detail/free_list.cpp b/src/detail/free_list.cpp index ebf494c..f7c2c78 100644 --- a/src/detail/free_list.cpp +++ b/src/detail/free_list.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/free_list.hpp" diff --git a/src/detail/free_list_array.cpp b/src/detail/free_list_array.cpp index 907f795..943d3d1 100644 --- a/src/detail/free_list_array.cpp +++ b/src/detail/free_list_array.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/free_list_array.hpp" diff --git a/src/detail/free_list_utils.hpp b/src/detail/free_list_utils.hpp index bffee4d..fa42c0f 100644 --- a/src/detail/free_list_utils.hpp +++ b/src/detail/free_list_utils.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_SRC_DETAIL_FREE_LIST_UTILS_HPP_INCLUDED @@ -142,7 +142,7 @@ namespace foonathan return a == b || greater(a, b); } } // namespace detail - } // namespace memory + } // namespace memory } // namespace foonathan #endif // FOONATHAN_MEMORY_SRC_DETAIL_FREE_LIST_UTILS_HPP_INCLUDED diff --git a/src/detail/small_free_list.cpp b/src/detail/small_free_list.cpp index 1967b74..7bebed3 100644 --- a/src/detail/small_free_list.cpp +++ b/src/detail/small_free_list.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/small_free_list.hpp" diff --git a/src/error.cpp b/src/error.cpp index 53adece..f814d1d 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "error.hpp" diff --git a/src/heap_allocator.cpp b/src/heap_allocator.cpp index 81c91d4..4ecd4f6 100644 --- a/src/heap_allocator.cpp +++ b/src/heap_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "heap_allocator.hpp" diff --git a/src/iteration_allocator.cpp b/src/iteration_allocator.cpp index 06194e8..5cd63c5 100644 --- a/src/iteration_allocator.cpp +++ b/src/iteration_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "iteration_allocator.hpp" diff --git a/src/malloc_allocator.cpp b/src/malloc_allocator.cpp index 8e70c03..fbe7668 100644 --- a/src/malloc_allocator.cpp +++ b/src/malloc_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "config.hpp" diff --git a/src/memory_arena.cpp b/src/memory_arena.cpp index 44edd1a..80294ef 100644 --- a/src/memory_arena.cpp +++ b/src/memory_arena.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_arena.hpp" diff --git a/src/memory_pool.cpp b/src/memory_pool.cpp index 0159f5d..ed522bb 100644 --- a/src/memory_pool.cpp +++ b/src/memory_pool.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_pool.hpp" diff --git a/src/memory_pool_collection.cpp b/src/memory_pool_collection.cpp index bd04746..b7afe44 100644 --- a/src/memory_pool_collection.cpp +++ b/src/memory_pool_collection.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_pool_collection.hpp" diff --git a/src/memory_stack.cpp b/src/memory_stack.cpp index 36a6c73..374fe1a 100644 --- a/src/memory_stack.cpp +++ b/src/memory_stack.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_stack.hpp" diff --git a/src/new_allocator.cpp b/src/new_allocator.cpp index 80dcc3f..8286e56 100644 --- a/src/new_allocator.cpp +++ b/src/new_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "new_allocator.hpp" diff --git a/src/static_allocator.cpp b/src/static_allocator.cpp index b309d29..c8f1733 100644 --- a/src/static_allocator.cpp +++ b/src/static_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "static_allocator.hpp" diff --git a/src/temporary_allocator.cpp b/src/temporary_allocator.cpp index 2101c5f..cd1ae03 100644 --- a/src/temporary_allocator.cpp +++ b/src/temporary_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "temporary_allocator.hpp" @@ -60,31 +60,11 @@ void detail::temporary_block_allocator::deallocate_block(memory_block block) // lifetime managment through the nifty counter and the list // note: I could have used a simple `thread_local` variable for the temporary stack // but this could lead to issues with destruction order -// and more importantly I have to support platforms that can't handle non-trivial thread local's // hence I need to dynamically allocate the stack's and store them in a container // on program exit the container is iterated and all stack's are properly destroyed -// if a thread exit can be detected, the dynamic memory of the stack is already released, +// if a thread exit is detected, the dynamic memory of the stack is already released, // but not the stack itself destroyed -#if !defined(__MINGW64__) - -// only use the thread exit detector if we have thread local and are not running on MinGW due to a bug -// see: https://sourceforge.net/p/mingw-w64/bugs/527/ -#define FOONATHAN_MEMORY_THREAD_EXIT_DETECTOR 1 - -#else -#define FOONATHAN_MEMORY_THREAD_EXIT_DETECTOR 0 - -#if defined(_MSC_VER) -#pragma message( \ - "thread_local doesn't support destructors, need to use the temporary_stack_initializer to ensure proper cleanup of the temporary memory") -#else -#warning \ - "thread_local doesn't support destructors, need to use the temporary_stack_initializer to ensure proper cleanup of the temporary memory" -#endif - -#endif - static class detail::temporary_stack_list { public: @@ -151,8 +131,6 @@ namespace thread_local std::size_t nifty_counter; thread_local temporary_stack* temp_stack = nullptr; -#if FOONATHAN_MEMORY_THREAD_EXIT_DETECTOR - // don't use this on a bug thread_local struct thread_exit_detector_t { ~thread_exit_detector_t() noexcept @@ -166,7 +144,6 @@ namespace temporary_stack_list_obj.clear(*temp_stack); } } thread_exit_detector; -#endif } // namespace detail::temporary_stack_list_node::temporary_stack_list_node(int) noexcept : in_use_(true) @@ -174,9 +151,7 @@ detail::temporary_stack_list_node::temporary_stack_list_node(int) noexcept : in_ next_ = temporary_stack_list_obj.first.load(); while (!temporary_stack_list_obj.first.compare_exchange_weak(next_, this)) ; -#if FOONATHAN_MEMORY_THREAD_EXIT_DETECTOR (void)&thread_exit_detector; // ODR-use it, so it will be created -#endif } detail::temporary_allocator_dtor_t::temporary_allocator_dtor_t() noexcept diff --git a/src/virtual_memory.cpp b/src/virtual_memory.cpp index 7cc7a6d..7c8ca00 100644 --- a/src/virtual_memory.cpp +++ b/src/virtual_memory.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "virtual_memory.hpp" @@ -49,6 +49,7 @@ void foonathan::memory::virtual_memory_release(void* pages, std::size_t) noexcep { auto result = VirtualFree(pages, 0u, MEM_RELEASE); FOONATHAN_MEMORY_ASSERT_MSG(result, "cannot release pages"); + (void)result; } void* foonathan::memory::virtual_memory_commit(void* memory, std::size_t no_pages) noexcept @@ -70,6 +71,7 @@ void foonathan::memory::virtual_memory_decommit(void* memory, std::size_t no_pag { auto result = VirtualFree(memory, no_pages * virtual_memory_page_size, MEM_DECOMMIT); FOONATHAN_MEMORY_ASSERT_MSG(result, "cannot decommit memory"); + (void)result; } #elif defined(__unix__) || defined(__APPLE__) || defined(__VXWORKS__) \ || defined(__QNXNTO__) // POSIX systems @@ -230,7 +232,7 @@ void virtual_block_allocator::deallocate_block(memory_block block) noexcept { return static_cast(block.memory) == cur_ - block_size_; }, info(), block.memory); cur_ -= block_size_; - virtual_memory_decommit(cur_, block_size_); + virtual_memory_decommit(cur_, block_size_ / virtual_memory_page_size); } allocator_info virtual_block_allocator::info() noexcept diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 37359ea..787dc61 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # builds test @@ -11,7 +11,7 @@ target_include_directories(foonathan_memory_profiling PRIVATE # Fetch doctest. message(STATUS "Fetching doctest") include(FetchContent) -FetchContent_Declare(doctest URL https://github.com/doctest/doctest/archive/refs/tags/v2.4.9.zip) +FetchContent_Declare(doctest URL https://github.com/doctest/doctest/archive/refs/tags/v2.4.12.zip) FetchContent_MakeAvailable(doctest) set(tests diff --git a/test/aligned_allocator.cpp b/test/aligned_allocator.cpp index 125ae70..05ee1f4 100644 --- a/test/aligned_allocator.cpp +++ b/test/aligned_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "aligned_allocator.hpp" diff --git a/test/allocator_traits.cpp b/test/allocator_traits.cpp index 1b66b1b..4b253da 100644 --- a/test/allocator_traits.cpp +++ b/test/allocator_traits.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "allocator_traits.hpp" diff --git a/test/benchmark.hpp b/test/benchmark.hpp index 1346329..969e2d7 100644 --- a/test/benchmark.hpp +++ b/test/benchmark.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TEST_BENCHMARK_HPP_INCLUDED @@ -172,8 +172,7 @@ struct butterfly : basic_bulk { butterfly(std::size_t c) : basic_bulk([](std::vector& ptrs) - { std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{}); }, - c) + { std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{}); }, c) { } diff --git a/test/default_allocator.cpp b/test/default_allocator.cpp index bdf7d53..d31e540 100644 --- a/test/default_allocator.cpp +++ b/test/default_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // tests all possible default allocator classes @@ -9,12 +9,13 @@ #include #include "detail/align.hpp" +#include "memory_arena.hpp" using namespace foonathan::memory; // *very* simple test case to ensure proper alignment and might catch some segfaults template -void check_default_allocator(Allocator& alloc, std::size_t def_alignment = detail::max_alignment) +void check_default_allocator(Allocator& alloc, std::size_t def_alignment = alignof(void*)) { auto ptr = alloc.allocate_node(1, 1); REQUIRE(detail::is_aligned(ptr, def_alignment)); @@ -72,3 +73,15 @@ TEST_CASE("virtual_memory_allocator") virtual_memory_allocator alloc; check_default_allocator(alloc, get_virtual_memory_page_size()); } + +TEST_CASE("virtual_block_allocator") +{ + auto const page_size = get_virtual_memory_page_size(); + constexpr std::size_t no_blocks{8u}; + + virtual_block_allocator alloc{page_size, no_blocks}; + auto block = alloc.allocate_block(); + REQUIRE(block.memory != nullptr); + REQUIRE(block.size == page_size); + alloc.deallocate_block(block); +} diff --git a/test/detail/align.cpp b/test/detail/align.cpp index 9ad7c84..c60dfcc 100644 --- a/test/detail/align.cpp +++ b/test/detail/align.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/align.hpp" @@ -8,6 +8,21 @@ using namespace foonathan::memory; using namespace detail; +TEST_CASE("detail::round_up_to_multiple_of_alignment") +{ + REQUIRE(round_up_to_multiple_of_alignment(0, 1) == 0); + REQUIRE(round_up_to_multiple_of_alignment(1, 1) == 1); + REQUIRE(round_up_to_multiple_of_alignment(2, 1) == 2); + REQUIRE(round_up_to_multiple_of_alignment(3, 1) == 3); + REQUIRE(round_up_to_multiple_of_alignment(4, 1) == 4); + + REQUIRE(round_up_to_multiple_of_alignment(0, 2) == 0); + REQUIRE(round_up_to_multiple_of_alignment(1, 2) == 2); + REQUIRE(round_up_to_multiple_of_alignment(2, 2) == 2); + REQUIRE(round_up_to_multiple_of_alignment(3, 2) == 4); + REQUIRE(round_up_to_multiple_of_alignment(4, 2) == 4); +} + TEST_CASE("detail::align_offset") { auto ptr = reinterpret_cast(0); @@ -54,12 +69,18 @@ TEST_CASE("detail::alignment_for") static_assert(max_alignment >= 8, "test case not working"); REQUIRE(alignment_for(1) == 1); REQUIRE(alignment_for(2) == 2); - REQUIRE(alignment_for(3) == 2); + REQUIRE(alignment_for(3) == 1); REQUIRE(alignment_for(4) == 4); - REQUIRE(alignment_for(5) == 4); - REQUIRE(alignment_for(6) == 4); - REQUIRE(alignment_for(7) == 4); + REQUIRE(alignment_for(5) == 1); + REQUIRE(alignment_for(6) == 2); + REQUIRE(alignment_for(7) == 1); REQUIRE(alignment_for(8) == 8); - REQUIRE(alignment_for(9) == 8); - REQUIRE(alignment_for(100) == max_alignment); + REQUIRE(alignment_for(9) == 1); + REQUIRE(alignment_for(10) == 2); + REQUIRE(alignment_for(12) == 4); + REQUIRE(alignment_for(13) == 1); + REQUIRE(alignment_for(14) == 2); + REQUIRE(alignment_for(15) == 1); + REQUIRE(alignment_for(16) == max_alignment); + REQUIRE(alignment_for(1024) == max_alignment); } diff --git a/test/detail/debug_helpers.cpp b/test/detail/debug_helpers.cpp index 847f5c3..a1d7b3c 100644 --- a/test/detail/debug_helpers.cpp +++ b/test/detail/debug_helpers.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/debug_helpers.hpp" diff --git a/test/detail/free_list.cpp b/test/detail/free_list.cpp index f7f7e41..214b536 100644 --- a/test/detail/free_list.cpp +++ b/test/detail/free_list.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/free_list.hpp" diff --git a/test/detail/free_list_array.cpp b/test/detail/free_list_array.cpp index 0e14f20..b404c82 100644 --- a/test/detail/free_list_array.cpp +++ b/test/detail/free_list_array.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/free_list_array.hpp" diff --git a/test/detail/ilog2.cpp b/test/detail/ilog2.cpp index 584f644..83db8ba 100644 --- a/test/detail/ilog2.cpp +++ b/test/detail/ilog2.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/ilog2.hpp" diff --git a/test/detail/memory_stack.cpp b/test/detail/memory_stack.cpp index da1ffc7..5d322f5 100644 --- a/test/detail/memory_stack.cpp +++ b/test/detail/memory_stack.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "detail/memory_stack.hpp" diff --git a/test/fallback_allocator.cpp b/test/fallback_allocator.cpp index d3278a5..878648b 100644 --- a/test/fallback_allocator.cpp +++ b/test/fallback_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "fallback_allocator.hpp" diff --git a/test/iteration_allocator.cpp b/test/iteration_allocator.cpp index f5a80d8..50f79aa 100644 --- a/test/iteration_allocator.cpp +++ b/test/iteration_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "iteration_allocator.hpp" diff --git a/test/joint_allocator.cpp b/test/joint_allocator.cpp index 5c3f4a2..1e33126 100644 --- a/test/joint_allocator.cpp +++ b/test/joint_allocator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "joint_allocator.hpp" diff --git a/test/memory_arena.cpp b/test/memory_arena.cpp index a054963..41b4b00 100644 --- a/test/memory_arena.cpp +++ b/test/memory_arena.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_arena.hpp" diff --git a/test/memory_pool.cpp b/test/memory_pool.cpp index f043c75..6c980d3 100644 --- a/test/memory_pool.cpp +++ b/test/memory_pool.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_pool.hpp" diff --git a/test/memory_pool_collection.cpp b/test/memory_pool_collection.cpp index 73bc81c..ad6e456 100644 --- a/test/memory_pool_collection.cpp +++ b/test/memory_pool_collection.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_pool_collection.hpp" diff --git a/test/memory_resource_adapter.cpp b/test/memory_resource_adapter.cpp index 4f01f8b..5a982f1 100644 --- a/test/memory_resource_adapter.cpp +++ b/test/memory_resource_adapter.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_resource_adapter.hpp" @@ -24,7 +24,7 @@ struct pmr_test_allocator void* allocate_array(std::size_t n, std::size_t size, std::size_t) { array_allocated += n * size; - return ::operator new(n* size); + return ::operator new(n * size); } void deallocate_node(void* p, std::size_t size, std::size_t) diff --git a/test/memory_stack.cpp b/test/memory_stack.cpp index e519ab5..35f5534 100644 --- a/test/memory_stack.cpp +++ b/test/memory_stack.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "memory_stack.hpp" diff --git a/test/profiling.cpp b/test/profiling.cpp index b50db5b..823d58d 100644 --- a/test/profiling.cpp +++ b/test/profiling.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib // Profiling code to check performance of allocators. diff --git a/test/segregator.cpp b/test/segregator.cpp index 70b3434..1d9fffe 100644 --- a/test/segregator.cpp +++ b/test/segregator.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "segregator.hpp" diff --git a/test/smart_ptr.cpp b/test/smart_ptr.cpp index ba98c88..7c273b3 100644 --- a/test/smart_ptr.cpp +++ b/test/smart_ptr.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include "smart_ptr.hpp" diff --git a/test/test.cpp b/test/test.cpp index 4319e6f..7e806cc 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN diff --git a/test/test_allocator.hpp b/test/test_allocator.hpp index df17666..53d4d4e 100644 --- a/test/test_allocator.hpp +++ b/test/test_allocator.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TEST_TEST_ALLOCATOR_HPP diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index a7a650e..60e188d 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -1,22 +1,14 @@ -# Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +# Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors # SPDX-License-Identifier: Zlib # builds tools add_executable(foonathan_memory_node_size_debugger test_types.hpp node_size_debugger.hpp node_size_debugger.cpp) -if (CMAKE_CROSSCOMPILING) - # statically link when cross compiling so emulator doesn't need library paths - if (MSVC) - set_target_properties(foonathan_memory_node_size_debugger PROPERTIES LINK_FLAGS "/WHOLEARCHIVE") - else() - set_target_properties(foonathan_memory_node_size_debugger PROPERTIES LINK_FLAGS "-static") - endif() -endif() if (MSVC) target_compile_options(foonathan_memory_node_size_debugger PRIVATE "/bigobj") endif() target_compile_definitions(foonathan_memory_node_size_debugger PUBLIC - VERSION="${FOONATHAN_MEMORY_VERSION_MAJOR}.${FOONATHAN_MEMORY_VERSION_MINOR}") + VERSION="$CACHE{FOONATHAN_MEMORY_VERSION_MAJOR}.$CACHE{FOONATHAN_MEMORY_VERSION_MINOR}") set_target_properties(foonathan_memory_node_size_debugger PROPERTIES OUTPUT_NAME nodesize_dbg) if(NOT MSVC) target_compile_features(foonathan_memory_node_size_debugger PUBLIC cxx_constexpr) diff --git a/tool/node_size_debugger.cpp b/tool/node_size_debugger.cpp index 917fb3f..43e82ab 100644 --- a/tool/node_size_debugger.cpp +++ b/tool/node_size_debugger.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #include @@ -91,8 +91,9 @@ struct code_serializer out << "} // namespace detail" << newline << newline << "template " << newline << "struct " << struct_name(result.container_name) << newline << ": std::integral_constant::value + sizeof(T)>" << newline << "{};" << newline << newline; + << " detail::round_up_to_multiple_of_alignment(detail::" + << struct_name(result.container_name) << '<' << alignment + << ">::value + sizeof(T), alignof(void*))>" << newline << "{};" << newline << newline; } void suffix() const diff --git a/tool/node_size_debugger.hpp b/tool/node_size_debugger.hpp index a9db2ce..8ee32bb 100644 --- a/tool/node_size_debugger.hpp +++ b/tool/node_size_debugger.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TOOL_NODE_SIZE_DEBUGGER_HPP diff --git a/tool/test_types.hpp b/tool/test_types.hpp index c2c50c7..801a215 100644 --- a/tool/test_types.hpp +++ b/tool/test_types.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors +// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors // SPDX-License-Identifier: Zlib #ifndef FOONATHAN_MEMORY_TOOL_TEST_TYPES_HPP_INCLUDED