Skip to content

Commit 9438b49

Browse files
author
Thomas Kiley
committed
Allow offset of a single bit
This is valid - the offset can be 0 for a pointer pointing at the start of a single byte allocation.
1 parent dee0d0f commit 9438b49

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static max_alloc_sizet
141141
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
142142
{
143143
PRECONDITION(pointer_width >= 1);
144-
PRECONDITION(object_bits < pointer_width - 1);
144+
PRECONDITION(object_bits < pointer_width);
145145
PRECONDITION(object_bits >= 1);
146146
const auto offset_bits = pointer_width - object_bits;
147147
// We require the offset to be able to express upto allocation_size - 1,

unit/ansi-c/max_malloc_size.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ TEST_CASE(
1818

1919
SECTION("Too many bits for pointer ID")
2020
{
21-
REQUIRE_THROWS_AS(max_malloc_size(4, 3), invariant_failedt);
2221
REQUIRE_THROWS_AS(max_malloc_size(4, 4), invariant_failedt);
2322
REQUIRE_THROWS_AS(max_malloc_size(4, 5), invariant_failedt);
2423
}
@@ -40,6 +39,9 @@ TEST_CASE(
4039

4140
SECTION("Valid allocation size configurations")
4241
{
42+
// The one bit offset can be used to store 0, or -1, so we can allocate
43+
// a single bit
44+
REQUIRE(max_malloc_size(4, 3) == 1);
4345
// Here we use 4 bits for the object ID, leaving 3 for the offset
4446
REQUIRE(max_malloc_size(8, 4) == 8);
4547
REQUIRE(max_malloc_size(128, 64) == 9223372036854775808ull /*2^63*/);

0 commit comments

Comments
 (0)