CBMC
memory_units.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Memory units
4 
5 Author: Hannes Steffenhagen
6 
7 \*******************************************************************/
8 
9 #include "memory_units.h"
10 
11 #include <sstream>
12 
14 {
15 }
16 memory_sizet::memory_sizet(std::size_t bytes) : bytes(bytes)
17 {
18 }
19 memory_sizet::memory_sizet(const memory_sizet &other) : bytes(other.bytes)
20 {
21 }
22 memory_sizet::memory_sizet(memory_sizet &&other) : bytes(other.bytes)
23 {
24 }
25 
27 {
28  bytes = other.bytes;
29  return *this;
30 }
31 
33 {
34  bytes = other.bytes;
35  return *this;
36 }
37 
39 {
40  return memory_sizet(bytes);
41 }
42 
43 std::size_t memory_sizet::get_bytes() const
44 {
45  return bytes;
46 }
47 
48 std::size_t memory_sizet::get_kibibytes() const
49 {
50  return bytes / 1024;
51 }
52 
53 std::size_t memory_sizet::get_mebibytes() const
54 {
55  return bytes / (1024 * 1024);
56 }
57 
58 std::size_t memory_sizet::get_gibibytes() const
59 {
60  return bytes / (1024 * 1024 * 1024);
61 }
62 
63 std::string memory_sizet::to_string() const
64 {
65  std::size_t remainder = get_bytes();
66  std::ostringstream out;
67  const std::size_t gib = remainder / (1024 * 1024 * 1024);
68  remainder -= gib * 1024 * 1024 * 1024;
69  if(gib > 0)
70  {
71  out << gib << si_gibibyte_symbol;
72  }
73  const std::size_t mib = remainder / (1024 * 1024);
74  remainder -= mib * 1024 * 1024;
75  if(mib > 0)
76  {
77  if(gib > 0)
78  {
79  out << ' ';
80  }
81  out << mib << si_mebibyte_symbol;
82  }
83  const std::size_t kib = remainder / 1024;
84  remainder -= kib * 1024;
85  if(kib > 0)
86  {
87  if(mib > 0 || gib > 0)
88  {
89  out << ' ';
90  }
91  out << kib << si_kibibyte_symbol;
92  }
93  if(gib > 0 || mib > 0 || kib > 0)
94  {
95  out << ' ';
96  }
97  out << remainder << si_byte_symbol;
98  return out.str();
99 }
100 
101 const char *memory_sizet::si_byte_symbol = "B";
102 const char *memory_sizet::si_kibibyte_symbol = "KiB";
103 const char *memory_sizet::si_mebibyte_symbol = "MiB";
104 const char *memory_sizet::si_gibibyte_symbol = "GiB";
105 
107 {
108  bytes += other.bytes;
109  return *this;
110 }
111 
113 {
114  return memory_sizet(*this) += other;
115 }
std::size_t get_bytes() const
std::size_t get_kibibytes() const
memory_sizet & operator=(const memory_sizet &)
static const char * si_gibibyte_symbol
Definition: memory_units.h:39
memory_sizet operator+(const memory_sizet &) const
std::string to_string() const
static const char * si_kibibyte_symbol
Definition: memory_units.h:37
std::size_t bytes
Definition: memory_units.h:42
std::size_t get_gibibytes() const
static const char * si_mebibyte_symbol
Definition: memory_units.h:38
static memory_sizet from_bytes(std::size_t bytes)
std::size_t get_mebibytes() const
memory_sizet & operator+=(const memory_sizet &)
static const char * si_byte_symbol
Definition: memory_units.h:36
double remainder(double x, double y)
Definition: math.c:2396