@@ -7,6 +7,9 @@ use libc::{c_char, c_int, c_void, size_t};
77use log:: { debug, trace, warn} ;
88use std:: sync:: atomic:: Ordering :: Relaxed ;
99
10+ #[ cfg( php_debug) ]
11+ use libc:: c_uint;
12+
1013#[ cfg( feature = "debug_stats" ) ]
1114use crate :: allocation:: { ALLOCATION_PROFILING_COUNT , ALLOCATION_PROFILING_SIZE } ;
1215
@@ -271,7 +274,24 @@ unsafe fn restore_zend_heap(heap: *mut zend::_zend_mm_heap, custom_heap: c_int)
271274 ptr:: write ( heap as * mut c_int , custom_heap) ;
272275}
273276
277+ #[ cfg( not( php_debug) ) ]
274278unsafe extern "C" fn alloc_prof_malloc ( len : size_t ) -> * mut c_void {
279+ alloc_prof_malloc_impl ( len)
280+ }
281+
282+ #[ cfg( php_debug) ]
283+ unsafe extern "C" fn alloc_prof_malloc (
284+ len : size_t ,
285+ _file : * const c_char ,
286+ _line : c_uint ,
287+ _orig_file : * const c_char ,
288+ _orig_line : c_uint ,
289+ ) -> * mut c_void {
290+ alloc_prof_malloc_impl ( len)
291+ }
292+
293+ #[ inline( always) ]
294+ unsafe fn alloc_prof_malloc_impl ( len : size_t ) -> * mut c_void {
275295 #[ cfg( feature = "debug_stats" ) ]
276296 ALLOCATION_PROFILING_COUNT . fetch_add ( 1 , Relaxed ) ;
277297 #[ cfg( feature = "debug_stats" ) ]
@@ -300,6 +320,11 @@ unsafe fn alloc_prof_prev_alloc(len: size_t) -> *mut c_void {
300320 // neighboring extension could misbehave. If that happens, we want a proper
301321 // panic with backtrace for debugging rather than undefined behavior.
302322 let alloc = tls_zend_mm_state_get ! ( prev_custom_mm_alloc) . unwrap ( ) ;
323+ #[ cfg( php_debug) ]
324+ {
325+ return alloc ( len, ptr:: null ( ) , 0 , ptr:: null ( ) , 0 ) ;
326+ }
327+ #[ cfg( not( php_debug) ) ]
303328 alloc ( len)
304329}
305330
@@ -308,14 +333,34 @@ unsafe fn alloc_prof_orig_alloc(len: size_t) -> *mut c_void {
308333 // handlers only point to this function after successful init. Using `unwrap_unchecked()` is
309334 // safe here as we have full control over ZendMM with no neighboring extensions.
310335 let heap = tls_zend_mm_state_get ! ( heap) . unwrap_unchecked ( ) ;
336+ #[ cfg( php_debug) ]
337+ return zend:: _zend_mm_alloc ( heap, len, ptr:: null ( ) , 0 , ptr:: null ( ) , 0 ) ;
338+ #[ cfg( not( php_debug) ) ]
311339 zend:: _zend_mm_alloc ( heap, len)
312340}
313341
314342/// This function exists because when calling `zend_mm_set_custom_handlers()`,
315343/// you need to pass a pointer to a `free()` function as well, otherwise your
316344/// custom handlers won't be installed. We cannot just point to the original
317345/// `zend::_zend_mm_free()` as the function definitions differ.
346+ #[ cfg( not( php_debug) ) ]
318347unsafe extern "C" fn alloc_prof_free ( ptr : * mut c_void ) {
348+ alloc_prof_free_impl ( ptr) ;
349+ }
350+
351+ #[ cfg( php_debug) ]
352+ unsafe extern "C" fn alloc_prof_free (
353+ ptr : * mut c_void ,
354+ _file : * const c_char ,
355+ _line : c_uint ,
356+ _orig_file : * const c_char ,
357+ _orig_line : c_uint ,
358+ ) {
359+ alloc_prof_free_impl ( ptr) ;
360+ }
361+
362+ #[ inline( always) ]
363+ unsafe fn alloc_prof_free_impl ( ptr : * mut c_void ) {
319364 tls_zend_mm_state_get ! ( free) ( ptr) ;
320365}
321366
@@ -327,6 +372,11 @@ unsafe fn alloc_prof_prev_free(ptr: *mut c_void) {
327372 // neighboring extension could misbehave. If that happens, we want a proper
328373 // panic with backtrace for debugging rather than undefined behavior.
329374 let free = tls_zend_mm_state_get ! ( prev_custom_mm_free) . unwrap ( ) ;
375+ #[ cfg( php_debug) ]
376+ {
377+ return free ( ptr, core:: ptr:: null ( ) , 0 , core:: ptr:: null ( ) , 0 ) ;
378+ }
379+ #[ cfg( not( php_debug) ) ]
330380 free ( ptr)
331381}
332382
@@ -335,10 +385,31 @@ unsafe fn alloc_prof_orig_free(ptr: *mut c_void) {
335385 // handlers only point to this function after successful init. Using `unwrap_unchecked()` is
336386 // safe here as we have full control over ZendMM with no neighboring extensions.
337387 let heap = tls_zend_mm_state_get ! ( heap) . unwrap_unchecked ( ) ;
388+ #[ cfg( php_debug) ]
389+ return zend:: _zend_mm_free ( heap, ptr, core:: ptr:: null ( ) , 0 , core:: ptr:: null ( ) , 0 ) ;
390+ #[ cfg( not( php_debug) ) ]
338391 zend:: _zend_mm_free ( heap, ptr) ;
339392}
340393
394+ #[ cfg( not( php_debug) ) ]
341395unsafe extern "C" fn alloc_prof_realloc ( prev_ptr : * mut c_void , len : size_t ) -> * mut c_void {
396+ alloc_prof_realloc_impl ( prev_ptr, len)
397+ }
398+
399+ #[ cfg( php_debug) ]
400+ unsafe extern "C" fn alloc_prof_realloc (
401+ prev_ptr : * mut c_void ,
402+ len : size_t ,
403+ _file : * const c_char ,
404+ _line : c_uint ,
405+ _orig_file : * const c_char ,
406+ _orig_line : c_uint ,
407+ ) -> * mut c_void {
408+ alloc_prof_realloc_impl ( prev_ptr, len)
409+ }
410+
411+ #[ inline( always) ]
412+ unsafe fn alloc_prof_realloc_impl ( prev_ptr : * mut c_void , len : size_t ) -> * mut c_void {
342413 #[ cfg( feature = "debug_stats" ) ]
343414 ALLOCATION_PROFILING_COUNT . fetch_add ( 1 , Relaxed ) ;
344415 #[ cfg( feature = "debug_stats" ) ]
@@ -367,6 +438,11 @@ unsafe fn alloc_prof_prev_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_
367438 // neighboring extension could misbehave. If that happens, we want a proper
368439 // panic with backtrace for debugging rather than undefined behavior.
369440 let realloc = tls_zend_mm_state_get ! ( prev_custom_mm_realloc) . unwrap ( ) ;
441+ #[ cfg( php_debug) ]
442+ {
443+ return realloc ( prev_ptr, len, ptr:: null ( ) , 0 , ptr:: null ( ) , 0 ) ;
444+ }
445+ #[ cfg( not( php_debug) ) ]
370446 realloc ( prev_ptr, len)
371447}
372448
@@ -375,6 +451,9 @@ unsafe fn alloc_prof_orig_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_
375451 // handlers only point to this function after successful init. Using `unwrap_unchecked()` is
376452 // safe here as we have full control over ZendMM with no neighboring extensions.
377453 let heap = tls_zend_mm_state_get ! ( heap) . unwrap_unchecked ( ) ;
454+ #[ cfg( php_debug) ]
455+ return zend:: _zend_mm_realloc ( heap, prev_ptr, len, ptr:: null ( ) , 0 , ptr:: null ( ) , 0 ) ;
456+ #[ cfg( not( php_debug) ) ]
378457 zend:: _zend_mm_realloc ( heap, prev_ptr, len)
379458}
380459
0 commit comments