Skip to content

Commit b83ab47

Browse files
committed
ext/opcache/zend_shared_alloc: add zend_shared_alloc_copy()
Allows simplifying some code in zend_jit_trace.c.
1 parent 6eb0745 commit b83ab47

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

ext/opcache/jit/zend_jit_trace.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7085,36 +7085,31 @@ static zend_jit_trace_stop zend_jit_compile_root_trace(zend_jit_trace_rec *trace
70857085
JIT_G(trigger) = orig_trigger;
70867086

70877087
if (handler) {
7088-
zend_jit_trace_exit_info *shared_exit_info = NULL;
7089-
70907088
t->exit_info = NULL;
70917089
if (t->exit_count) {
70927090
/* reallocate exit_info into shared memory */
7093-
shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
7091+
t->exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc_copy(exit_info,
70947092
sizeof(zend_jit_trace_exit_info) * t->exit_count);
70957093

7096-
if (!shared_exit_info) {
7094+
if (!t->exit_info) {
70977095
if (t->stack_map) {
70987096
efree(t->stack_map);
70997097
}
71007098
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
71017099
goto exit;
71027100
}
7103-
memcpy(shared_exit_info, exit_info,
7104-
sizeof(zend_jit_trace_exit_info) * t->exit_count);
7105-
t->exit_info = shared_exit_info;
71067101
}
71077102

71087103
if (t->stack_map_size) {
7109-
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
7104+
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc_copy(
7105+
t->stack_map,
7106+
t->stack_map_size * sizeof(zend_jit_trace_stack));
7107+
efree(t->stack_map);
7108+
t->stack_map = shared_stack_map;
71107109
if (!shared_stack_map) {
7111-
efree(t->stack_map);
71127110
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
71137111
goto exit;
71147112
}
7115-
memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
7116-
efree(t->stack_map);
7117-
t->stack_map = shared_stack_map;
71187113
}
71197114

71207115
t->exit_counters = ZEND_JIT_EXIT_COUNTERS;
@@ -7815,36 +7810,31 @@ static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace
78157810
JIT_G(trigger) = orig_trigger;
78167811

78177812
if (handler) {
7818-
zend_jit_trace_exit_info *shared_exit_info = NULL;
7819-
78207813
t->exit_info = NULL;
78217814
if (t->exit_count) {
78227815
/* reallocate exit_info into shared memory */
7823-
shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
7816+
t->exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc_copy(exit_info,
78247817
sizeof(zend_jit_trace_exit_info) * t->exit_count);
78257818

7826-
if (!shared_exit_info) {
7819+
if (!t->exit_info) {
78277820
if (t->stack_map) {
78287821
efree(t->stack_map);
78297822
}
78307823
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
78317824
goto exit;
78327825
}
7833-
memcpy(shared_exit_info, exit_info,
7834-
sizeof(zend_jit_trace_exit_info) * t->exit_count);
7835-
t->exit_info = shared_exit_info;
78367826
}
78377827

78387828
if (t->stack_map_size) {
7839-
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
7829+
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc_copy(
7830+
t->stack_map,
7831+
t->stack_map_size * sizeof(zend_jit_trace_stack));
7832+
efree(t->stack_map);
7833+
t->stack_map = shared_stack_map;
78407834
if (!shared_stack_map) {
7841-
efree(t->stack_map);
78427835
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
78437836
goto exit;
78447837
}
7845-
memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
7846-
efree(t->stack_map);
7847-
t->stack_map = shared_stack_map;
78487838
}
78497839

78507840
zend_jit_link_side_trace(

ext/opcache/zend_shared_alloc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ void *zend_shared_alloc(size_t size)
363363
return NULL;
364364
}
365365

366+
void *zend_shared_alloc_copy(const void *src, size_t size)
367+
{
368+
void *dest = zend_shared_alloc(size);
369+
if (dest != NULL)
370+
memcpy(dest, src, size);
371+
return dest;
372+
}
373+
366374
static zend_always_inline zend_ulong zend_rotr3(zend_ulong key)
367375
{
368376
return (key >> 3) | (key << ((sizeof(key) * 8) - 3));

ext/opcache/zend_shared_alloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ void zend_shared_alloc_shutdown(void);
133133
/* allocate shared memory block */
134134
void *zend_shared_alloc(size_t size);
135135

136+
/**
137+
* Allocate a shared memory block and copy raw data from the given
138+
* source pointer into it.
139+
*
140+
* @return the new allocation or NULL if out of memory
141+
*/
142+
void *zend_shared_alloc_copy(const void *src, size_t size);
143+
136144
/**
137145
* Wrapper for zend_shared_alloc() which aligns at 64-byte boundary if
138146
* AVX or SSE2 are used.

0 commit comments

Comments
 (0)