ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnkit::support::onnx::Allocator Class Referencefinal

#include <Allocator.h>

Collaboration diagram for nnkit::support::onnx::Allocator:

Public Member Functions

 Allocator (void)
 
 ~Allocator (void)
 
void * Alloc (size_t size)
 
void Free (void *p)
 
const OrtAllocatorInfo * Info (void) const
 
void LeakCheck (void)
 
 Allocator (const Allocator &)=delete
 
Allocatoroperator= (const Allocator &)=delete
 

Detailed Description

Definition at line 31 of file Allocator.h.

Constructor & Destructor Documentation

◆ Allocator() [1/2]

nnkit::support::onnx::Allocator::Allocator ( void  )

Definition at line 29 of file Allocator.cpp.

30{
31 OrtAllocator::version = ORT_API_VERSION;
32 OrtAllocator::Alloc = [](OrtAllocator *this_, size_t size) {
33 return static_cast<Allocator *>(this_)->Alloc(size);
34 };
35 OrtAllocator::Free = [](OrtAllocator *this_, void *p) {
36 static_cast<Allocator *>(this_)->Free(p);
37 };
38 OrtAllocator::Info = [](const OrtAllocator *this_) {
39 return static_cast<const Allocator *>(this_)->Info();
40 };
41
42 Status status;
43 status = OrtCreateCpuAllocatorInfo(OrtDeviceAllocator, OrtMemTypeDefault, &_cpu_allocator_info);
44 status.throwOnError();
45}
void * Alloc(size_t size)
Definition Allocator.cpp:49
const OrtAllocatorInfo * Info(void) const
Definition Allocator.cpp:76
int32_t size[5]
Definition Slice.cpp:35

References Alloc(), Free(), Info(), size, and nnkit::support::onnx::Status::throwOnError().

◆ ~Allocator()

nnkit::support::onnx::Allocator::~Allocator ( void  )

Definition at line 47 of file Allocator.cpp.

47{ OrtReleaseAllocatorInfo(_cpu_allocator_info); }

◆ Allocator() [2/2]

nnkit::support::onnx::Allocator::Allocator ( const Allocator )
delete

Member Function Documentation

◆ Alloc()

void * nnkit::support::onnx::Allocator::Alloc ( size_t  size)

Definition at line 49 of file Allocator.cpp.

50{
51 // NOTE The extra_len is added to check resource leak.
52 //
53 // This Alloc function will allocate the given size with extra_len.
54 // The first extra_len will save the allocated memory size and
55 // the user will use address from the allocated memory plus extra_len.
56 // The size value that saved in extra_len is used to Free function
57 // to check resource leak. The size value uses in _memory_inuse.
58 constexpr size_t extra_len = sizeof(size_t);
59 _memory_inuse.fetch_add(size += extra_len);
60 void *p = ::malloc(size);
61 *(size_t *)p = size;
62 return (char *)p + extra_len;
63}

References size.

Referenced by Allocator().

◆ Free()

void nnkit::support::onnx::Allocator::Free ( void *  p)

Definition at line 65 of file Allocator.cpp.

66{
67 constexpr size_t extra_len = sizeof(size_t);
68 if (!p)
69 return;
70 p = (char *)p - extra_len;
71 size_t len = *(size_t *)p;
72 _memory_inuse.fetch_sub(len);
73 return ::free(p);
74}

Referenced by Allocator().

◆ Info()

const OrtAllocatorInfo * nnkit::support::onnx::Allocator::Info ( void  ) const

Definition at line 76 of file Allocator.cpp.

76{ return _cpu_allocator_info; }

Referenced by Allocator().

◆ LeakCheck()

void nnkit::support::onnx::Allocator::LeakCheck ( void  )

Definition at line 78 of file Allocator.cpp.

79{
80 if (_memory_inuse.load())
81 {
82 throw std::runtime_error{"memory leak!!!"};
83 }
84}

◆ operator=()

Allocator & nnkit::support::onnx::Allocator::operator= ( const Allocator )
delete

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