1/*******************************************************************************
2 * Copyright 2020 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#include "scratchpad_debug.hpp"
18
19#include "engine.hpp"
20#include "memory_debug.hpp"
21#include "memory_tracking.hpp"
22
23namespace dnnl {
24namespace impl {
25namespace scratchpad_debug {
26
27void protect_scratchpad_buffer(void *scratchpad_ptr, engine_kind_t engine_kind,
28 const memory_tracking::registry_t &registry) {
29 if (scratchpad_ptr == nullptr) return;
30
31 auto end = registry.end(scratchpad_ptr);
32 auto curr = registry.begin(scratchpad_ptr);
33 for (; curr != end; curr++) {
34 std::pair<void *, size_t> data_range = *curr;
35 memory_debug::protect_buffer(
36 data_range.first, data_range.second, engine_kind);
37 }
38}
39void unprotect_scratchpad_buffer(const void *scratchpad_ptr,
40 engine_kind_t engine_kind,
41 const memory_tracking::registry_t &registry) {
42 if (scratchpad_ptr == nullptr) return;
43
44 auto end = registry.cend(scratchpad_ptr);
45 auto curr = registry.cbegin(scratchpad_ptr);
46 for (; curr != end; curr++) {
47 std::pair<const void *, size_t> data_range = *curr;
48 memory_debug::unprotect_buffer(
49 data_range.first, data_range.second, engine_kind);
50 }
51}
52
53void protect_scratchpad_buffer(const memory_storage_t *storage,
54 const memory_tracking::registry_t &registry) {
55 if (storage != nullptr)
56 protect_scratchpad_buffer(
57 storage->data_handle(), storage->engine()->kind(), registry);
58}
59void unprotect_scratchpad_buffer(const memory_storage_t *storage,
60 const memory_tracking::registry_t &registry) {
61 if (storage != nullptr)
62 unprotect_scratchpad_buffer(
63 storage->data_handle(), storage->engine()->kind(), registry);
64}
65} // namespace scratchpad_debug
66} // namespace impl
67} // namespace dnnl
68