Skip to content

Commit 4d0a2f6

Browse files
committed
Provide is_zend_ptr() function to check if a pointer lays in Zend MM heap.
1 parent 05f3706 commit 4d0a2f6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Zend/zend_alloc.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,40 @@ ZEND_API int is_zend_mm(void)
23762376
#endif
23772377
}
23782378

2379+
ZEND_API int is_zend_ptr(const void *ptr)
2380+
{
2381+
#if ZEND_MM_CUSTOM
2382+
if (AG(mm_heap)->use_custom_heap) {
2383+
return 0;
2384+
}
2385+
#endif
2386+
2387+
if (AG(mm_heap)->main_chunk) {
2388+
zend_mm_chunk *chunk = AG(mm_heap)->main_chunk;
2389+
2390+
do {
2391+
if (ptr >= (void*)chunk
2392+
&& ptr < (void*)((char*)chunk + ZEND_MM_CHUNK_SIZE)) {
2393+
return 1;
2394+
}
2395+
chunk = chunk->next;
2396+
} while (chunk != AG(mm_heap)->main_chunk);
2397+
}
2398+
2399+
if (AG(mm_heap)->huge_list) {
2400+
zend_mm_huge_list *block = AG(mm_heap)->huge_list;
2401+
2402+
do {
2403+
if (ptr >= (void*)block
2404+
&& ptr < (void*)((char*)block + block->size)) {
2405+
return 1;
2406+
}
2407+
block = block->next;
2408+
} while (block != AG(mm_heap)->huge_list);
2409+
}
2410+
return 0;
2411+
}
2412+
23792413
#if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
23802414
#undef _emalloc
23812415

Zend/zend_alloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ ZEND_API int zend_set_memory_limit(size_t memory_limit);
224224
ZEND_API void start_memory_manager(void);
225225
ZEND_API void shutdown_memory_manager(int silent, int full_shutdown);
226226
ZEND_API int is_zend_mm(void);
227+
ZEND_API int is_zend_ptr(const void *ptr);
227228

228229
ZEND_API size_t zend_memory_usage(int real_usage);
229230
ZEND_API size_t zend_memory_peak_usage(int real_usage);

0 commit comments

Comments
 (0)