Skip to content

Commit bc5c540

Browse files
committed
Explicitly describe padding algorithm in pseudocode
1 parent 5426423 commit bc5c540

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/type-layout.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ Here is this algorithm described in pseudocode.
197197

198198
<!-- ignore: pseudocode -->
199199
```rust,ignore
200+
fn padding_needed_for(offset: usize, alignment: usize) -> usize {
201+
let misalignment = offset % alignment;
202+
if misalignment > 0 {
203+
// round up to next multiple of `alignment`
204+
alignment - misalignment
205+
} else {
206+
// already a multiple of `alignment`
207+
0
208+
}
209+
}
210+
200211
struct.alignment = struct.fields().map(|field| field.alignment).max();
201212
202213
let current_offset = 0;
@@ -205,10 +216,6 @@ for field in struct.fields_in_declaration_order() {
205216
// Increase the current offset so that it's a multiple of the alignment
206217
// of this field. For the first field, this will always be zero.
207218
// The skipped bytes are called padding bytes.
208-
//
209-
// padding_needed_for() is equivalent to
210-
// std::alloc::Layout::padding_needed_for(), but takes an integer rather
211-
// than a Layout as the first argument.
212219
current_offset += padding_needed_for(current_offset, field.alignment);
213220
214221
struct[field].offset = current_offset;

0 commit comments

Comments
 (0)