class nbla::Memory
-
class Memory
Memory interface class.
The Memory class is designed to be managed by a Allocator instance. This is extended to implement a device memory class which is responsible for allocating/freeing a device memory block, and splitting into/merging blocks.
Subclassed by nbla::CpuMemory
Public Functions
-
NBLA_API Memory(size_t bytes, const string &device_id)
Constructor will be called from a inherited class constructor to set bytes and device_id.
-
virtual NBLA_API ~Memory()
A derived class must implements destructor.
This destructor does nothing.
The memory allocated in Memory::alloc_impl function of a derived class
must be freed in destuctor of the derived class. It is recommended to insert assertion ensuring that this->prev() returns false to check runtime fatal issue.
Note
If ptr_ is nullptr, a Memory has previously been merged to another, and is not retaining a memory block that should be freed.
-
inline size_t bytes() const
Get memory block size in bytes.
-
inline size_t requested_bytes() const
Get requested allocation memory size in bytes.
-
inline string device_id() const
Get device id as a string.
-
inline void *pointer()
Get a raw pointer retaining a memory block instance.
In CpuMemory realization, it is a raw memory block allocated by malloc.
-
inline const void *const_pointer() const
Get a raw pointer retaining a memory block instance.
In CpuMemory realization, it is a raw memory block allocated by malloc.
-
inline bool locked() const
This returns true if this Memory instance is in use.
The lock state is managed by AllocatorMemory instance in a RAII way. Allocator::alloc function returns a AllocatorMemory instance retaining a Memory instance with lock. At the end of life of the AllocatorMemory instance, the lock is released to inform whether the memory instance can be merged with a consecutive (next_ or prev_) memory block at Memory::try_merge function.
-
inline bool disabled()
This returns whether the Memory instance was disabled by merging to another memory block.
-
inline Memory *next() const
Returns the next memory block which has previously been split from an originally allocated memory block.
-
inline void lock()
Get a lock of this Memory instance when it’s used to prevent merging with another.
The lock is obtained by AllocatorMemory in its initialization.
-
inline void release()
Release a lock when it’s not used.
The lock is released by AllocatorMemory in its destructor.
-
inline VecPhysicalMemoryPtr &get_physical_memory()
Get physical memory as reference.
-
inline void append_physical_memories(VecPhysicalMemoryPtr &p_mems)
Append physical memories.
-
inline void clear_physical_memory()
Clear physical memory.
-
void alloc()
Allocate memory by Memory::alloc_impl implemented in an implementation class.
This should be called before using this instance, and is designed to be called via Allocator::alloc_retry which should be called in Allocator::alloc_impl in an implementation class.
-
size_t bytes_active()
Returns number of bytes of memory block this owns.
This returns 0 if this is not originally allocated memory instance, i.e., divided by another memory.
-
shared_ptr<Memory> divide(size_t second_start)
This splits memory at an offset specified by second_start, and returns a second block.
A first block is retained in this instance.
-
void try_merge(Memory *from)
Merge another memory block specified by from if it’s possible.
Merging is performed if the following conditions are met;
`from` is a valid pointer.
`from` is not locked.
`from` is next or previous of this.
When merged, `from` will be disabled by Memory::disable.
-
void bind()
Bind physical memories on virtual address.
This method can be executed only if memory_type_ == MemType::Virtual.
-
void unbind()
Unbind physical memories from corresponding virtual address, and return physical memory list as vector.
This method can be executed only if memory_type_ == MemType::Virtual.
-
bool grow(VecPhysicalMemoryPtr &p_mems)
Glow virtual memory from already allocated virtual address.
-
inline virtual DeviceMemoryState get_device_memory_state()
Get device memory state.
In default this function does noting and return DeviceMemoryState::Unlocked.
-
inline virtual void lock_device_memory()
Request device memory state to `request`.
`request` must be DeviceMemoryState. In default this function does nothing.
-
NBLA_API Memory(size_t bytes, const string &device_id)