1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_CMDLINE_MEMORY_REPRESENTATION_H_ 18 #define ART_CMDLINE_MEMORY_REPRESENTATION_H_ 19 20 #include <string> 21 #include <assert.h> 22 #include <ostream> 23 24 #include "base/bit_utils.h" 25 26 namespace art { 27 28 // An integral representation of bytes of memory. 29 // The underlying runtime size_t value is guaranteed to be a multiple of Divisor. 30 template <size_t kDivisor = 1024> 31 struct Memory { 32 static_assert(IsPowerOfTwo(kDivisor), "Divisor must be a power of 2"); 33 FromBytesMemory34 static Memory<kDivisor> FromBytes(size_t bytes) { 35 assert(bytes % kDivisor == 0); 36 return Memory<kDivisor>(bytes); 37 } 38 MemoryMemory39 Memory() : Value(0u) {} MemoryMemory40 Memory(size_t value) : Value(value) { // NOLINT [runtime/explicit] [5] 41 assert(value % kDivisor == 0); 42 } size_tMemory43 operator size_t() const { return Value; } 44 ToBytesMemory45 size_t ToBytes() const { 46 return Value; 47 } 48 NameMemory49 static const char* Name() { 50 static std::string str; 51 if (str.empty()) { 52 str = "Memory<" + std::to_string(kDivisor) + '>'; 53 } 54 55 return str.c_str(); 56 } 57 58 size_t Value; 59 }; 60 61 template <size_t kDivisor> 62 std::ostream& operator<<(std::ostream& stream, Memory<kDivisor> memory) { 63 return stream << memory.Value << '*' << kDivisor; 64 } 65 66 using MemoryKiB = Memory<1024>; 67 68 } // namespace art 69 70 #endif // ART_CMDLINE_MEMORY_REPRESENTATION_H_ 71