@@ -72,6 +72,11 @@ struct _zend_fiber_stack {
72
72
const void * asan_pointer ;
73
73
size_t asan_size ;
74
74
#endif
75
+
76
+ #ifdef ZEND_FIBER_UCONTEXT
77
+ /* Embedded ucontext to avoid unnecessary memory allocations. */
78
+ ucontext_t ucontext ;
79
+ #endif
75
80
};
76
81
77
82
/* Zend VM state that needs to be captured / restored during fiber context switch. */
@@ -315,7 +320,8 @@ ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, z
315
320
}
316
321
317
322
#ifdef ZEND_FIBER_UCONTEXT
318
- ucontext_t * handle = emalloc (sizeof (ucontext_t ));
323
+ ucontext_t * handle = & context -> stack -> ucontext ;
324
+
319
325
getcontext (handle );
320
326
321
327
handle -> uc_stack .ss_size = context -> stack -> size ;
@@ -346,10 +352,6 @@ ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, z
346
352
ZEND_API void zend_fiber_destroy_context (zend_fiber_context * context )
347
353
{
348
354
zend_fiber_stack_free (context -> stack );
349
-
350
- #ifdef ZEND_FIBER_UCONTEXT
351
- efree (context -> handle );
352
- #endif
353
355
}
354
356
355
357
ZEND_API void zend_fiber_switch_context (zend_fiber_transfer * transfer )
@@ -883,13 +885,13 @@ void zend_fiber_init(void)
883
885
{
884
886
zend_fiber_context * context = ecalloc (1 , sizeof (zend_fiber_context ));
885
887
886
- #ifdef __SANITIZE_ADDRESS__
887
- // Main fiber context stack is only accessed if ASan is enabled.
888
+ #if defined( __SANITIZE_ADDRESS__ ) || defined( ZEND_FIBER_UCONTEXT )
889
+ // Main fiber stack is only needed if ASan or ucontext is enabled.
888
890
context -> stack = emalloc (sizeof (zend_fiber_stack ));
889
- #endif
890
891
891
892
#ifdef ZEND_FIBER_UCONTEXT
892
- context -> handle = emalloc (sizeof (ucontext_t ));
893
+ context -> handle = & context -> stack -> ucontext ;
894
+ #endif
893
895
#endif
894
896
895
897
context -> status = ZEND_FIBER_STATUS_RUNNING ;
@@ -903,11 +905,7 @@ void zend_fiber_init(void)
903
905
904
906
void zend_fiber_shutdown (void )
905
907
{
906
- #ifdef ZEND_FIBER_UCONTEXT
907
- efree (EG (main_fiber_context )-> handle );
908
- #endif
909
-
910
- #ifdef __SANITIZE_ADDRESS__
908
+ #if defined(__SANITIZE_ADDRESS__ ) || defined(ZEND_FIBER_UCONTEXT )
911
909
efree (EG (main_fiber_context )-> stack );
912
910
#endif
913
911
0 commit comments