libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
memspace.h
Go to the documentation of this file.
1
8#pragma once
9
11#include <promeki/core/string.h>
12#include <promeki/core/error.h>
13
15
16struct MemAllocation;
17
30class MemSpace {
31 public:
38
40 struct Ops {
46 bool (*copy)(const MemAllocation &src, const MemAllocation &dst, size_t bytes);
47 Error (*fill)(void *ptr, size_t bytes, char value);
48 };
49
54 MemSpace(ID id = Default) : d(lookup(id)) { }
55
60 const String &name() const { return d->name; }
61
66 ID id() const { return d->id; }
67
74 return d->isHostAccessible(alloc);
75 }
76
83 inline MemAllocation alloc(size_t bytes, size_t align) const;
84
89 inline void release(MemAllocation &alloc) const;
90
101 inline bool copy(const MemAllocation &src, const MemAllocation &dst, size_t bytes) const;
102
110 Error fill(void *ptr, size_t bytes, char value) const {
111 if(ptr == nullptr) return Error::Invalid;
112 return d->fill(ptr, bytes, value);
113 }
114
115 private:
116 const Ops *d = nullptr;
117 static const Ops *lookup(ID id);
118};
119
128 void *ptr = nullptr;
129 size_t size = 0;
130 size_t align = 0;
132 void *priv = nullptr;
133
135 bool isValid() const { return ptr != nullptr; }
136};
137
138inline bool MemSpace::copy(const MemAllocation &src, const MemAllocation &dst, size_t bytes) const {
139 if(src.ptr == nullptr || dst.ptr == nullptr) return false;
140 return d->copy(src, dst, bytes);
141}
142
143inline MemAllocation MemSpace::alloc(size_t bytes, size_t align) const {
145 a.size = bytes;
146 a.align = align;
147 a.ms = *this;
148 d->alloc(a);
149 return a;
150}
151
152inline void MemSpace::release(MemAllocation &alloc) const {
153 if(alloc.ptr == nullptr) return;
154 d->release(alloc);
155 alloc.ptr = nullptr;
156 alloc.priv = nullptr;
157}
158
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ Invalid
Invalid value or argument (EINVAL).
Definition error.h:66
Dynamic array container wrapping std::vector.
Definition list.h:40
Abstraction for memory allocation in different address spaces.
Definition memspace.h:30
MemSpace(ID id=Default)
Constructs a MemSpace for the given memory space ID.
Definition memspace.h:54
ID
Identifies a memory space.
Definition memspace.h:33
@ Default
Alias for System memory.
Definition memspace.h:36
@ SystemSecure
System memory with secure zeroing on free and page locking.
Definition memspace.h:35
bool copy(const MemAllocation &src, const MemAllocation &dst, size_t bytes) const
Copies bytes from a source allocation to a destination allocation.
Definition memspace.h:138
MemAllocation alloc(size_t bytes, size_t align) const
Allocates memory in this memory space.
Definition memspace.h:143
ID id() const
Returns the memory space identifier.
Definition memspace.h:66
Error fill(void *ptr, size_t bytes, char value) const
Fills memory with a byte value.
Definition memspace.h:110
bool isHostAccessible(const MemAllocation &alloc) const
Returns true if the given allocation is directly accessible from the host CPU.
Definition memspace.h:73
const String & name() const
Returns the human-readable name of this memory space.
Definition memspace.h:60
void release(MemAllocation &alloc) const
Releases a previously allocated memory region.
Definition memspace.h:152
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Provides system-level utility functions.
Definition system.h:29
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
Describes a memory allocation from a MemSpace.
Definition memspace.h:127
size_t size
Size of the allocation in bytes.
Definition memspace.h:129
size_t align
Alignment of the allocation in bytes.
Definition memspace.h:130
bool isValid() const
Returns true if this allocation is valid.
Definition memspace.h:135
MemSpace ms
The memory space this was allocated from.
Definition memspace.h:131
void * priv
Private data for the allocator implementation.
Definition memspace.h:132
void * ptr
Pointer to the allocated memory.
Definition memspace.h:128
Function table for memory space operations.
Definition memspace.h:40
void(* alloc)(MemAllocation &alloc)
Allocate memory. Size and align are pre-filled.
Definition memspace.h:44
bool(* copy)(const MemAllocation &src, const MemAllocation &dst, size_t bytes)
Copy bytes from this space to another.
Definition memspace.h:46
String name
Human-readable name of the memory space.
Definition memspace.h:42
Error(* fill)(void *ptr, size_t bytes, char value)
Fill memory with a byte value.
Definition memspace.h:47
ID id
The memory space identifier.
Definition memspace.h:41
void(* release)(MemAllocation &alloc)
Release previously allocated memory.
Definition memspace.h:45
bool(* isHostAccessible)(const MemAllocation &alloc)
Returns true if the allocation is directly accessible from the host CPU.
Definition memspace.h:43