@@ -334,6 +334,8 @@ struct _zend_mm_heap {
334334 void * (* _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
335335 void (* _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
336336 void * (* _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
337+ size_t (* _gc )(void );
338+ void (* _shutdown )(bool full , bool silent );
337339 } custom_heap ;
338340 HashTable * tracked_allocs ;
339341#endif
@@ -2119,6 +2121,10 @@ ZEND_API size_t zend_mm_gc(zend_mm_heap *heap)
21192121
21202122#if ZEND_MM_CUSTOM
21212123 if (heap -> use_custom_heap ) {
2124+ size_t (* gc )(void ) = heap -> custom_heap ._gc ;
2125+ if (gc ) {
2126+ return gc ();
2127+ }
21222128 return 0 ;
21232129 }
21242130#endif
@@ -2421,10 +2427,10 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
24212427
24222428#if ZEND_MM_CUSTOM
24232429static void * tracked_malloc (size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
2424- static void tracked_free_all (void );
2430+ static void tracked_free_all (zend_mm_heap * heap );
24252431#endif
24262432
2427- void zend_mm_shutdown (zend_mm_heap * heap , bool full , bool silent )
2433+ ZEND_API void zend_mm_shutdown (zend_mm_heap * heap , bool full , bool silent )
24282434{
24292435 zend_mm_chunk * p ;
24302436 zend_mm_huge_list * list ;
@@ -2433,7 +2439,7 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
24332439 if (heap -> use_custom_heap ) {
24342440 if (heap -> custom_heap ._malloc == tracked_malloc ) {
24352441 if (silent ) {
2436- tracked_free_all ();
2442+ tracked_free_all (heap );
24372443 }
24382444 zend_hash_clean (heap -> tracked_allocs );
24392445 if (full ) {
@@ -2445,9 +2451,16 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
24452451 heap -> size = 0 ;
24462452 }
24472453
2454+ void (* shutdown )(bool , bool ) = heap -> custom_heap ._shutdown ;
2455+
24482456 if (full ) {
24492457 heap -> custom_heap ._free (heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC );
24502458 }
2459+
2460+ if (shutdown ) {
2461+ shutdown (full , silent );
2462+ }
2463+
24512464 return ;
24522465 }
24532466#endif
@@ -3039,8 +3052,8 @@ static void *tracked_realloc(void *ptr, size_t new_size ZEND_FILE_LINE_DC ZEND_F
30393052 return ptr ;
30403053}
30413054
3042- static void tracked_free_all (void ) {
3043- HashTable * tracked_allocs = AG ( mm_heap ) -> tracked_allocs ;
3055+ static void tracked_free_all (zend_mm_heap * heap ) {
3056+ HashTable * tracked_allocs = heap -> tracked_allocs ;
30443057 zend_ulong h ;
30453058 ZEND_HASH_FOREACH_NUM_KEY (tracked_allocs , h ) {
30463059 void * ptr = (void * ) (uintptr_t ) (h << ZEND_MM_ALIGNMENT_LOG2 );
@@ -3138,6 +3151,18 @@ ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
31383151 void (* _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
31393152 void * (* _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ))
31403153{
3154+ #if ZEND_MM_CUSTOM
3155+ zend_mm_set_custom_handlers_ex (heap , _malloc , _free , _realloc , NULL , NULL );
3156+ #endif
3157+ }
3158+
3159+ ZEND_API void zend_mm_set_custom_handlers_ex (zend_mm_heap * heap ,
3160+ void * (* _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3161+ void (* _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3162+ void * (* _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3163+ size_t (* _gc )(void ),
3164+ void (* _shutdown )(bool , bool ))
3165+ {
31413166#if ZEND_MM_CUSTOM
31423167 zend_mm_heap * _heap = (zend_mm_heap * )heap ;
31433168
@@ -3148,14 +3173,28 @@ ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
31483173 _heap -> custom_heap ._malloc = _malloc ;
31493174 _heap -> custom_heap ._free = _free ;
31503175 _heap -> custom_heap ._realloc = _realloc ;
3176+ _heap -> custom_heap ._gc = _gc ;
3177+ _heap -> custom_heap ._shutdown = _shutdown ;
31513178 }
31523179#endif
31533180}
31543181
31553182ZEND_API void zend_mm_get_custom_handlers (zend_mm_heap * heap ,
3156- void * (* * _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3157- void (* * _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3158- void * (* * _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ))
3183+ void * (* * _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3184+ void (* * _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3185+ void * (* * _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ))
3186+ {
3187+ #if ZEND_MM_CUSTOM
3188+ zend_mm_get_custom_handlers_ex (heap , _malloc , _free , _realloc , NULL , NULL );
3189+ #endif
3190+ }
3191+
3192+ ZEND_API void zend_mm_get_custom_handlers_ex (zend_mm_heap * heap ,
3193+ void * (* * _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3194+ void (* * _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3195+ void * (* * _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC ),
3196+ size_t (* * _gc )(void ),
3197+ void (* * _shutdown )(bool , bool ))
31593198{
31603199#if ZEND_MM_CUSTOM
31613200 zend_mm_heap * _heap = (zend_mm_heap * )heap ;
@@ -3164,15 +3203,29 @@ ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
31643203 * _malloc = _heap -> custom_heap ._malloc ;
31653204 * _free = _heap -> custom_heap ._free ;
31663205 * _realloc = _heap -> custom_heap ._realloc ;
3206+ if (_gc != NULL ) {
3207+ * _gc = _heap -> custom_heap ._gc ;
3208+ }
3209+ if (_shutdown != NULL ) {
3210+ * _shutdown = _heap -> custom_heap ._shutdown ;
3211+ }
31673212 } else {
31683213 * _malloc = NULL ;
31693214 * _free = NULL ;
31703215 * _realloc = NULL ;
3216+ if (_gc != NULL ) {
3217+ * _gc = NULL ;
3218+ }
3219+ if (_shutdown != NULL ) {
3220+ * _shutdown = NULL ;
3221+ }
31713222 }
31723223#else
31733224 * _malloc = NULL ;
31743225 * _free = NULL ;
31753226 * _realloc = NULL ;
3227+ * _gc = NULL ;
3228+ * _shutdown = NULL ;
31763229#endif
31773230}
31783231
0 commit comments