Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

gc_heap.hpp
Go to the documentation of this file.
1 //
2 // immer: immutable data structures for C++
3 // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4 //
5 // This software is distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7 //
8 
9 #pragma once
10 
11 #include <immer/config.hpp>
12 #include <immer/heap/tags.hpp>
13 
14 #if IMMER_HAS_LIBGC
15 #include <gc/gc.h>
16 #else
17 #error "Using garbage collection requires libgc"
18 #endif
19 
20 #include <cstdlib>
21 #include <memory>
22 
23 namespace immer {
24 
25 #ifdef __APPLE__
26 #define IMMER_GC_REQUIRE_INIT 1
27 #else
28 #define IMMER_GC_REQUIRE_INIT 0
29 #endif
30 
31 #if IMMER_GC_REQUIRE_INIT
32 
33 namespace detail {
34 
35 template <int Dummy=0>
36 struct gc_initializer
37 {
38  gc_initializer() { GC_init(); }
39  static gc_initializer init;
40 };
41 
42 template <int D>
43 gc_initializer<D> gc_initializer<D>::init {};
44 
45 inline void gc_initializer_guard()
46 {
47  static gc_initializer<> init_ = gc_initializer<>::init;
48  (void) init_;
49 }
50 
51 } // namespace detail
52 
53 #define IMMER_GC_INIT_GUARD_ ::immer::detail::gc_initializer_guard()
54 
55 #else
56 
57 #define IMMER_GC_INIT_GUARD_
58 
59 #endif // IMMER_GC_REQUIRE_INIT
60 
98 class gc_heap
99 {
100 public:
101  static void* allocate(std::size_t n)
102  {
104  auto p = GC_malloc(n);
105  if (IMMER_UNLIKELY(!p))
106  throw std::bad_alloc{};
107  return p;
108  }
109 
110  static void* allocate(std::size_t n, norefs_tag)
111  {
113  auto p = GC_malloc_atomic(n);
114  if (IMMER_UNLIKELY(!p))
115  throw std::bad_alloc{};
116  return p;
117  }
118 
119  static void deallocate(std::size_t, void* data)
120  {
121  GC_free(data);
122  }
123 
124  static void deallocate(std::size_t, void* data, norefs_tag)
125  {
126  GC_free(data);
127  }
128 };
129 
130 } // namespace immer
static void * allocate(std::size_t n, norefs_tag)
Definition: gc_heap.hpp:110
static void deallocate(std::size_t, void *data, norefs_tag)
Definition: gc_heap.hpp:124
#define IMMER_UNLIKELY(cond)
Definition: config.hpp:47
std::size_t size_t
Definition: bits.hpp:21
#define IMMER_GC_INIT_GUARD_
Definition: gc_heap.hpp:57
static void * allocate(std::size_t n)
Definition: gc_heap.hpp:101
static void deallocate(std::size_t, void *data)
Definition: gc_heap.hpp:119
Released under the MIT license