1// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef sw_Memset_hpp
16#define sw_Memset_hpp
17
18#include <cstring>
19#include <type_traits>
20
21namespace sw
22{
23 // Helper class for clearing the memory of objects at construction.
24 // Useful as the first base class of cache keys which may contain padding
25 // bytes or bits otherwise left uninitialized.
26 template<class T>
27 struct Memset
28 {
29 Memset(T *object, int val)
30 {
31 static_assert(std::is_base_of<Memset<T>, T>::value, "Memset<T> must only clear the memory of a type of which it is a base class");
32
33 // GCC 8+ warns that
34 // "‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘T’;
35 // use assignment or value-initialization instead [-Werror=class-memaccess]"
36 // This is benign iff it happens before any of the base or member constructrs are called.
37 #if defined(__GNUC__) && (__GNUC__ >= 8)
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wclass-memaccess"
40 #endif
41
42 memset(object, 0, sizeof(T));
43
44 #if defined(__GNUC__) && (__GNUC__ >= 8)
45 #pragma GCC diagnostic pop
46 #endif
47 }
48 };
49
50}
51
52#endif // sw_Memset_hpp