Skip to content

Commit 19c9368

Browse files
committed
improve ptr_unalign check
1 parent 49600a1 commit 19c9368

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

include/mimalloc/internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ mi_decl_restrict void* _mi_theap_malloc_sampled(mi_theap_t* theap, size_t req_si
349349
size_t _mi_theap_update_sample_rate(mi_theap_t* theap);
350350

351351
mi_decl_restrict void* _mi_theap_malloc_profiled(mi_theap_t* theap, size_t size, uint64_t requested_since_last_sample, bool zero, mi_page_t** ppage) mi_attr_noexcept;
352-
void _mi_page_profile_free(mi_page_t* page, mi_block_t* block, void* p);
352+
void _mi_page_profile_on_free(mi_page_t* page, mi_block_t* block, void* p);
353353
size_t _mi_theap_set_profile_sample_rate(mi_theap_t* theap, size_t sample_rate);
354354

355355

@@ -1516,7 +1516,7 @@ static mi_decl_forceinline void* _mi_memzero_block(mi_block_t* dst, size_t bsize
15161516
// fast memzero for small sizes based on overlapping writes (and assuming non-zero size_t-multiple size, and size_t aligned)
15171517
// assumes constant memset(p,0,N) gets optimized to fast simd stores by the compiler
15181518
// (compile with -DMI_USE_MEMZERO16X=0 to disable this)
1519-
#if !defined(MI_USE_MEMZERO16X) || (MI_USE_MEMZERO16X != 0) // 16x MI_SIZE_SIZE
1519+
#if !defined(MI_USE_MEMZERO16X) || (MI_USE_MEMZERO16X != 0) // 16x MI_SIZE_SIZE (128 bytes on 64-bit)
15201520
if mi_unlikely(bsize < 2*MI_SIZE_SIZE) { // bsize < 16 (8)
15211521
*((size_t*)dst) = 0;
15221522
return dst;

src/free.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static inline void mi_free_block_mt(mi_page_t* page, mi_block_t* block, bool was
9797
// note: this can be called from `mi_free_generic_mt` where a non-owning thread accesses the
9898
// `page_woffset` and `block_size` fields; however these are constant and the page won't be
9999
// deallocated (as the block we are freeing keeps it alive) and thus safe to read concurrently.
100-
mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p) {
100+
static inline mi_block_t* mi_page_ptr_unalign_ex(const mi_page_t* page, const void* p, size_t* poffset) {
101101
mi_assert_internal(page!=NULL && p!=NULL);
102102

103103
const size_t diff = (uint8_t*)p - mi_page_start(page);
@@ -106,9 +106,14 @@ mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p) {
106106
if mi_unlikely(!_mi_is_power_of_two(block_size)) {
107107
adjust = diff % block_size;
108108
}
109+
if (poffset!=NULL) { *poffset = adjust; }
109110
return (mi_block_t*)((uintptr_t)p - adjust);
110111
}
111112

113+
mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p) {
114+
return mi_page_ptr_unalign_ex(page,p,NULL);
115+
}
116+
112117
static inline mi_block_t* mi_validate_block_from_ptr( const mi_page_t* page, const void* p ) {
113118
mi_assert(_mi_page_ptr_unalign(page,p) == (mi_block_t*)p); // should never be an interior pointer
114119
#if MI_SECURE > 0
@@ -126,13 +131,13 @@ static inline mi_block_t* mi_page_ptr_block_check(mi_page_t* page, void* p, bool
126131
return mi_validate_block_from_ptr(page,p);
127132
}
128133
else {
129-
mi_block_t* const block = _mi_page_ptr_unalign(page,p);
134+
size_t offset;
135+
mi_block_t* const block = mi_page_ptr_unalign_ex(page,p,&offset);
130136
#if MI_GUARDED || MI_PROFILE
131-
const size_t offset = (uint8_t*)p - (uint8_t*)block;
132137
if (offset >= sizeof(mi_block_t)) {
133138
#if MI_PROFILE
134139
if (block->next == MI_BLOCK_TAG_PROFILED) {
135-
_mi_page_profile_free(page,block,p);
140+
_mi_page_profile_on_free(page,block,p);
136141
}
137142
else
138143
#endif
@@ -141,8 +146,9 @@ static inline mi_block_t* mi_page_ptr_block_check(mi_page_t* page, void* p, bool
141146
_mi_page_block_unguard(page, block, p);
142147
*was_guarded = true;
143148
}
149+
else
144150
#else
145-
{ }
151+
{ /* nothing */}
146152
#endif
147153
}
148154
#endif

src/sample-profile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ mi_decl_noinline mi_decl_restrict void* _mi_theap_malloc_profiled(mi_theap_t* th
179179
return p;
180180
}
181181

182-
void _mi_page_profile_free(mi_page_t* page, mi_block_t* block, void* p) {
182+
void _mi_page_profile_on_free(mi_page_t* page, mi_block_t* block, void* p) {
183183
mi_assert_internal(mi_block_ptr_is_sampled(block,p));
184184

185185
// get the heap and profiler

0 commit comments

Comments
 (0)