File: aligned_allocator.cpp

package info (click to toggle)
foonathan-memory 0.7.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,644 kB
  • sloc: cpp: 12,425; xml: 139; sh: 48; makefile: 25
file content (32 lines) | stat: -rw-r--r-- 971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib

#include <foonathan/memory/aligned_allocator.hpp>

#include <doctest/doctest.h>

#include <foonathan/memory/detail/align.hpp>
#include <foonathan/memory/allocator_storage.hpp>
#include <foonathan/memory/memory_stack.hpp>

using namespace foonathan::memory;

TEST_CASE("aligned_allocator")
{
    using allocator_t = aligned_allocator<allocator_reference<memory_stack<>>>;

    memory_stack<> stack(1024);
    stack.allocate(3, 1); // manual misalign

    allocator_t alloc(4u, stack);
    REQUIRE(alloc.min_alignment() == 4u);

    auto mem1 = alloc.allocate_node(16u, 1u);
    REQUIRE(detail::align_offset(mem1, 4u) == 0u);
    auto mem2 = alloc.allocate_node(16u, 8u);
    REQUIRE(detail::align_offset(mem2, 4u) == 0u);
    REQUIRE(detail::align_offset(mem2, 8u) == 0u);

    alloc.deallocate_node(mem2, 16u, 8u);
    alloc.deallocate_node(mem1, 16u, 1u);
}