1
// Copyright (C) 2018-2019 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
6
* @brief The header file defines utility PreAllocator class
7
* @file ie_pre_allocator.hpp
11
#include <details/ie_exception.hpp>
12
#include "ie_allocator.hpp"
15
namespace InferenceEngine {
18
* @brief This is a helper class to wrap external memory
20
class PreAllocator : public IAllocator {
25
PreAllocator(void *ptr, size_t bytes_size)
26
: _actualData(ptr), _sizeInBytes(bytes_size) {}
28
* @brief Locks a handle to heap memory accessible by any memory manipulation routines
29
* @return The generic pointer to a memory buffer
31
void * lock(void * handle, LockOp = LOCK_FOR_WRITE) noexcept override {
32
if (handle != _actualData) {
38
* @brief The PreAllocator class does not utilize this function
40
void unlock(void *) noexcept override {} // NOLINT
43
* @brief Returns a pointer to preallocated memory
44
* @param size Size in bytes
45
* @return A handle to the preallocated memory or nullptr
47
void * alloc(size_t size) noexcept override {
48
if (size <= _sizeInBytes) {
55
* @brief The PreAllocator class cannot release the handle
58
bool free(void *) noexcept override { // NOLINT
63
* @brief Deletes current allocator.
64
* Can be used if a shared_from_irelease pointer is used
66
void Release() noexcept override {
71
virtual ~PreAllocator() = default;
75
* @brief Creates a special allocator that only works on external memory
76
* @param ptr Pointer to preallocated memory
77
* @param size Number of elements allocated
78
* @return A new allocator
81
std::shared_ptr<IAllocator> make_pre_allocator(T *ptr, size_t size) {
82
return shared_from_irelease(new PreAllocator(ptr, size * sizeof(T)));
85
} // namespace details
86
} // namespace InferenceEngine
b'\\ No newline at end of file'