-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.cpp
More file actions
43 lines (38 loc) · 955 Bytes
/
pool.cpp
File metadata and controls
43 lines (38 loc) · 955 Bytes
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
#include "pool.h"
#include <math.h>
#include <stdio.h>
static std::vector<Pool> pools;
void add_pool_of_size(size_t size)
{
Pool p;
pools.push_back(p);
Pool& pool = pools.back();
pool.pool.reserve(size);
pool.lock = 0;
}
void init_pools(std::vector<size_t> sizes)
{
pools.reserve(sizes.size());
for (size_t size : sizes)
{
add_pool_of_size(size);
}
}
PoolView get_pool(size_t min_size)
{
for (Pool& pool : pools)
{
if (pool.lock == 0 && pool.pool.capacity() >= min_size)
{
pool.pool.resize(0);
return PoolView(pool);
}
}
// No pools large enough. Create a new one with size of the next
// power of two.
float fp2 = log2f((float)min_size);
int target_size = 1 << (int)ceil(fp2);
printf("Pools exhausted! Creating pool of size %d to meet request of size %zu.\n", target_size, min_size);
add_pool_of_size(target_size);
return PoolView(pools[pools.size() - 1]);
}