Skip to content

Commit 5c1440d

Browse files
hannes-steffenhagen-diffbluemartin
authored andcommitted
Add helper class for displaying memory quantities
1 parent de23b27 commit 5c1440d

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ SRC = allocate_objects.cpp \
4545
mathematical_expr.cpp \
4646
mathematical_types.cpp \
4747
memory_info.cpp \
48+
memory_units.cpp \
4849
merge_irep.cpp \
4950
message.cpp \
5051
mp_arith.cpp \

src/util/memory_units.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*******************************************************************\
2+
3+
Module: Memory units
4+
5+
Author: Hannes Steffenhagen
6+
7+
\*******************************************************************/
8+
9+
#include "memory_units.h"
10+
11+
#include <sstream>
12+
13+
memory_sizet::memory_sizet() : bytes(0)
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+
26+
memory_sizet &memory_sizet::operator=(const memory_sizet &other)
27+
{
28+
bytes = other.bytes;
29+
return *this;
30+
}
31+
32+
memory_sizet &memory_sizet::operator=(memory_sizet &&other) noexcept
33+
{
34+
bytes = other.bytes;
35+
return *this;
36+
}
37+
38+
memory_sizet memory_sizet::from_bytes(std::size_t bytes)
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+
106+
memory_sizet &memory_sizet::operator+=(const memory_sizet &other)
107+
{
108+
bytes += other.bytes;
109+
return *this;
110+
}
111+
112+
memory_sizet memory_sizet::operator+(const memory_sizet &other) const
113+
{
114+
return memory_sizet(*this) += other;
115+
}

src/util/memory_units.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************\
2+
3+
Module: Memory units
4+
5+
Author: Hannes Steffenhagen
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_MEMORY_UNITS_H
10+
#define CPROVER_UTIL_MEMORY_UNITS_H
11+
12+
#include <cstddef>
13+
#include <string>
14+
15+
class memory_sizet
16+
{
17+
public:
18+
static memory_sizet from_bytes(std::size_t bytes);
19+
20+
memory_sizet();
21+
memory_sizet(const memory_sizet &);
22+
memory_sizet(memory_sizet &&);
23+
24+
memory_sizet &operator=(const memory_sizet &);
25+
memory_sizet &operator=(memory_sizet &&) noexcept;
26+
27+
memory_sizet &operator+=(const memory_sizet &);
28+
memory_sizet operator+(const memory_sizet &) const;
29+
30+
std::size_t get_bytes() const;
31+
std::size_t get_kibibytes() const;
32+
std::size_t get_mebibytes() const;
33+
std::size_t get_gibibytes() const;
34+
std::string to_string() const;
35+
36+
static const char *si_byte_symbol;
37+
static const char *si_kibibyte_symbol;
38+
static const char *si_mebibyte_symbol;
39+
static const char *si_gibibyte_symbol;
40+
41+
private:
42+
std::size_t bytes;
43+
explicit memory_sizet(std::size_t bytes);
44+
};
45+
46+
#endif // CPROVER_UTIL_MEMORY_UNITS_H

0 commit comments

Comments
 (0)