blob: 93387de83c6c63e4126d2cb744d8d7318da84654 [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
20#include <string>
21#include <assert.h>
22#include <ostream>
23#include "utils.h"
24
25namespace art {
26
27// An integral representation of bytes of memory.
28// The underlying runtime size_t value is guaranteed to be a multiple of Divisor.
29template <size_t Divisor = 1024>
30struct Memory {
31 static_assert(IsPowerOfTwo(Divisor), "Divisor must be a power of 2");
32
33 static Memory<Divisor> FromBytes(size_t bytes) {
34 assert(bytes % Divisor == 0);
35 return Memory<Divisor>(bytes);
36 }
37
38 Memory() : Value(0u) {}
39 Memory(size_t value) : Value(value) { // NOLINT [runtime/explicit] [5]
40 assert(value % Divisor == 0);
41 }
42 operator size_t() const { return Value; }
43
44 size_t ToBytes() const {
45 return Value;
46 }
47
48 static constexpr size_t kDivisor = Divisor;
49
50 static const char* Name() {
51 static std::string str;
52 if (str.empty()) {
53 str = "Memory<" + std::to_string(Divisor) + '>';
54 }
55
56 return str.c_str();
57 }
58
59 size_t Value;
60};
61
62template <size_t Divisor>
63std::ostream& operator<<(std::ostream& stream, Memory<Divisor> memory) {
64 return stream << memory.Value << '*' << Divisor;
65}
66
67using MemoryKiB = Memory<1024>;
68
69} // namespace art
70
71#endif // ART_CMDLINE_MEMORY_REPRESENTATION_H_