58
58
#include "zend_multiply.h"
59
59
#include "zend_bitset.h"
60
60
#include "zend_mmap.h"
61
+ #include "zend_portability.h"
61
62
#include <signal.h>
62
63
63
64
#ifdef HAVE_UNISTD_H
@@ -273,11 +274,15 @@ struct _zend_mm_heap {
273
274
void * (* _malloc )(size_t );
274
275
void (* _free )(void * );
275
276
void * (* _realloc )(void * , size_t );
277
+ size_t (* _gc )(void );
278
+ void (* _shutdown )(bool full , bool silent );
276
279
} std ;
277
280
struct {
278
281
void * (* _malloc )(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
279
282
void (* _free )(void * ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
280
283
void * (* _realloc )(void * , size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC );
284
+ size_t (* _gc )(void );
285
+ void (* _shutdown )(bool full , bool silent );
281
286
} debug ;
282
287
} custom_heap ;
283
288
HashTable * tracked_allocs ;
@@ -1968,6 +1973,10 @@ ZEND_API size_t zend_mm_gc(zend_mm_heap *heap)
1968
1973
1969
1974
#if ZEND_MM_CUSTOM
1970
1975
if (heap -> use_custom_heap ) {
1976
+ size_t (* gc )(void ) = heap -> custom_heap .std ._gc ;
1977
+ if (gc ) {
1978
+ return gc ();
1979
+ }
1971
1980
return 0 ;
1972
1981
}
1973
1982
#endif
@@ -2262,10 +2271,10 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
2262
2271
2263
2272
#if ZEND_MM_CUSTOM
2264
2273
static void * tracked_malloc (size_t size );
2265
- static void tracked_free_all (void );
2274
+ static void tracked_free_all (zend_mm_heap * heap );
2266
2275
#endif
2267
2276
2268
- void zend_mm_shutdown (zend_mm_heap * heap , bool full , bool silent )
2277
+ ZEND_API void zend_mm_shutdown (zend_mm_heap * heap , bool full , bool silent )
2269
2278
{
2270
2279
zend_mm_chunk * p ;
2271
2280
zend_mm_huge_list * list ;
@@ -2274,7 +2283,7 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
2274
2283
if (heap -> use_custom_heap ) {
2275
2284
if (heap -> custom_heap .std ._malloc == tracked_malloc ) {
2276
2285
if (silent ) {
2277
- tracked_free_all ();
2286
+ tracked_free_all (heap );
2278
2287
}
2279
2288
zend_hash_clean (heap -> tracked_allocs );
2280
2289
if (full ) {
@@ -2286,13 +2295,20 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
2286
2295
heap -> size = 0 ;
2287
2296
}
2288
2297
2298
+ void (* shutdown )(bool , bool ) = heap -> custom_heap .std ._shutdown ;
2299
+
2289
2300
if (full ) {
2290
2301
if (ZEND_DEBUG && heap -> use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG ) {
2291
2302
heap -> custom_heap .debug ._free (heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC );
2292
2303
} else {
2293
2304
heap -> custom_heap .std ._free (heap );
2294
2305
}
2295
2306
}
2307
+
2308
+ if (shutdown ) {
2309
+ shutdown (full , silent );
2310
+ }
2311
+
2296
2312
return ;
2297
2313
}
2298
2314
#endif
@@ -2895,8 +2911,8 @@ static void *tracked_realloc(void *ptr, size_t new_size) {
2895
2911
return ptr ;
2896
2912
}
2897
2913
2898
- static void tracked_free_all (void ) {
2899
- HashTable * tracked_allocs = AG ( mm_heap ) -> tracked_allocs ;
2914
+ static void tracked_free_all (zend_mm_heap * heap ) {
2915
+ HashTable * tracked_allocs = heap -> tracked_allocs ;
2900
2916
zend_ulong h ;
2901
2917
ZEND_HASH_FOREACH_NUM_KEY (tracked_allocs , h ) {
2902
2918
void * ptr = (void * ) (uintptr_t ) (h << ZEND_MM_ALIGNMENT_LOG2 );
@@ -2980,6 +2996,16 @@ ZEND_API zend_mm_heap *zend_mm_get_heap(void)
2980
2996
return AG (mm_heap );
2981
2997
}
2982
2998
2999
+ ZEND_API zend_mm_heap * zend_mm_heap_create (void )
3000
+ {
3001
+ return zend_mm_init ();
3002
+ }
3003
+
3004
+ ZEND_API void zend_mm_heap_free (zend_mm_heap * heap )
3005
+ {
3006
+ zend_mm_chunk_free (heap , heap -> main_chunk , ZEND_MM_CHUNK_SIZE );
3007
+ }
3008
+
2983
3009
ZEND_API bool zend_mm_is_custom_heap (zend_mm_heap * new_heap )
2984
3010
{
2985
3011
#if ZEND_MM_CUSTOM
@@ -2990,9 +3016,11 @@ ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap)
2990
3016
}
2991
3017
2992
3018
ZEND_API void zend_mm_set_custom_handlers (zend_mm_heap * heap ,
2993
- void * (* _malloc )(size_t ),
2994
- void (* _free )(void * ),
2995
- void * (* _realloc )(void * , size_t ))
3019
+ void * (* _malloc )(size_t ),
3020
+ void (* _free )(void * ),
3021
+ void * (* _realloc )(void * , size_t ),
3022
+ size_t (* _gc )(void ),
3023
+ void (* _shutdown )(bool , bool ))
2996
3024
{
2997
3025
#if ZEND_MM_CUSTOM
2998
3026
zend_mm_heap * _heap = (zend_mm_heap * )heap ;
@@ -3004,14 +3032,18 @@ ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
3004
3032
_heap -> custom_heap .std ._malloc = _malloc ;
3005
3033
_heap -> custom_heap .std ._free = _free ;
3006
3034
_heap -> custom_heap .std ._realloc = _realloc ;
3035
+ _heap -> custom_heap .std ._gc = _gc ;
3036
+ _heap -> custom_heap .std ._shutdown = _shutdown ;
3007
3037
}
3008
3038
#endif
3009
3039
}
3010
3040
3011
3041
ZEND_API void zend_mm_get_custom_handlers (zend_mm_heap * heap ,
3012
- void * (* * _malloc )(size_t ),
3013
- void (* * _free )(void * ),
3014
- void * (* * _realloc )(void * , size_t ))
3042
+ void * (* * _malloc )(size_t ),
3043
+ void (* * _free )(void * ),
3044
+ void * (* * _realloc )(void * , size_t ),
3045
+ size_t (* * _gc )(void ),
3046
+ void (* * _shutdown )(bool , bool ))
3015
3047
{
3016
3048
#if ZEND_MM_CUSTOM
3017
3049
zend_mm_heap * _heap = (zend_mm_heap * )heap ;
@@ -3020,15 +3052,29 @@ ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
3020
3052
* _malloc = _heap -> custom_heap .std ._malloc ;
3021
3053
* _free = _heap -> custom_heap .std ._free ;
3022
3054
* _realloc = _heap -> custom_heap .std ._realloc ;
3055
+ if (_gc != NULL ) {
3056
+ * _gc = _heap -> custom_heap .std ._gc ;
3057
+ }
3058
+ if (_shutdown != NULL ) {
3059
+ * _shutdown = _heap -> custom_heap .std ._shutdown ;
3060
+ }
3023
3061
} else {
3024
3062
* _malloc = NULL ;
3025
3063
* _free = NULL ;
3026
3064
* _realloc = NULL ;
3065
+ if (_gc != NULL ) {
3066
+ * _gc = NULL ;
3067
+ }
3068
+ if (_shutdown != NULL ) {
3069
+ * _shutdown = NULL ;
3070
+ }
3027
3071
}
3028
3072
#else
3029
3073
* _malloc = NULL ;
3030
3074
* _free = NULL ;
3031
3075
* _realloc = NULL ;
3076
+ * _gc = NULL ;
3077
+ * _shutdown = NULL ;
3032
3078
#endif
3033
3079
}
3034
3080
0 commit comments