1/*******************************************************************************
2 * Copyright 2020-2022 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 COMMON_MEMORY_DEBUG_HPP
18#define COMMON_MEMORY_DEBUG_HPP
19
20#ifndef DNNL_MEM_DEBUG_PROTECT_SIZE
21#define DNNL_MEM_DEBUG_PROTECT_SIZE 1
22#endif
23
24#define DNNL_MEM_DEBUG_UNDERFLOW 0
25#define DNNL_MEM_DEBUG_OVERFLOW 1
26
27#include <stddef.h>
28
29#include "c_types_map.hpp"
30#include "oneapi/dnnl/dnnl_config.h"
31
32namespace dnnl {
33namespace impl {
34namespace memory_debug {
35
36// Static inline for optimization purposes when memory_debug is disabled
37static inline bool is_mem_debug() {
38#ifndef DNNL_ENABLE_MEM_DEBUG
39 return false;
40#else
41 return true;
42#endif
43}
44
45// Static inline for optimization purposes when memory_debug is disabled
46static inline bool is_mem_debug_overflow() {
47// Note: not re-using `is_mem_debug` due to `-Wundef` compilation option.
48#if defined(DNNL_ENABLE_MEM_DEBUG)
49#if (DNNL_ENABLE_MEM_DEBUG == DNNL_MEM_DEBUG_UNDERFLOW)
50 return false;
51#else // DNNL_ENABLE_MEM_DEBUG == DNNL_MEM_DEBUG_OVERFLOW
52 // Default to DNNL_MEM_DEBUG_OVERFLOW as buffer overflows are a
53 // more common memory error.
54 return true;
55#endif
56#else // defined(DNNL_ENABLE_MEM_DEBUG)
57 return false;
58#endif
59}
60
61// Export the memory_debug::malloc symbol to for external linkage.
62#ifdef DNNL_ENABLE_MEM_DEBUG
63void DNNL_API *malloc(size_t size, int alignment);
64#else
65void *malloc(size_t size, int alignment);
66#endif
67void free(void *p);
68
69size_t protect_size();
70void protect_buffer(void *addr, size_t size, engine_kind_t engine_kind);
71void unprotect_buffer(const void *addr, size_t size, engine_kind_t engine_kind);
72
73} // namespace memory_debug
74} // namespace impl
75} // namespace dnnl
76#endif
77
78// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s
79