-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChunkPool.hxx
65 lines (48 loc) · 1.28 KB
/
ChunkPool.hxx
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
/* Copyright 2015, 2016 Robert Haberlach
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) */
#pragma once
#include "Array.hxx"
#include "Bitset.hxx"
namespace Constainer {
template <typename T, STD::size_t N>
class ChunkPool : private Array<T, N> {
using _base = Array<T, N>;
public:
template <typename U>
using rebind = ChunkPool<U, N>;
using typename _base::size_type;
using typename _base::value_type;
using typename _base::pointer;
using typename _base::const_pointer;
using typename _base::reference;
using typename _base::const_reference;
static constexpr size_type max_size() {return _base::size();}
private:
Bitset<N> _used;
public:
constexpr ChunkPool() : _base{}, _used{} {}
constexpr size_type used() const {
return _used.count();
}
constexpr bool available() const {
return !_used.all();
}
constexpr pointer grab() {
auto pos = _used.leading(1);
AssertExcept<STD::bad_alloc>(pos != this->size());
_used.set(pos);
auto& ref = _base::operator[](pos);
ref = value_type();
return &ref;
}
constexpr void free() {
_used.reset();
}
constexpr void free( const_pointer p ) {
auto i = p-_base::data();
assert( _used.test(i) );
_used.reset(i);
}
};
}