BlackBox.h (1944B)
1 // Copyright 2015 Google Inc. 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 GTEST_BLACKBOX_H 16 #define GTEST_BLACKBOX_H 17 18 #include "mozilla/Attributes.h" 19 #if defined(_MSC_VER) 20 # include <intrin.h> 21 #endif // _MSC_VER 22 23 namespace mozilla { 24 25 #if defined(_MSC_VER) 26 27 char volatile* UseCharPointer(char volatile*); 28 29 MOZ_ALWAYS_INLINE_EVEN_DEBUG void* BlackBoxVoidPtr(void* aPtr) { 30 aPtr = const_cast<char*>(UseCharPointer(reinterpret_cast<char*>(aPtr))); 31 _ReadWriteBarrier(); 32 return aPtr; 33 } 34 35 #else 36 37 // See: https://youtu.be/nXaxk27zwlk?t=2441 38 MOZ_ALWAYS_INLINE_EVEN_DEBUG void* BlackBoxVoidPtr(void* aPtr) { 39 // "g" is what we want here, but the comment in the Google 40 // benchmark code suggests that GCC likes "i,r,m" better. 41 // However, on Mozilla try server i,r,m breaks GCC but g 42 // works in GCC, so using g for both clang and GCC. 43 // godbolt.org indicates that g works already in GCC 4.9, 44 // which is the oldest GCC we support at the time of this 45 // code landing. godbolt.org suggests that this clearly 46 // works is LLVM 5, but it's unclear if this inhibits 47 // all relevant optimizations properly on earlier LLVM. 48 asm volatile("" : "+g"(aPtr) : "g"(aPtr) : "memory"); 49 return aPtr; 50 } 51 52 #endif // _MSC_VER 53 54 template <class T> 55 MOZ_ALWAYS_INLINE_EVEN_DEBUG T* BlackBox(T* aPtr) { 56 return static_cast<T*>(BlackBoxVoidPtr(aPtr)); 57 } 58 59 } // namespace mozilla 60 61 #endif // GTEST_BLACKBOX_H