1/*******************************************************************************
2* Copyright 2016-2018 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#ifndef CPU_MEMORY_HPP
18#define CPU_MEMORY_HPP
19
20#include <assert.h>
21
22#include "c_types_map.hpp"
23#include "memory.hpp"
24#include "memory_desc_wrapper.hpp"
25
26#include "cpu_engine.hpp"
27
28namespace mkldnn {
29namespace impl {
30namespace cpu {
31
32struct cpu_memory_t: public memory_t {
33 cpu_memory_t(cpu_engine_t *engine, const memory_desc_t *md, void *handle)
34 : memory_t(engine, md)
35 , own_data_(handle == MKLDNN_NATIVE_HANDLE_ALLOCATE)
36 , data_((char *)handle) {}
37
38 cpu_memory_t(cpu_engine_t *engine, const memory_desc_t *md)
39 : cpu_memory_t(engine, md, nullptr) {}
40
41 ~cpu_memory_t() { if (own_data_) free(data_); }
42
43 virtual status_t init() override {
44 if (own_data_) {
45 data_ = nullptr;
46 const size_t size = memory_desc_wrapper(this->md()).size();
47 if (size) {
48 data_ = (char *)malloc(size, 64);
49 if (data_ == nullptr)
50 return status::out_of_memory;
51 }
52 }
53 return zero_pad();
54 }
55
56 cpu_engine_t *engine() const { return (cpu_engine_t *)memory_t::engine(); }
57
58 virtual status_t get_data_handle(void **handle) const override {
59 *handle = static_cast<void *>(data_);
60 return status::success;
61 }
62
63 virtual mkldnn::impl::status_t set_data_handle(void *handle) override {
64 if (own_data_) { free(data_); own_data_ = false; }
65 data_ = static_cast<char *>(handle);
66 return zero_pad();
67 }
68
69 virtual mkldnn::impl::status_t zero_pad() const override;
70
71private:
72 bool own_data_;
73 char *data_;
74
75 template <mkldnn::impl::data_type_t>
76 mkldnn::impl::status_t typed_zero_pad() const;
77
78 cpu_memory_t(const cpu_memory_t &) = delete;
79 cpu_memory_t &operator=(const cpu_memory_t &) = delete;
80 cpu_memory_t &operator=(cpu_memory_t &&) = delete;
81};
82
83}
84}
85}
86
87#endif
88
89// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
90