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 <utility>
16#include <promeki/namespace.h>
17#include <promeki/sharedptr.h>
18#include <promeki/error.h>
19#include <promeki/function.h>
20#include <promeki/mutex.h>
21#include <promeki/map.h>
22#include <promeki/memspace.h>
23#include <promeki/memdomain.h>
26
27PROMEKI_NAMESPACE_BEGIN
28
72class BufferImpl {
73 public:
84 PROMEKI_SHARED_BASE(BufferImpl)
85
86
91 using ReleaseCallback = Function<void()>;
92
93 BufferImpl() = default;
94
105 virtual ~BufferImpl() {
106 if (_onRelease) _onRelease();
107 }
108
109 BufferImpl(const BufferImpl &) = default;
110 BufferImpl &operator=(const BufferImpl &) = default;
111
112 // ---- Identity / sizing ----
113
115 virtual MemSpace memSpace() const = 0;
116
118 virtual size_t allocSize() const = 0;
119
121 virtual size_t align() const = 0;
122
123 // ---- Map state ----
124
129 bool isMapped(MemDomain domain) const { return mapRefcount(domain.id()) > 0; }
130
140 virtual void *mappedHostData() const = 0;
141
156 virtual BufferRequest mapAcquire(MemDomain domain, MapFlags flags) = 0;
157
167 virtual BufferRequest mapRelease(MemDomain domain) = 0;
168
169 // ---- Mutation ----
170
179 virtual Error fill(char value, size_t offset, size_t bytes) = 0;
180
192 virtual Error copyFromHost(const void *src, size_t bytes, size_t offset) = 0;
193
194 // ---- Logical size + data shift (shared across handles) ----
195
197 size_t logicalSize() const { return _logicalSize; }
198
211 void setLogicalSize(size_t s) const { _logicalSize = s; }
212
214 size_t shift() const { return _shift; }
215
222 void setShift(size_t s) {
223 _shift = s;
224 _logicalSize = 0;
225 }
226
227 // ---- Cloning ----
228
238 virtual bool canClone() const { return true; }
239
240 // ---- Producer→post-producer transition (seal) ----
241
264 virtual Error seal() const { return Error::Ok; }
265
284 virtual size_t residentBytes() const { return allocSize(); }
285
297 virtual bool isCowBacked() const { return false; }
298
299 // ---- Release notification ----
300
326 void setReleaseCallback(ReleaseCallback cb) { _onRelease = std::move(cb); }
327
328 protected:
336 int incrementMapRefcount(MemDomain::ID domain) {
337 Mutex::Locker lock(_mapMutex);
338 auto it = _mapRefcounts.find(domain);
339 if (it == _mapRefcounts.end()) {
340 _mapRefcounts.insert(domain, 1);
341 return 1;
342 }
343 return ++it->second;
344 }
345
353 int decrementMapRefcount(MemDomain::ID domain) {
354 Mutex::Locker lock(_mapMutex);
355 auto it = _mapRefcounts.find(domain);
356 if (it == _mapRefcounts.end()) return -1;
357 if (it->second == 0) return -1;
358 return --it->second;
359 }
360
366 int mapRefcount(MemDomain::ID domain) const {
367 Mutex::Locker lock(_mapMutex);
368 auto it = _mapRefcounts.find(domain);
369 return (it == _mapRefcounts.end()) ? 0 : it->second;
370 }
371
382 void seedMapRefcount(MemDomain::ID domain, int count) { _mapRefcounts.insert(domain, count); }
383
384 mutable size_t _logicalSize = 0;
385 size_t _shift = 0;
386
387 private:
388 mutable Mutex _mapMutex;
389 Map<MemDomain::ID, int> _mapRefcounts;
390 ReleaseCallback _onRelease;
391};
392
393PROMEKI_NAMESPACE_END
394
395#endif // PROMEKI_ENABLE_CORE