forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.h
194 lines (165 loc) · 4.85 KB
/
pool.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
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
/*
* MEGAHIT
* Copyright (C) 2014 The University of Hong Kong
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file pool.h
* @brief Pool Class.
* @author Yu Peng ([email protected])
* @version 1.0.0
* @date 2011-08-23
*/
#ifndef __BASIC_POOL_H_
#define __BASIC_POOL_H_
#include <omp.h>
#include <stdint.h>
#include <deque>
#include <vector>
template <typename T>
struct Chunk
{
Chunk(T *address = NULL, uint32_t size = 0)
{ this->address = address; this->size = size; }
T *address;
uint32_t size;
};
template <typename T>
struct Buffer
{
Buffer(T *address = NULL, uint32_t size = 0, uint32_t index = 0)
{ this->address = address; this->size = size; this->index = index; }
T *address;
uint32_t size;
uint32_t index;
};
template <typename T, typename Allocator = std::allocator<T> >
class Pool
{
public:
typedef T value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef Allocator allocator_type;
typedef Chunk<T> chunk_type;
typedef Buffer<T> buffer_type;
typedef Pool<T> pool_type;
// static const uint32_t kMaxChunkSize = (1 << 12);
// static const uint32_t kMinChunkSize = (1 << 12);
static const uint32_t kMaxChunkSize = (1 << 20);
static const uint32_t kMinChunkSize = (1 << 8);
Pool()
{
omp_init_lock(&lock_alloc_);
heads_.resize(omp_get_max_threads(), (pointer)0);
buffers_.resize(omp_get_max_threads(), buffer_type());
chunk_size_ = kMinChunkSize;
}
~Pool()
{
clear();
omp_destroy_lock(&lock_alloc_);
}
pointer allocate()
{
int thread_id = omp_get_thread_num();
if (heads_[thread_id] != NULL)
{
pointer p = heads_[thread_id];
heads_[thread_id] = *(pointer *)heads_[thread_id];
return p;
}
else
{
buffer_type &buffer = buffers_[thread_id];
if (buffer.index == buffer.size)
{
omp_set_lock(&lock_alloc_);
uint32_t size = chunk_size_;
if (chunk_size_ < kMaxChunkSize)
chunk_size_ <<= 1;
omp_unset_lock(&lock_alloc_);
pointer p = alloc_.allocate(size);
omp_set_lock(&lock_alloc_);
chunks_.push_back(chunk_type(p, size));
omp_unset_lock(&lock_alloc_);
buffer.address = p;
buffer.size = size;
buffer.index = 0;
}
return buffer.address + buffer.index++;
}
}
void deallocate(pointer p)
{
int thread_id = omp_get_thread_num();
*(pointer *)p = heads_[thread_id];
heads_[thread_id] = p;
}
pointer construct()
{
pointer p = allocate();
new ((void *)p)value_type();
return p;
}
pointer construct(const_reference x)
{
pointer p = allocate();
new ((void *)p)value_type(x);
return p;
}
void destroy(pointer p)
{
((value_type*)p)->~value_type();
}
void swap(Pool<value_type> &pool)
{
if (this != &pool)
{
heads_.swap(pool.heads_);
buffers_.swap(pool.buffers_);
chunks_.swap(pool.chunks_);
std::swap(chunk_size_, pool.chunk_size_);
std::swap(alloc_, pool.alloc_);
}
}
void clear()
{
omp_set_lock(&lock_alloc_);
for (unsigned i = 0; i < chunks_.size(); ++i)
alloc_.deallocate(chunks_[i].address, chunks_[i].size);
chunks_.resize(0);
fill(heads_.begin(), heads_.end(), (pointer)0);
fill(buffers_.begin(), buffers_.end(), buffer_type());
chunk_size_ = kMinChunkSize;
omp_unset_lock(&lock_alloc_);
}
private:
Pool(const pool_type &);
const pool_type &operator =(const pool_type &);
std::vector<pointer> heads_;
std::vector<buffer_type> buffers_;
std::deque<chunk_type> chunks_;
uint32_t chunk_size_;
omp_lock_t lock_alloc_;
allocator_type alloc_;
};
namespace std
{
template <typename T> inline void swap(Pool<T> &x, Pool<T> &y) { x.swap(y); }
}
#endif