blob: 8db68bc9a443b2f41ce91949edb1335ed76dd434 [file] [log] [blame]
Igor Murashkinaaebaa02015-01-26 10:55:53 -08001/*
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
Igor Murashkinaaebaa02015-01-26 10:55:53 -080020#include <assert.h>
21#include <ostream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include <string>
Vladimir Marko80afd022015-05-19 18:08:00 +010023
24#include "base/bit_utils.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080025
26namespace 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.
Vladimir Marko80afd022015-05-19 18:08:00 +010030template <size_t kDivisor = 1024>
Igor Murashkinaaebaa02015-01-26 10:55:53 -080031struct Memory {
Vladimir Marko80afd022015-05-19 18:08:00 +010032 static_assert(IsPowerOfTwo(kDivisor), "Divisor must be a power of 2");
Igor Murashkinaaebaa02015-01-26 10:55:53 -080033
Vladimir Marko80afd022015-05-19 18:08:00 +010034 static Memory<kDivisor> FromBytes(size_t bytes) {
35 assert(bytes % kDivisor == 0);
36 return Memory<kDivisor>(bytes);
Igor Murashkinaaebaa02015-01-26 10:55:53 -080037 }
38
39 Memory() : Value(0u) {}
40 Memory(size_t value) : Value(value) { // NOLINT [runtime/explicit] [5]
Vladimir Marko80afd022015-05-19 18:08:00 +010041 assert(value % kDivisor == 0);
Igor Murashkinaaebaa02015-01-26 10:55:53 -080042 }
43 operator size_t() const { return Value; }
44
45 size_t ToBytes() const {
46 return Value;
47 }
48
Igor Murashkinaaebaa02015-01-26 10:55:53 -080049 static const char* Name() {
50 static std::string str;
51 if (str.empty()) {
Vladimir Marko80afd022015-05-19 18:08:00 +010052 str = "Memory<" + std::to_string(kDivisor) + '>';
Igor Murashkinaaebaa02015-01-26 10:55:53 -080053 }
54
55 return str.c_str();
56 }
57
58 size_t Value;
59};
60
Vladimir Marko80afd022015-05-19 18:08:00 +010061template <size_t kDivisor>
62std::ostream& operator<<(std::ostream& stream, Memory<kDivisor> memory) {
63 return stream << memory.Value << '*' << kDivisor;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080064}
65
66using MemoryKiB = Memory<1024>;
67
68} // namespace art
69
70#endif // ART_CMDLINE_MEMORY_REPRESENTATION_H_