File: aligned_allocator.cpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (33 lines) | stat: -rw-r--r-- 1,064 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
33
// Copyright (C) 2015-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

#include <foonathan/memory/aligned_allocator.hpp>

#include <catch.hpp>

#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", "[adapter]")
{
    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);
}