Skip to content

Truly independent web browser

LibGC: Align cells to at least 16 bytes

Andreas Kling08e49b3b5820
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index 051be85e0d5..e4d17c64473 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -116,7 +116,7 @@ struct CAPI {
.cell_kind_offset = offsetof(Cell, m_cell_kind),
.min_cell_size = HeapBlock::min_possible_cell_size,
.max_cell_size = HeapBlock::BLOCK_SIZE - sizeof(HeapBlock),
- .max_cell_alignment = __BIGGEST_ALIGNMENT__,
+ .max_cell_alignment = max_cell_alignment,
.cell_type_info_size = sizeof(CellTypeInfo),
.weak_impl_pointer_offset = WeakImpl::value_offset(),
.free_cell_next_offset = HeapBlock::freelist_next_offset(),
@@ -311,7 +311,8 @@ GCAllocator* gc_allocator_create(GCCellTypeInfo const* type_info, char const* na
VERIFY(type_info->visit_edges);
VERIFY(type_info->cell_size >= HeapBlock::min_possible_cell_size);
VERIFY(type_info->cell_size <= HeapBlock::BLOCK_SIZE - sizeof(HeapBlock));
- VERIFY(type_info->alignment <= __BIGGEST_ALIGNMENT__);
+ VERIFY(type_info->alignment <= max_cell_alignment);
+ VERIFY(type_info->cell_size % type_info->alignment == 0);
auto* allocator = new CAPICellAllocator(*reinterpret_cast<CellTypeInfo const*>(type_info), StringView { name, name_length });
return reinterpret_cast<GCAllocator*>(allocator);
}
diff --git a/Libraries/LibGC/CellTypeInfo.h b/Libraries/LibGC/CellTypeInfo.h
index 4bb58722d76..1ef7cbee662 100644
--- a/Libraries/LibGC/CellTypeInfo.h
+++ b/Libraries/LibGC/CellTypeInfo.h
@@ -12,6 +12,9 @@
namespace GC {
+// NB: Cells defined outside C++ keep 16-byte values in them, which some platforms align to less than 16 bytes.
+constexpr size_t max_cell_alignment = max<size_t>(16, __BIGGEST_ALIGNMENT__);
+
struct CellTypeInfo {
u32 cell_size { 0 };
u32 alignment { 0 };
@@ -70,7 +73,7 @@ struct CellTypeThunks {
template<typename T>
static constexpr CellTypeInfo info()
{
- static_assert(alignof(T) <= __BIGGEST_ALIGNMENT__);
+ static_assert(alignof(T) <= max_cell_alignment);
return {
.cell_size = static_cast<u32>(sizeof(T)),
.alignment = static_cast<u32>(alignof(T)),
diff --git a/Libraries/LibGC/HeapBlock.h b/Libraries/LibGC/HeapBlock.h
index fa5720c3821..5e734b286e4 100644
--- a/Libraries/LibGC/HeapBlock.h
+++ b/Libraries/LibGC/HeapBlock.h
@@ -172,7 +172,7 @@ private:
// See follow_freelist_link().
FlatPtr m_freelist { 0 };
- alignas(__BIGGEST_ALIGNMENT__) u8 m_storage[];
+ alignas(max_cell_alignment) u8 m_storage[];
public:
static constexpr size_t min_possible_cell_size = sizeof(FreelistEntry);