@@ -1880,20 +1880,33 @@ namespace hud
18801880
18811881 constexpr usize allocate_control_and_slot (usize max_slot_count) noexcept
18821882 {
1883+ /* *
1884+ * Allocation layout is :
1885+ *
1886+ * | aligned_control_size | slots_size |
1887+ * | control_size | |
1888+ * | controls | slot padding | slots |
1889+ * ^ ^
1890+ * control_ptr_ slot_ptr_
1891+ *
1892+ */
1893+
18831894 const usize control_size {control_size_for_max_count (max_slot_count)};
1884- const usize slot_size {max_slot_count * sizeof (slot_type)};
1895+ const usize slots_size {max_slot_count * sizeof (slot_type)};
18851896 const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
18861897
18871898 if (hud::is_constant_evaluated ())
18881899 {
18891900 control_ptr_ = allocator_.template allocate <control_type>(aligned_control_size).data ();
1890- slot_ptr_ = allocator_.template allocate <slot_type>(slot_size ).data ();
1901+ slot_ptr_ = allocator_.template allocate <slot_type>(slots_size ).data ();
18911902 }
18921903 else
18931904 {
1894- const usize aligned_allocation_size {aligned_control_size + slot_size};
1905+
1906+ const usize aligned_allocation_size {aligned_control_size + slots_size};
18951907 control_ptr_ = allocator_.template allocate <control_type>(aligned_allocation_size).data ();
1896- slot_ptr_ = reinterpret_cast <slot_type *>(hud::memory::align_address (reinterpret_cast <const uptr>(control_ptr_ + control_size), alignof (slot_type)));
1908+ slot_ptr_ = reinterpret_cast <slot_type *>(control_ptr_ + aligned_control_size, alignof (slot_type));
1909+ hud::check ((u8 *)slot_ptr_ - (u8 *)control_ptr_ == aligned_control_size);
18971910 hud::check (hud::memory::is_pointer_aligned (slot_ptr_, alignof (slot_type)));
18981911 }
18991912 return control_size;
@@ -1904,19 +1917,19 @@ namespace hud
19041917 if (max_slot_count > 0 )
19051918 {
19061919 const usize control_size {control_size_for_max_count (max_slot_count)};
1907- const usize slot_size {max_slot_count * sizeof (slot_type)};
1920+ const usize slots_size {max_slot_count * sizeof (slot_type)};
19081921 const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
19091922
19101923 // In a constant-evaluated context, bit_cast cannot be used with pointers
19111924 // and allocation is done in two separate allocation
19121925 if (hud::is_constant_evaluated ())
19131926 {
19141927 allocator_.template free <control_type>({control_ptr, aligned_control_size});
1915- allocator_.template free <slot_type>({slot_ptr, slot_size });
1928+ allocator_.template free <slot_type>({slot_ptr, slots_size });
19161929 }
19171930 else
19181931 {
1919- const usize aligned_allocation_size {aligned_control_size + slot_size };
1932+ const usize aligned_allocation_size {aligned_control_size + slots_size };
19201933 allocator_.template free <control_type>({control_ptr, aligned_allocation_size});
19211934 }
19221935 }
0 commit comments