about summary refs log tree commit diff
path: root/immer/heap/debug_size_heap.hpp
blob: d5288c646f8d757e4e76e66120a1ec0c83a86006 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include <immer/config.hpp>
#include <immer/heap/identity_heap.hpp>

#include <cassert>
#include <cstddef>
#include <type_traits>
#include <memory>

namespace immer {

#if IMMER_ENABLE_DEBUG_SIZE_HEAP

/*!
 * A heap that in debug mode ensures that the sizes for allocation and
 * deallocation do match.
 */
template <typename Base>
struct debug_size_heap
{
#if defined(__MINGW32__) && !defined(__MINGW64__)
    // There is a bug in MinGW 32bit:
    // https://sourceforge.net/p/mingw-w64/bugs/778/ It causes different
    // versions of std::max_align_t to be defined, depending on inclusion order
    // of stddef.h and stdint.h. As we have no control over the inclusion order
    // here (as it might be set in stone by the outside world), we can't easily
    // pin it to one of both versions of std::max_align_t. This means, we have
    // to hardcode extra_size for MinGW 32bit builds until the mentioned bug is
    // fixed.
    constexpr static auto extra_size = 8;
#else
    constexpr static auto extra_size = sizeof(
        std::aligned_storage_t<sizeof(std::size_t), alignof(std::max_align_t)>);
#endif

    template <typename... Tags>
    static void* allocate(std::size_t size, Tags... tags)
    {
        auto p = (std::size_t*) Base::allocate(size + extra_size, tags...);
        new (p) std::size_t{size};
        return ((char*) p) + extra_size;
    }

    template <typename... Tags>
    static void deallocate(std::size_t size, void* data, Tags... tags)
    {
        auto p = (std::size_t*) (((char*) data) - extra_size);
        assert(*p == size);
        Base::deallocate(size + extra_size, p, tags...);
    }
};

#else // IMMER_ENABLE_DEBUG_SIZE_HEAP

template <typename Base>
using debug_size_heap = identity_heap<Base>;

#endif // !IMMER_ENABLE_DEBUG_SIZE_HEAP

} // namespace immer