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