libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
bufferimpl.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstddef>
14#include <cstdint>
15#include <promeki/namespace.h>
16#include <promeki/sharedptr.h>
17#include <promeki/error.h>
18#include <promeki/mutex.h>
19#include <promeki/map.h>
20#include <promeki/memspace.h>
21#include <promeki/memdomain.h>
24
25PROMEKI_NAMESPACE_BEGIN
26
70class BufferImpl {
71 public:
82 PROMEKI_SHARED_BASE(BufferImpl)
83
84 BufferImpl() = default;
85 virtual ~BufferImpl() = default;
86
87 BufferImpl(const BufferImpl &) = default;
88 BufferImpl &operator=(const BufferImpl &) = default;
89
90 // ---- Identity / sizing ----
91
93 virtual MemSpace memSpace() const = 0;
94
96 virtual size_t allocSize() const = 0;
97
99 virtual size_t align() const = 0;
100
101 // ---- Map state ----
102
107 bool isMapped(MemDomain domain) const { return mapRefcount(domain.id()) > 0; }
108
118 virtual void *mappedHostData() const = 0;
119
134 virtual BufferRequest mapAcquire(MemDomain domain, MapFlags flags) = 0;
135
145 virtual BufferRequest mapRelease(MemDomain domain) = 0;
146
147 // ---- Mutation ----
148
157 virtual Error fill(char value, size_t offset, size_t bytes) = 0;
158
170 virtual Error copyFromHost(const void *src, size_t bytes, size_t offset) = 0;
171
172 // ---- Logical size + data shift (shared across handles) ----
173
175 size_t logicalSize() const { return _logicalSize; }
176
189 void setLogicalSize(size_t s) const { _logicalSize = s; }
190
192 size_t shift() const { return _shift; }
193
200 void setShift(size_t s) {
201 _shift = s;
202 _logicalSize = 0;
203 }
204
205 // ---- Cloning ----
206
216 virtual bool canClone() const { return true; }
217
218 // ---- Producer→post-producer transition (seal) ----
219
242 virtual Error seal() const { return Error::Ok; }
243
262 virtual size_t residentBytes() const { return allocSize(); }
263
275 virtual bool isCowBacked() const { return false; }
276
277 protected:
285 int incrementMapRefcount(MemDomain::ID domain) {
286 Mutex::Locker lock(_mapMutex);
287 auto it = _mapRefcounts.find(domain);
288 if (it == _mapRefcounts.end()) {
289 _mapRefcounts.insert(domain, 1);
290 return 1;
291 }
292 return ++it->second;
293 }
294
302 int decrementMapRefcount(MemDomain::ID domain) {
303 Mutex::Locker lock(_mapMutex);
304 auto it = _mapRefcounts.find(domain);
305 if (it == _mapRefcounts.end()) return -1;
306 if (it->second == 0) return -1;
307 return --it->second;
308 }
309
315 int mapRefcount(MemDomain::ID domain) const {
316 Mutex::Locker lock(_mapMutex);
317 auto it = _mapRefcounts.find(domain);
318 return (it == _mapRefcounts.end()) ? 0 : it->second;
319 }
320
331 void seedMapRefcount(MemDomain::ID domain, int count) { _mapRefcounts.insert(domain, count); }
332
333 mutable size_t _logicalSize = 0;
334 size_t _shift = 0;
335
336 private:
337 mutable Mutex _mapMutex;
338 Map<MemDomain::ID, int> _mapRefcounts;
339};
340
341PROMEKI_NAMESPACE_END
342
343#endif // PROMEKI_ENABLE_CORE