2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_CMDLINE_MEMORY_REPRESENTATION_H_
18 #define ART_CMDLINE_MEMORY_REPRESENTATION_H_
24 #include "base/bit_utils.h"
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>
32 static_assert(IsPowerOfTwo(kDivisor), "Divisor must be a power of 2");
34 static Memory<kDivisor> FromBytes(size_t bytes) {
35 assert(bytes % kDivisor == 0);
36 return Memory<kDivisor>(bytes);
39 Memory() : Value(0u) {}
40 Memory(size_t value) : Value(value) { // NOLINT [runtime/explicit] [5]
41 assert(value % kDivisor == 0);
43 operator size_t() const { return Value; }
45 size_t ToBytes() const {
49 static const char* Name() {
50 static std::string str;
52 str = "Memory<" + std::to_string(kDivisor) + '>';
61 template <size_t kDivisor>
62 std::ostream& operator<<(std::ostream& stream, Memory<kDivisor> memory) {
63 return stream << memory.Value << '*' << kDivisor;
66 using MemoryKiB = Memory<1024>;
70 #endif // ART_CMDLINE_MEMORY_REPRESENTATION_H_