• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 MEM_MANAGER_H
17 #define MEM_MANAGER_H
18 
19 #include <cstdint>
20 #include <cstddef>
21 
22 namespace libabckit {
23 
24 class MemManager {
25 public:
26     [[maybe_unused]] static MemManager *Initialize(size_t compilerMemSize = 0)
27     {
28         MemManager::compilerMemSize_ = compilerMemSize;
29         static auto memManager = libabckit::MemManager();
30         if (!isInitialized_) {
31             memManager.InitializeMemory();
32         }
33         return &memManager;
34     };
35 
36     static void Finalize();
37 
38 private:
39     void InitializeMemory();
40     static size_t compilerMemSize_;
41     static bool isInitialized_;
42 };
43 
44 constexpr uint64_t SHIFT_KB = 10ULL;
45 constexpr uint64_t SHIFT_MB = 20ULL;
46 constexpr uint64_t SHIFT_GB = 30ULL;
47 
48 constexpr uint64_t operator"" _KB(long double count)
49 {
50     return static_cast<uint64_t>(count * (1ULL << SHIFT_KB));
51 }
52 
53 // NOLINTNEXTLINE(google-runtime-int)
54 constexpr uint64_t operator"" _KB(unsigned long long count)
55 {
56     return count * (1ULL << SHIFT_KB);
57 }
58 
59 constexpr uint64_t operator"" _MB(long double count)
60 {
61     return static_cast<uint64_t>(count * (1ULL << SHIFT_MB));
62 }
63 
64 // NOLINTNEXTLINE(google-runtime-int)
65 constexpr uint64_t operator"" _MB(unsigned long long count)
66 {
67     return count * (1ULL << SHIFT_MB);
68 }
69 
70 constexpr uint64_t operator"" _GB(long double count)
71 {
72     return static_cast<uint64_t>(count * (1ULL << SHIFT_GB));
73 }
74 
75 // CC-OFFNXT(WordsTool.95) sensitive word conflict
76 // NOLINTNEXTLINE(google-runtime-int)
77 constexpr uint64_t operator"" _GB(unsigned long long count)
78 {
79     return count * (1ULL << SHIFT_GB);
80 }
81 
82 }  // namespace libabckit
83 
84 #endif
85