Skip to content

Commit 12d94f1

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 4fe6842 commit 12d94f1

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
@@ -17,7 +17,6 @@ TEST_CASE(
1717

1818
SECTION("Too many bits for pointer ID")
1919
{
20-
REQUIRE_THROWS_AS(max_malloc_size(4, 3), invariant_failedt);
2120
REQUIRE_THROWS_AS(max_malloc_size(4, 4), invariant_failedt);
2221
REQUIRE_THROWS_AS(max_malloc_size(4, 5), invariant_failedt);
2322
}
@@ -39,6 +38,9 @@ TEST_CASE(
3938

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

0 commit comments

Comments
 (0)