2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "api/CPP/memory.hpp"
22 #include "engine_impl.h"
23 #include "refcounted_obj.h"
28 struct memory_impl : refcounted_obj<memory_impl>
30 memory_impl(const engine_impl::ptr& engine, layout layout, bool reused=false)
36 virtual ~memory_impl()
38 if (_engine != nullptr && !_reused)
40 _engine->get_memory_pool().subtract_memory_used(_layout.bytes_count());
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; }
51 const engine_impl::ptr _engine;
57 struct simple_attached_memory : memory_impl
59 simple_attached_memory(layout layout, void* pointer)
60 : memory_impl(nullptr, layout), _pointer(pointer)
64 void* lock() override { return _pointer; }
65 void unlock() override {}
66 void fill(unsigned char, event_impl::ptr) override {}
74 mem_lock(memory_impl::ptr mem)
75 : mem(mem), ptr(reinterpret_cast<T*>(mem->lock()))
79 mem_lock(memory_impl& mem)
89 size_t size() const { return mem->size() / sizeof(T); }
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()); }
95 T* begin() & { return ptr; }
96 T* end() & { return ptr + size(); }
99 T* data() const { return ptr; }
102 memory_impl::ptr mem;
108 API_CAST(::cldnn_memory, cldnn::memory_impl)