-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpool.cpp
More file actions
90 lines (68 loc) · 2.45 KB
/
pool.cpp
File metadata and controls
90 lines (68 loc) · 2.45 KB
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
/* Memory pool for fixed size objects.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/exceptions.hpp>
#include <villas/kernel/kernel.hpp>
#include <villas/log.hpp>
#include <villas/node/memory.hpp>
#include <villas/pool.hpp>
#include <villas/utils.hpp>
using namespace villas;
const char *villas::node::pool_buffer(const struct Pool *pool) {
return reinterpret_cast<const char *>(pool) + pool->buffer_off;
}
int villas::node::pool_init(struct Pool *p, size_t cnt, size_t blocksz,
struct memory::Type *m) {
int ret;
auto logger = Log::get("pool");
// Make sure that we use a block size that is aligned to the size of a cache line
p->alignment = kernel::getCachelineSize();
p->blocksz = p->alignment * CEIL(blocksz, p->alignment);
p->len = cnt * p->blocksz;
logger->debug("New memory pool: alignment={}, blocksz={}, len={}, memory={}",
p->alignment, p->blocksz, p->len, m->name);
void *buffer = memory::alloc_aligned(p->len, p->alignment, m);
if (!buffer)
throw MemoryAllocationError();
logger->debug("Allocated {:#x} bytes for memory pool", p->len);
p->buffer_off =
reinterpret_cast<char *>(buffer) - reinterpret_cast<char *>(p);
ret = queue_init(&p->queue, std::bit_ceil(cnt), m);
if (ret)
return ret;
for (unsigned i = 0; i < cnt; i++)
queue_push(&p->queue, (char *)buffer + i * p->blocksz);
p->state = State::INITIALIZED;
return 0;
}
int villas::node::pool_destroy(struct Pool *p) {
int ret;
if (p->state == State::DESTROYED)
return 0;
ret = queue_destroy(&p->queue);
if (ret)
return ret;
void *buffer = reinterpret_cast<char *>(p) + p->buffer_off;
ret = memory::free(buffer);
if (ret == 0)
p->state = State::DESTROYED;
return ret;
}
ssize_t villas::node::pool_get_many(struct Pool *p, void *blocks[],
size_t cnt) {
return queue_pull_many(&p->queue, blocks, cnt);
}
ssize_t villas::node::pool_put_many(struct Pool *p, void *blocks[],
size_t cnt) {
return queue_push_many(&p->queue, blocks, cnt);
}
void *villas::node::pool_get(struct Pool *p) {
void *ptr;
return queue_pull(&p->queue, &ptr) == 1 ? ptr : nullptr;
}
int villas::node::pool_put(struct Pool *p, void *buf) {
return queue_push(&p->queue, buf);
}