Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / memory_impl.h
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19 #include "api/CPP/memory.hpp"
20
21 #include "api_impl.h"
22 #include "engine_impl.h"
23 #include "refcounted_obj.h"
24
25 namespace cldnn
26 {
27
28 struct memory_impl : refcounted_obj<memory_impl>
29 {
30     memory_impl(const engine_impl::ptr& engine, layout layout, bool reused=false)
31         : _engine(engine)
32         , _layout(layout)
33         , _reused(reused)
34     {}
35
36     virtual ~memory_impl()
37     {
38         if (_engine != nullptr && !_reused) 
39         {
40             _engine->get_memory_pool().subtract_memory_used(_layout.bytes_count());
41         }
42     }
43     virtual void* lock() = 0;
44     virtual void unlock() = 0;
45     virtual void fill(unsigned char pattern, event_impl::ptr ev) = 0;
46     size_t size() const { return _layout.bytes_count(); }
47     virtual bool is_allocated_by(const engine_impl& engine) const { return &engine == _engine.get(); }
48     const refcounted_obj_ptr<engine_impl>& get_engine() const { return _engine; }
49     const layout& get_layout() const { return _layout; }
50 protected:
51     const engine_impl::ptr _engine;
52     const layout _layout;
53 private:
54     bool _reused;
55 };
56
57 struct simple_attached_memory : memory_impl
58 {
59     simple_attached_memory(layout layout, void* pointer)
60         : memory_impl(nullptr, layout), _pointer(pointer)
61     {
62     }
63
64     void* lock() override { return _pointer; }
65     void unlock() override {}
66     void fill(unsigned char, event_impl::ptr) override {}
67 private:
68     void* _pointer;
69 };
70
71 template <class T>
72 struct mem_lock
73 {
74     mem_lock(memory_impl::ptr mem)
75         : mem(mem), ptr(reinterpret_cast<T*>(mem->lock()))
76     {
77     }
78
79     mem_lock(memory_impl& mem)
80         : mem_lock(&mem)
81     {}
82
83     ~mem_lock()
84     {
85         ptr = nullptr;
86         mem->unlock();
87     }
88
89     size_t size() const { return mem->size() / sizeof(T); }
90
91 #if defined(_SECURE_SCL) && (_SECURE_SCL > 0)
92     auto begin() & { return stdext::make_checked_array_iterator(ptr, size()); }
93     auto end() & { return stdext::make_checked_array_iterator(ptr, size(), size()); }
94 #else
95     T* begin() & { return ptr; }
96     T* end() & { return ptr + size(); }
97 #endif
98
99     T* data() const { return ptr; }
100
101 private:
102     memory_impl::ptr mem;
103     T* ptr;
104 };
105
106 }
107
108 API_CAST(::cldnn_memory, cldnn::memory_impl)