Skip to content

Commit 13ac3b9

Browse files
author
Thomas Kiley
committed
Use size_t since this is the type stored anyway
1 parent 871f7eb commit 13ac3b9

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ static std::string architecture_string(T value, const char *s)
122122
std::string(s) + "=" + std::to_string(value) + ";\n";
123123
}
124124

125-
using max_alloc_sizet = uint64_t;
126125
/// The maximum allocation size is determined by the number of bits that
127126
/// are left in the pointer of width \p pointer_width.
128127
///
@@ -131,7 +130,7 @@ using max_alloc_sizet = uint64_t;
131130
/// \param pointer_width: The width of the pointer
132131
/// \param object_bits : The number of bits used to represent the ID
133132
/// \return The size in bytes of the maximum allocation supported.
134-
static max_alloc_sizet
133+
static size_t
135134
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
136135
{
137136
PRECONDITION(pointer_width >= 1);
@@ -142,9 +141,8 @@ max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
142141
// but also down to -allocation_size + 1, therefore the size is allowable
143142
// is number of bits, less the signed bit.
144143
const auto bits_for_positive_offset = offset_bits - 1;
145-
PRECONDITION(
146-
bits_for_positive_offset < std::numeric_limits<max_alloc_sizet>::digits);
147-
return ((max_alloc_sizet)1) << (max_alloc_sizet)bits_for_positive_offset;
144+
PRECONDITION(bits_for_positive_offset < std::numeric_limits<size_t>::digits);
145+
return ((size_t)1) << (size_t)bits_for_positive_offset;
148146
}
149147

150148
void ansi_c_internal_additions(std::string &code)

unit/ansi-c/max_malloc_size.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,32 @@ TEST_CASE(
3333
REQUIRE_THROWS_AS(max_malloc_size(0, 0), invariant_failedt);
3434
}
3535

36-
SECTION("Max allocation size overflow")
37-
{
38-
REQUIRE_THROWS_AS(max_malloc_size(128, 63), invariant_failedt);
39-
}
40-
4136
SECTION("Valid allocation size configurations")
4237
{
4338
// Here we use 4 bits for the object ID, leaving 3 for the offset
4439
REQUIRE(max_malloc_size(8, 4) == 8);
45-
REQUIRE(max_malloc_size(128, 64) == 9223372036854775808ull /*2^63*/);
40+
}
41+
if(std::numeric_limits<size_t>::digits == 64)
42+
{
43+
SECTION("Overflow edge cases")
44+
{
45+
REQUIRE_THROWS_AS(max_malloc_size(128, 63), invariant_failedt);
46+
REQUIRE(max_malloc_size(128, 64) == 9223372036854775808ull /*2^63*/);
47+
}
48+
}
49+
else if(std::numeric_limits<size_t>::digits == 32)
50+
{
51+
SECTION("Overflow edge cases")
52+
{
53+
REQUIRE_THROWS_AS(max_malloc_size(64, 31), invariant_failedt);
54+
REQUIRE(max_malloc_size(64, 32) == 2147483648ull /*2^31*/);
55+
}
56+
}
57+
else
58+
{
59+
INFO(
60+
"Unit test not supported on platform with "
61+
<< std::numeric_limits<size_t>::digits << " digits");
62+
REQUIRE(false);
4663
}
4764
}

0 commit comments

Comments
 (0)