Skip to content

Commit 9ad3b72

Browse files
kooldevtrowski
authored andcommitted
Avoid unnecessary ucontext memory allocations.
1 parent cb0da91 commit 9ad3b72

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Zend/zend_fibers.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ struct _zend_fiber_stack {
7272
const void *asan_pointer;
7373
size_t asan_size;
7474
#endif
75+
76+
#ifdef ZEND_FIBER_UCONTEXT
77+
/* Embedded ucontext to avoid unnecessary memory allocations. */
78+
ucontext_t ucontext;
79+
#endif
7580
};
7681

7782
/* 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
315320
}
316321

317322
#ifdef ZEND_FIBER_UCONTEXT
318-
ucontext_t *handle = emalloc(sizeof(ucontext_t));
323+
ucontext_t *handle = &context->stack->ucontext;
324+
319325
getcontext(handle);
320326

321327
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
346352
ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context)
347353
{
348354
zend_fiber_stack_free(context->stack);
349-
350-
#ifdef ZEND_FIBER_UCONTEXT
351-
efree(context->handle);
352-
#endif
353355
}
354356

355357
ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer)
@@ -883,13 +885,13 @@ void zend_fiber_init(void)
883885
{
884886
zend_fiber_context *context = ecalloc(1, sizeof(zend_fiber_context));
885887

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.
888890
context->stack = emalloc(sizeof(zend_fiber_stack));
889-
#endif
890891

891892
#ifdef ZEND_FIBER_UCONTEXT
892-
context->handle = emalloc(sizeof(ucontext_t));
893+
context->handle = &context->stack->ucontext;
894+
#endif
893895
#endif
894896

895897
context->status = ZEND_FIBER_STATUS_RUNNING;
@@ -903,11 +905,7 @@ void zend_fiber_init(void)
903905

904906
void zend_fiber_shutdown(void)
905907
{
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)
911909
efree(EG(main_fiber_context)->stack);
912910
#endif
913911

0 commit comments

Comments
 (0)