rogue
Loading...
Searching...
No Matches
Pool.cpp
Go to the documentation of this file.
1
19#include "rogue/Directives.h"
20
22
23#include <inttypes.h>
24#include <unistd.h>
25
26#include <memory>
27#include <string>
28
29#include "rogue/GeneralError.h"
30#include "rogue/GilRelease.h"
33
35
36#ifndef NO_PYTHON
37 #include <boost/python.hpp>
38namespace bp = boost::python;
39#endif
40
42ris::Pool::Pool() {
43 allocMeta_ = 0;
44 allocBytes_ = 0;
45 allocCount_ = 0;
46 fixedSize_ = 0;
47 poolSize_ = 0;
48}
49
51ris::Pool::~Pool() {
52 while (!dataQ_.empty()) {
53 free(dataQ_.front());
54 }
55}
56
58uint32_t ris::Pool::getAllocBytes() {
59 return (allocBytes_);
60}
61
63uint32_t ris::Pool::getAllocCount() {
64 return (allocCount_);
65}
66
68/*
69 * Pass total size required.
70 * Pass flag indicating if zero copy buffers are acceptable
71 */
72ris::FramePtr ris::Pool::acceptReq(uint32_t size, bool zeroCopyEn) {
73 ris::FramePtr ret;
74 uint32_t frSize;
75
76 ret = ris::Frame::create();
77 frSize = 0;
78
79 // Buffers may be smaller than frame
80 while (frSize < size) ret->appendBuffer(allocBuffer(size, &frSize));
81 return (ret);
82}
83
85/*
86 * Called when this instance is marked as owner of a Buffer entity
87 */
88void ris::Pool::retBuffer(uint8_t* data, uint32_t meta, uint32_t rawSize) {
90 std::lock_guard<std::mutex> lock(mtx_);
91
92 if (data != NULL) {
93 if (rawSize == fixedSize_ && poolSize_ > dataQ_.size())
94 dataQ_.push(data);
95 else
96 free(data);
97 }
98 allocBytes_ -= rawSize;
99 allocCount_--;
100}
101
102void ris::Pool::setup_python() {
103#ifndef NO_PYTHON
104 bp::class_<ris::Pool, ris::PoolPtr, boost::noncopyable>("Pool", bp::init<>())
105 .def("getAllocCount", &ris::Pool::getAllocCount)
106 .def("getAllocBytes", &ris::Pool::getAllocBytes)
107 .def("setFixedSize", &ris::Pool::setFixedSize)
108 .def("getFixedSize", &ris::Pool::getFixedSize)
109 .def("setPoolSize", &ris::Pool::setPoolSize)
110 .def("getPoolSize", &ris::Pool::getPoolSize);
111#endif
112}
113
115void ris::Pool::setFixedSize(uint32_t size) {
116 rogue::GilRelease noGil;
117 std::lock_guard<std::mutex> lock(mtx_);
118
119 fixedSize_ = size;
120}
121
123uint32_t ris::Pool::getFixedSize() {
124 return fixedSize_;
125}
126
128void ris::Pool::setPoolSize(uint32_t size) {
129 rogue::GilRelease noGil;
130 std::lock_guard<std::mutex> lock(mtx_);
131
132 poolSize_ = size;
133}
134
136uint32_t ris::Pool::getPoolSize() {
137 return poolSize_;
138}
139
141// Buffer container and raw data should be allocated from shared memory pool
142ris::BufferPtr ris::Pool::allocBuffer(uint32_t size, uint32_t* total) {
143 uint8_t* data;
144 uint32_t bAlloc;
145 uint32_t bSize;
146 uint32_t meta = 0;
147
148 bAlloc = size;
149 bSize = size;
150
151 rogue::GilRelease noGil;
152 std::lock_guard<std::mutex> lock(mtx_);
153 if (fixedSize_ > 0) {
154 bAlloc = fixedSize_;
155 if (bSize > bAlloc) bSize = bAlloc;
156 }
157
158 if (dataQ_.size() > 0) {
159 data = dataQ_.front();
160 dataQ_.pop();
161 } else if ((data = reinterpret_cast<uint8_t*>(malloc(bAlloc))) == NULL) {
162 throw(
163 rogue::GeneralError::create("Pool::allocBuffer", "Failed to allocate buffer with size = %" PRIu32, bAlloc));
164 }
165
166 // Only use lower 24 bits of meta.
167 // Upper 8 bits may have special meaning to sub-class
168 meta = allocMeta_;
169 allocMeta_++;
170 allocMeta_ &= 0xFFFFFF;
171 allocBytes_ += bAlloc;
172 allocCount_++;
173 if (total != NULL) *total += bSize;
174 return (ris::Buffer::create(shared_from_this(), data, meta, bSize, bAlloc));
175}
176
178ris::BufferPtr ris::Pool::createBuffer(void* data, uint32_t meta, uint32_t size, uint32_t alloc) {
179 ris::BufferPtr buff;
180
181 rogue::GilRelease noGil;
182 std::lock_guard<std::mutex> lock(mtx_);
183
184 buff = ris::Buffer::create(shared_from_this(), data, meta, size, alloc);
185
186 allocBytes_ += alloc;
187 allocCount_++;
188 return (buff);
189}
190
192void ris::Pool::decCounter(uint32_t alloc) {
193 rogue::GilRelease noGil;
194 std::lock_guard<std::mutex> lock(mtx_);
195 allocBytes_ -= alloc;
196 allocCount_--;
197}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549