-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.h
59 lines (55 loc) · 1.48 KB
/
kmp.h
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
#pragma once
#include <type_traits>
#include <stdexcept>
#include <cstdlib>
namespace kmp {
using std::size_t;
template<typename T>
struct DestructIf {
void operator()(const T *v) const {const_cast<T *>(v)->~T();}
};
template<typename T, typename FreeFunc=DestructIf<T>>
class Pool {
// Pointer memory pool
size_t cnt_, n_, max_;
T **buf_;
FreeFunc func_;
public:
Pool(): cnt_(0), n_(0), max_(0), buf_(0), func_() {}
T *malloc() {
++cnt_;
if(n_ == 0) return static_cast<T *>(std::malloc(sizeof(T)));
return buf_[--n_];
}
T *calloc() {
++cnt_;
if(n_ == 0) return static_cast<T *>(std::calloc(1, sizeof(T)));
auto ret = buf_[--n_];
std::memset(ret, 0, sizeof(T)); // Zero memory pointed to.
return ret;
}
template<typename... Args>
T *placement_new(Args &&... args) {
++cnt_;
if(n_ == 0) return new T(std::forward<Args>(args)...);
T *ret = buf_[n_];
func_(ret); // This should be its destructor.
return buf_[--n_];
}
void free(T *p) {
--cnt_;
if(n_ == max_) {
max_ = max_ ? max_ << 1: 16;
if((buf_ = static_cast<T **>(std::realloc(buf_, sizeof(T *) * max_))) == nullptr) throw std::bad_alloc();
}
buf_[n_++] = p;
}
~Pool() {
for(size_t k = 0; k < n_; ++k) {
func_(buf_[k]);
std::free(buf_[k]);
}
std::free(buf_);
}
};
} // namespace kmp