Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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> |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | |
| 24 | #include "base/bit_utils.h" |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | template <size_t kDivisor = 1024> |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 31 | struct Memory { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 32 | static_assert(IsPowerOfTwo(kDivisor), "Divisor must be a power of 2"); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 33 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | static Memory<kDivisor> FromBytes(size_t bytes) { |
| 35 | assert(bytes % kDivisor == 0); |
| 36 | return Memory<kDivisor>(bytes); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Memory() : Value(0u) {} |
| 40 | Memory(size_t value) : Value(value) { // NOLINT [runtime/explicit] [5] |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 41 | assert(value % kDivisor == 0); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 42 | } |
| 43 | operator size_t() const { return Value; } |
| 44 | |
| 45 | size_t ToBytes() const { |
| 46 | return Value; |
| 47 | } |
| 48 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 49 | static const char* Name() { |
| 50 | static std::string str; |
| 51 | if (str.empty()) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 52 | str = "Memory<" + std::to_string(kDivisor) + '>'; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return str.c_str(); |
| 56 | } |
| 57 | |
| 58 | size_t Value; |
| 59 | }; |
| 60 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 61 | template <size_t kDivisor> |
| 62 | std::ostream& operator<<(std::ostream& stream, Memory<kDivisor> memory) { |
| 63 | return stream << memory.Value << '*' << kDivisor; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | using MemoryKiB = Memory<1024>; |
| 67 | |
| 68 | } // namespace art |
| 69 | |
| 70 | #endif // ART_CMDLINE_MEMORY_REPRESENTATION_H_ |