1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16 #ifndef ECMASCRIPT_MEM_MEM_COMMON_H 17 #define ECMASCRIPT_MEM_MEM_COMMON_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 namespace panda::ecmascript { 23 template <class T> ToUintPtr(T * val)24inline uintptr_t ToUintPtr(T *val) 25 { 26 return reinterpret_cast<uintptr_t>(val); 27 } 28 ToUintPtr(std::nullptr_t)29inline uintptr_t ToUintPtr(std::nullptr_t) 30 { 31 return reinterpret_cast<uintptr_t>(nullptr); 32 } 33 ToVoidPtr(uintptr_t val)34inline void *ToVoidPtr(uintptr_t val) 35 { 36 return reinterpret_cast<void *>(val); 37 } 38 39 /* 40 uint64_t return type usage in memory literals for giving 41 compile-time error in case of integer overflow 42 */ 43 constexpr uint64_t SHIFT_KB = 10ULL; 44 constexpr uint64_t SHIFT_MB = 20ULL; 45 constexpr uint64_t SHIFT_GB = 30ULL; 46 47 constexpr uint64_t operator"" _KB(long double count) 48 { 49 return count * (1ULL << SHIFT_KB); 50 } 51 52 // NOLINTNEXTLINE(google-runtime-int) 53 constexpr uint64_t operator"" _KB(unsigned long long count) 54 { 55 return count * (1ULL << SHIFT_KB); 56 } 57 58 constexpr uint64_t operator"" _MB(long double count) 59 { 60 return count * (1ULL << SHIFT_MB); 61 } 62 63 // NOLINTNEXTLINE(google-runtime-int) 64 constexpr uint64_t operator"" _MB(unsigned long long count) 65 { 66 return count * (1ULL << SHIFT_MB); 67 } 68 69 constexpr uint64_t operator"" _GB(long double count) 70 { 71 return count * (1ULL << SHIFT_GB); 72 } 73 74 // NOLINTNEXTLINE(google-runtime-int) 75 constexpr uint64_t operator"" _GB(unsigned long long count) 76 { 77 return count * (1ULL << SHIFT_GB); 78 } 79 } // namespace panda::ecmascript 80 81 #endif // ECMASCRIPT_MEM_MEM_COMMON_H 82