Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 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 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 21 | namespace 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 |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 28 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 29 | Memset(T *object, int val) |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 30 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 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"); |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 32 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 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 |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 41 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 42 | memset(object, 0, sizeof(T)); |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 43 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 44 | #if defined(__GNUC__) && (__GNUC__ >= 8) |
| 45 | # pragma GCC diagnostic pop |
| 46 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | } |
| 48 | }; |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 49 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 50 | } // namespace sw |
Ben Clayton | eea9d35 | 2019-08-29 01:05:14 +0100 | [diff] [blame] | 51 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 52 | #endif // sw_Memset_hpp |