/usr/include/caf/detail/memory.hpp is in libcaf-dev 0.13.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2015 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_DETAIL_MEMORY_HPP
#define CAF_DETAIL_MEMORY_HPP
#include <new>
#include <vector>
#include <memory>
#include <utility>
#include <typeinfo>
#include "caf/config.hpp"
#include "caf/ref_counted.hpp"
#include "caf/detail/embedded.hpp"
#include "caf/detail/memory_cache_flag_type.hpp"
namespace caf {
class mailbox_element;
} // namespace caf
namespace caf {
namespace detail {
namespace {
constexpr size_t s_alloc_size = 1024 * 1024; // allocate ~1mb chunks
constexpr size_t s_cache_size = 10 * 1024 * 1024; // cache about 10mb per thread
constexpr size_t s_min_elements = 5; // don't create < 5 elements
constexpr size_t s_max_elements = 20; // don't create > 20 elements
} // namespace <anonymous>
using embedded_storage = std::pair<intrusive_ptr<ref_counted>, void*>;
class memory_cache {
public:
virtual ~memory_cache();
virtual embedded_storage new_embedded_storage() = 0;
};
template <class T>
class basic_memory_cache;
#ifdef CAF_NO_MEM_MANAGEMENT
template <class T>
struct rc_storage : public ref_counted {
T instance;
template <class... Ts>
rc_storage(Ts&&... xs)
: instance(intrusive_ptr<ref_counted>(this, false),
std::forward<Ts>(xs)...) {
CAF_ASSERT(get_reference_count() >= 1);
}
};
template <class T>
T* unbox_rc_storage(T* ptr) {
return ptr;
}
template <class T>
T* unbox_rc_storage(rc_storage<T>* ptr) {
return &(ptr->instance);
}
class memory {
public:
memory() = delete;
// Allocates storage, initializes a new object, and returns the new instance.
template <class T, class... Ts>
static T* create(Ts&&... xs) {
using embedded_t =
typename std::conditional<
T::memory_cache_flag == provides_embedding,
rc_storage<T>,
T
>::type;
return unbox_rc_storage(new embedded_t(std::forward<Ts>(xs)...));
}
static inline memory_cache* get_cache_map_entry(const std::type_info*) {
return nullptr;
}
};
#else // CAF_NO_MEM_MANAGEMENT
template <class T>
class basic_memory_cache : public memory_cache {
public:
static constexpr size_t ne = s_alloc_size / sizeof(T);
static constexpr size_t ms = ne < s_min_elements ? s_min_elements : ne;
static constexpr size_t dsize = ms > s_max_elements ? s_max_elements : ms;
static_assert(dsize > 0, "dsize == 0");
using embedded_t =
typename std::conditional<
T::memory_cache_flag == needs_embedding,
embedded<T>,
T
>::type;
struct wrapper {
union {
embedded_t instance;
};
wrapper() {
// nop
}
~wrapper() {
// nop
}
};
class storage : public ref_counted {
public:
storage() : m_pos(0) {
// nop
}
~storage() {
// nop
}
bool has_next() {
return m_pos < dsize;
}
embedded_t* next() {
return &(m_data[m_pos++].instance);
}
private:
size_t m_pos;
wrapper m_data[dsize];
};
embedded_storage new_embedded_storage() override {
// allocate cache on-the-fly
if (!m_cache) {
m_cache.reset(new storage, false); // starts with ref count of 1
CAF_ASSERT(m_cache->unique());
}
auto res = m_cache->next();
if (m_cache->has_next()) {
return {m_cache, res};
}
// we got the last element out of the cache; pass the reference to the
// client to avoid pointless increase/decrease ops on the reference count
embedded_storage result;
result.first.reset(m_cache.release(), false);
result.second = res;
return result;
}
private:
intrusive_ptr<storage> m_cache;
};
class memory {
memory() = delete;
template <class>
friend class basic_memory_cache;
public:
// Allocates storage, initializes a new object, and returns the new instance.
template <class T, class... Ts>
static T* create(Ts&&... xs) {
using embedded_t =
typename std::conditional<
T::memory_cache_flag == needs_embedding,
embedded<T>,
T
>::type;
auto mc = get_or_set_cache_map_entry<T>();
auto es = mc->new_embedded_storage();
auto ptr = reinterpret_cast<embedded_t*>(es.second);
new (ptr) embedded_t(std::move(es.first), std::forward<Ts>(xs)...);
return ptr;
}
static memory_cache* get_cache_map_entry(const std::type_info* tinf);
private:
static void add_cache_map_entry(const std::type_info* tinf,
memory_cache* instance);
template <class T>
static inline memory_cache* get_or_set_cache_map_entry() {
auto mc = get_cache_map_entry(&typeid(T));
if (!mc) {
mc = new basic_memory_cache<T>;
add_cache_map_entry(&typeid(T), mc);
}
return mc;
}
};
#endif // CAF_NO_MEM_MANAGEMENT
} // namespace detail
} // namespace caf
#endif // CAF_DETAIL_MEMORY_HPP
|