Compute Library
 22.11
MemoryRegion Class Referencefinal

Memory region CPU implementation. More...

#include <MemoryRegion.h>

Collaboration diagram for MemoryRegion:
[legend]

Public Member Functions

 MemoryRegion (size_t size, size_t alignment=0)
 Constructor. More...
 
 MemoryRegion (void *ptr, size_t size)
 
 MemoryRegion (const MemoryRegion &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 MemoryRegion (MemoryRegion &&)=default
 Default move constructor. More...
 
MemoryRegionoperator= (const MemoryRegion &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
MemoryRegionoperator= (MemoryRegion &&)=default
 Default move assignment operator. More...
 
void * buffer () final
 Returns the pointer to the allocated data. More...
 
const void * buffer () const final
 Returns the pointer to the allocated data. More...
 
std::unique_ptr< IMemoryRegionextract_subregion (size_t offset, size_t size) final
 Extract a sub-region from the memory. More...
 
- Public Member Functions inherited from IMemoryRegion
 IMemoryRegion (size_t size)
 Default constructor. More...
 
virtual ~IMemoryRegion ()=default
 Virtual Destructor. More...
 
size_t size () const
 Memory region size accessor. More...
 
void set_size (size_t size)
 Sets size of region. More...
 

Detailed Description

Memory region CPU implementation.

Definition at line 36 of file MemoryRegion.h.

Constructor & Destructor Documentation

◆ MemoryRegion() [1/4]

MemoryRegion ( size_t  size,
size_t  alignment = 0 
)
inline

Constructor.

Parameters
[in]sizeRegion size
[in]alignmentAlignment in bytes of the base pointer. Defaults to 0

Definition at line 44 of file MemoryRegion.h.

Referenced by MemoryRegion::MemoryRegion().

45  : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
46  {
47  if(size != 0)
48  {
49  // Allocate backing memory
50  size_t space = size + alignment;
51  _mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr)
52  {
53  delete[] ptr;
54  });
55  _ptr = _mem.get();
56 
57  // Calculate alignment offset
58  if(alignment != 0)
59  {
60  void *aligned_ptr = _mem.get();
61  std::align(alignment, size, aligned_ptr, space);
62  _ptr = aligned_ptr;
63  }
64  }
65  }
IMemoryRegion(size_t size)
Default constructor.
Definition: IMemoryRegion.h:40
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73

◆ MemoryRegion() [2/4]

MemoryRegion ( void *  ptr,
size_t  size 
)
inline

Definition at line 66 of file MemoryRegion.h.

References MemoryRegion::MemoryRegion(), and MemoryRegion::operator=().

67  : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
68  {
69  if(size != 0)
70  {
71  _ptr = ptr;
72  }
73  }
IMemoryRegion(size_t size)
Default constructor.
Definition: IMemoryRegion.h:40
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73

◆ MemoryRegion() [3/4]

MemoryRegion ( const MemoryRegion )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ MemoryRegion() [4/4]

MemoryRegion ( MemoryRegion &&  )
default

Default move constructor.

Member Function Documentation

◆ buffer() [1/2]

void* buffer ( )
inlinefinalvirtual

Returns the pointer to the allocated data.

Returns
Pointer to the allocated data

Implements IMemoryRegion.

Definition at line 84 of file MemoryRegion.h.

85  {
86  return _ptr;
87  }

◆ buffer() [2/2]

const void* buffer ( ) const
inlinefinalvirtual

Returns the pointer to the allocated data.

Returns
Pointer to the allocated data

Implements IMemoryRegion.

Definition at line 88 of file MemoryRegion.h.

89  {
90  return _ptr;
91  }

◆ extract_subregion()

std::unique_ptr<IMemoryRegion> extract_subregion ( size_t  offset,
size_t  size 
)
inlinefinalvirtual

Extract a sub-region from the memory.

Warning
Ownership is maintained by the parent memory, while a wrapped raw memory region is returned by this function. Thus parent memory should not be released before this.
Parameters
[in]offsetOffset to the region
[in]sizeSize of the region
Returns
A wrapped memory sub-region with no ownership of the underlying memory

Implements IMemoryRegion.

Definition at line 92 of file MemoryRegion.h.

References offset(), and IMemoryRegion::size().

93  {
94  if(_ptr != nullptr && (offset < _size) && (_size - offset >= size))
95  {
96  return std::make_unique<MemoryRegion>(static_cast<uint8_t *>(_ptr) + offset, size);
97  }
98  else
99  {
100  return nullptr;
101  }
102  }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1084
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73

◆ operator=() [1/2]

MemoryRegion& operator= ( const MemoryRegion )
delete

Prevent instances of this class from being copied (As this class contains pointers)

Referenced by MemoryRegion::MemoryRegion().

◆ operator=() [2/2]

MemoryRegion& operator= ( MemoryRegion &&  )
default

Default move assignment operator.


The documentation for this class was generated from the following file: