deCONZ C++ API v2.6.1
Loading...
Searching...
No Matches
mem_pool.h
1#ifndef MEM_POOL_H
2#define MEM_POOL_H
3
4/*
5 * Copyright (c) 2012-2023 dresden elektronik ingenieurtechnik gmbh.
6 * All rights reserved.
7 *
8 * The software in this package is published under the terms of the BSD
9 * style license a copy of which has been included with this distribution in
10 * the LICENSE.txt file.
11 *
12 */
13
14#include <algorithm>
15#include <array>
16#include <cassert>
17#include <tuple>
18
19template <typename T, typename Tuple>
20auto &MEM_GetAllocContainer(Tuple &t)
21{
22 return std::get<std::array<T*, T::PoolSize>>(t);
23}
24
25template <typename T, typename MemTuple>
26T *MEM_AllocItem(MemTuple *m)
27{
28 assert(m);
29 auto &cont = MEM_GetAllocContainer<T>(*m);
30
31 auto i = std::find_if(std::begin(cont), std::end(cont), [](const auto &i) { return i != nullptr; });
32
33 if (i != cont.end())
34 {
35 auto *p = *i;
36 *i = nullptr;
37 return p;
38 }
39
40 return new T;
41}
42
43template <typename T, typename MemTuple>
44void MEM_DeallocItem(T *priv, MemTuple *m)
45{
46 assert(m);
47 auto &cont = MEM_GetAllocContainer<T>(*m);
48 auto i = std::find(std::begin(cont), std::end(cont), nullptr);
49
50 if (i != cont.end())
51 {
52 *i = priv;
53 return;
54 }
55
56 delete priv;
57}
58
59#endif // MEM_POOL_H