11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
27PROMEKI_NAMESPACE_BEGIN
84 PROMEKI_SHARED_BASE(BufferImpl)
91 using ReleaseCallback = Function<void()>;
93 BufferImpl() =
default;
105 virtual ~BufferImpl() {
106 if (_onRelease) _onRelease();
109 BufferImpl(
const BufferImpl &) =
default;
110 BufferImpl &operator=(
const BufferImpl &) =
default;
115 virtual MemSpace memSpace()
const = 0;
118 virtual size_t allocSize()
const = 0;
121 virtual size_t align()
const = 0;
129 bool isMapped(MemDomain domain)
const {
return mapRefcount(domain.id()) > 0; }
140 virtual void *mappedHostData()
const = 0;
156 virtual BufferRequest mapAcquire(MemDomain domain, MapFlags flags) = 0;
167 virtual BufferRequest mapRelease(MemDomain domain) = 0;
179 virtual Error fill(
char value,
size_t offset,
size_t bytes) = 0;
192 virtual Error copyFromHost(
const void *src,
size_t bytes,
size_t offset) = 0;
197 size_t logicalSize()
const {
return _logicalSize; }
211 void setLogicalSize(
size_t s)
const { _logicalSize = s; }
214 size_t shift()
const {
return _shift; }
222 void setShift(
size_t s) {
238 virtual bool canClone()
const {
return true; }
264 virtual Error seal()
const {
return Error::Ok; }
284 virtual size_t residentBytes()
const {
return allocSize(); }
297 virtual bool isCowBacked()
const {
return false; }
326 void setReleaseCallback(ReleaseCallback cb) { _onRelease = std::move(cb); }
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);
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;
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;
382 void seedMapRefcount(MemDomain::ID domain,
int count) { _mapRefcounts.insert(domain, count); }
384 mutable size_t _logicalSize = 0;
388 mutable Mutex _mapMutex;
389 Map<MemDomain::ID, int> _mapRefcounts;
390 ReleaseCallback _onRelease;