Skip to content

Commit 968e247

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Pull size alignment to AValue
Summary: Align object size of alignment of allocation to `AValue`. Simplify code code and make sure alignment is not overlooked. Reviewed By: ndmitchell Differential Revision: D41111517 fbshipit-source-id: add1472ccc0393054109a93570d9855f86f58439
1 parent 9df3cc5 commit 968e247

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

starlark/src/values/layout/avalue.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ pub(crate) trait AValue<'v>: StarlarkValueDyn<'v> + Sized {
163163
Self::offset_of_extra() % mem::align_of::<Self::ExtraElem>() == 0,
164164
"extra must be aligned"
165165
);
166-
cmp::max(
166+
let size = cmp::max(
167167
mem::size_of::<Self::StarlarkValue>(),
168168
// Content is not necessarily aligned to end of `A`.
169169
Self::offset_of_extra() + (mem::size_of::<Self::ExtraElem>() * extra_len),
170-
)
170+
);
171+
AValueHeader::align_up(size)
171172
}
172173

173174
unsafe fn heap_freeze(

starlark/src/values/layout/heap/arena.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ impl<'c> Iterator for ChunkIter<'c> {
132132
let or_forward = &*(self.chunk.as_ptr() as *const AValueOrForward);
133133
let n = or_forward.alloc_size();
134134
debug_assert!(n <= self.chunk.len());
135-
let n = AValueHeader::align_up(n);
136-
let n = cmp::min(n, self.chunk.len());
137135
self.chunk = self.chunk.split_at(n).1;
138136
Some(or_forward)
139137
}
@@ -174,6 +172,7 @@ impl Arena {
174172
mem::size_of::<AValueHeader>() + T::memory_size_for_extra_len(extra_len),
175173
MIN_ALLOC,
176174
);
175+
debug_assert!(size % AValueHeader::ALIGN == 0);
177176
let layout = Layout::from_size_align(size, mem::align_of::<AValueHeader>()).unwrap();
178177
let p = bump.alloc_layout(layout).as_ptr();
179178
unsafe {

starlark/src/values/layout/heap/repr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ impl AValueOrForward {
179179
}
180180
};
181181
let n = mem::size_of::<AValueHeader>() + n;
182-
cmp::max(n, MIN_ALLOC)
182+
let size = cmp::max(n, MIN_ALLOC);
183+
debug_assert!(size % AValueHeader::ALIGN == 0);
184+
size
183185
}
184186
}
185187

starlark/src/values/layout/vtable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ pub(crate) struct AValueDyn<'v> {
223223
impl<'v> AValueDyn<'v> {
224224
#[inline]
225225
pub(crate) fn memory_size(self) -> usize {
226-
(self.vtable.memory_size)(self.value as *const ())
226+
let size = (self.vtable.memory_size)(self.value as *const ());
227+
debug_assert!(size % AValueHeader::ALIGN == 0);
228+
size
227229
}
228230

229231
pub(crate) fn total_memory(self) -> usize {

0 commit comments

Comments
 (0)