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 dataQ_.pop();
55 }
56}
57
59uint32_t ris::Pool::getAllocBytes() {
60 return (allocBytes_);
61}
62
64uint32_t ris::Pool::getAllocCount() {
65 return (allocCount_);
66}
67
69/*
70 * Pass total size required.
71 * Pass flag indicating if zero copy buffers are acceptable
72 */
73ris::FramePtr ris::Pool::acceptReq(uint32_t size, bool zeroCopyEn) {
74 ris::FramePtr ret;
75 uint32_t frSize;
76
77 ret = ris::Frame::create();
78 frSize = 0;
79
80 // Buffers may be smaller than frame
81 while (frSize < size) ret->appendBuffer(allocBuffer(size, &frSize));
82 return (ret);
83}
84
86/*
87 * Called when this instance is marked as owner of a Buffer entity
88 */
89void ris::Pool::retBuffer(uint8_t* data, uint32_t meta, uint32_t rawSize) {
91 std::lock_guard<std::mutex> lock(mtx_);
92
93 if (data != NULL) {
94 if (rawSize == fixedSize_ && poolSize_ > dataQ_.size())
95 dataQ_.push(data);
96 else
97 free(data);
98 }
99 allocBytes_ -= rawSize;
100 allocCount_--;
101}
102
103void ris::Pool::setup_python() {
104#ifndef NO_PYTHON
105 bp::class_<ris::Pool, ris::PoolPtr, boost::noncopyable>("Pool", bp::init<>())
106 .def("getAllocCount", &ris::Pool::getAllocCount)
107 .def("getAllocBytes", &ris::Pool::getAllocBytes)
108 .def("setFixedSize", &ris::Pool::setFixedSize)
109 .def("getFixedSize", &ris::Pool::getFixedSize)
110 .def("setPoolSize", &ris::Pool::setPoolSize)
111 .def("getPoolSize", &ris::Pool::getPoolSize);
112#endif
113}
114
116void ris::Pool::setFixedSize(uint32_t size) {
117 rogue::GilRelease noGil;
118 std::lock_guard<std::mutex> lock(mtx_);
119
120 fixedSize_ = size;
121}
122
124uint32_t ris::Pool::getFixedSize() {
125 return fixedSize_;
126}
127
129void ris::Pool::setPoolSize(uint32_t size) {
130 rogue::GilRelease noGil;
131 std::lock_guard<std::mutex> lock(mtx_);
132
133 poolSize_ = size;
134}
135
137uint32_t ris::Pool::getPoolSize() {
138 return poolSize_;
139}
140
142// Buffer container and raw data should be allocated from shared memory pool
143ris::BufferPtr ris::Pool::allocBuffer(uint32_t size, uint32_t* total) {
144 uint8_t* data;
145 uint32_t bAlloc;
146 uint32_t bSize;
147 uint32_t meta = 0;
148
149 bAlloc = size;
150 bSize = size;
151
152 rogue::GilRelease noGil;
153 std::lock_guard<std::mutex> lock(mtx_);
154 if (fixedSize_ > 0) {
155 bAlloc = fixedSize_;
156 if (bSize > bAlloc) bSize = bAlloc;
157 }
158
159 if (dataQ_.size() > 0) {
160 data = dataQ_.front();
161 dataQ_.pop();
162 } else if ((data = reinterpret_cast<uint8_t*>(malloc(bAlloc))) == NULL) {
163 throw(
164 rogue::GeneralError::create("Pool::allocBuffer", "Failed to allocate buffer with size = %" PRIu32, bAlloc));
165 }
166
167 // Only use lower 24 bits of meta.
168 // Upper 8 bits may have special meaning to sub-class
169 meta = allocMeta_;
170 allocMeta_++;
171 allocMeta_ &= 0xFFFFFF;
172 allocBytes_ += bAlloc;
173 allocCount_++;
174 if (total != NULL) *total += bSize;
175 return (ris::Buffer::create(shared_from_this(), data, meta, bSize, bAlloc));
176}
177
179ris::BufferPtr ris::Pool::createBuffer(void* data, uint32_t meta, uint32_t size, uint32_t alloc) {
180 ris::BufferPtr buff;
181
182 rogue::GilRelease noGil;
183 std::lock_guard<std::mutex> lock(mtx_);
184
185 buff = ris::Buffer::create(shared_from_this(), data, meta, size, alloc);
186
187 allocBytes_ += alloc;
188 allocCount_++;
189 return (buff);
190}
191
193void ris::Pool::decCounter(uint32_t alloc) {
194 rogue::GilRelease noGil;
195 std::lock_guard<std::mutex> lock(mtx_);
196 allocBytes_ -= alloc;
197 allocCount_--;
198}
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