88#include < cstddef>
99#include < cstdint>
1010#include < cstring>
11+ #include < limits>
1112
1213#include " common/wrappers/likely.h"
1314#include " runtime-common/core/allocator/runtime-allocator.h"
@@ -24,10 +25,12 @@ constexpr uint64_t MALLOC_REPLACER_MAX_ALLOC = 0xFFFFFF00; // 4GiB
2425namespace details {
2526struct control_block {
2627private:
27- static inline constexpr auto SIZE_FIELD_BITSIZE{48 };
28- static inline constexpr auto BASE_OFFSET_FIELD_BITSIZE{16 };
29- static inline constexpr uint64_t BLOCK_SIZE_MASK{(1UL << SIZE_FIELD_BITSIZE) - 1 };
30- static inline constexpr uint64_t BASE_OFFSET_MASK{(1UL << BASE_OFFSET_FIELD_BITSIZE) - 1 };
28+ static constexpr auto SIZE_FIELD_BITSIZE{48 };
29+ static constexpr auto BASE_OFFSET_FIELD_BITSIZE{16 };
30+ static constexpr uint64_t BLOCK_SIZE_MASK{(1UL << SIZE_FIELD_BITSIZE) - 1 };
31+ static constexpr uint64_t BASE_OFFSET_MASK{(1UL << BASE_OFFSET_FIELD_BITSIZE) - 1 };
32+
33+ static_assert (SIZE_FIELD_BITSIZE + BASE_OFFSET_FIELD_BITSIZE == std::numeric_limits<uint64_t >::digits);
3134
3235public:
3336 static constexpr uint64_t max_size () noexcept {
@@ -59,7 +62,7 @@ static_assert(sizeof(control_block) == sizeof(uint64_t), "Control block's size m
5962} // namespace details
6063
6164inline void * alloc (size_t size) noexcept {
62- const size_t cb_size{sizeof (details::control_block)};
65+ constexpr size_t cb_size{sizeof (details::control_block)};
6366 if (unlikely (size > std::min (details::control_block::max_size (), MALLOC_REPLACER_MAX_ALLOC) - cb_size)) {
6467 php_warning (" attempt to allocate too much memory by malloc replacer, requested : %lu" , size);
6568 return nullptr ;
@@ -70,21 +73,21 @@ inline void* alloc(size_t size) noexcept {
7073 php_warning (" not enough script memory to allocate, requested : %lu, actual requested: %lu" , size, total_size);
7174 return base;
7275 }
73- *(reinterpret_cast <uint64_t *>(base)) = details::control_block{.size = total_size, .base_offset = cb_size}.raw ();
74- return reinterpret_cast <void *>(reinterpret_cast < uint64_t >(base) + cb_size); // NOLINT
76+ *(static_cast <uint64_t *>(base)) = details::control_block{.size = total_size, .base_offset = cb_size}.raw ();
77+ return static_cast <void *>(static_cast < uint8_t * >(base) + cb_size);
7578}
7679
7780inline void * alloc_aligned (size_t size, std::align_val_t alignment) noexcept {
7881 // Check that provided alignment is power of two
7982 const size_t align{static_cast <uint64_t >(alignment)};
8083 if (unlikely (align == 0 || !details::is_power_of_2 (align) || align >= details::control_block::max_alignment ())) {
81- php_warning (" allocation alignment have to be non-zero power of two and not greater than %" PRIi64 " , got : %lu" , details::control_block::max_alignment (),
84+ php_warning (" allocation alignment have to be non-zero power of two and not greater than %" PRIu64 " , got : %lu" , details::control_block::max_alignment (),
8285 align);
8386 return nullptr ;
8487 }
8588
8689 // Check that memory is enough
87- const size_t cb_size{sizeof (details::control_block)};
90+ constexpr size_t cb_size{sizeof (details::control_block)};
8891 if (unlikely (size > std::min (details::control_block::max_size (), MALLOC_REPLACER_MAX_ALLOC) - (align - 1 ) - cb_size)) {
8992 php_warning (" attempt to allocate too much memory by malloc replacer, requested : %lu" , size);
9093 return nullptr ;
@@ -123,7 +126,7 @@ inline void free(void* ptr) noexcept {
123126 return ;
124127 }
125128
126- const size_t cb_size{sizeof (details::control_block)};
129+ constexpr size_t cb_size{sizeof (details::control_block)};
127130 const auto mem{reinterpret_cast <uint64_t >(ptr)};
128131
129132 const auto cb{details::control_block::from_raw (*reinterpret_cast <uint64_t *>(mem - cb_size))}; // NOLINT
@@ -142,7 +145,7 @@ inline void* realloc(void* ptr, size_t new_size) noexcept {
142145 return nullptr ;
143146 }
144147
145- const size_t cb_size{sizeof (details::control_block)};
148+ constexpr size_t cb_size{sizeof (details::control_block)};
146149 const auto mem{reinterpret_cast <uint64_t >(ptr)};
147150
148151 const auto cb{details::control_block::from_raw (*reinterpret_cast <uint64_t *>(mem - cb_size))}; // NOLINT
0 commit comments