Skip to content

Commit 8789d7e

Browse files
author
Thomas Kiley
committed
fixup clarify the calculation
1 parent f07b437 commit 8789d7e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,14 @@ using max_alloc_sizet = uint64_t;
126126
/// The maximum allocation size is determined by the number of bits that
127127
/// are left in the pointer of width \p pointer_width.
128128
///
129-
/// The offset needs to be able to represent up to the allocation_size, and
130-
/// down to -allocation_size, hence 2^(pointer_width - object_bits - 1)
129+
/// The allocation size cannot exceed the number represented by the (signed)
130+
/// offset, otherwise it would not be possible to store a pointer into a
131+
/// valid bit of memory. Therefore, the max allocation size is
132+
/// 2^(offset_bits - 1), where the offset bits is the number of bits left in the
133+
/// pointer after the object bits.
134+
///
135+
/// The offset must be signed, as a pointer can point to the end of the memory
136+
/// block, and needs to be able to point back to the start.
131137
/// \param pointer_width: The width of the pointer
132138
/// \param object_bits : The number of bits used to represent the ID
133139
/// \return The size in bytes of the maximum allocation supported.
@@ -139,7 +145,7 @@ max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
139145
PRECONDITION(object_bits >= 1);
140146
const auto offset_bits = pointer_width - object_bits;
141147
// We require the offset to be able to express upto allocation_size - 1,
142-
// but also down to -allocation_size + 1, therefore the size is allowable
148+
// but also down to -allocation_size, therefore the size is allowable
143149
// is number of bits, less the signed bit.
144150
const auto bits_for_positive_offset = offset_bits - 1;
145151
PRECONDITION(

0 commit comments

Comments
 (0)