Skip to content

Commit 028853c

Browse files
committed
ext/opcache/zend_shared_alloc: add zend_shared_alloc_copy()
Allows simplifying some code in zend_jit_trace.c.
1 parent 2392a42 commit 028853c

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
@@ -7071,37 +7071,32 @@ static zend_jit_trace_stop zend_jit_compile_root_trace(zend_jit_trace_rec *trace
70717071
JIT_G(trigger) = orig_trigger;
70727072

70737073
if (handler) {
7074-
zend_jit_trace_exit_info *shared_exit_info = NULL;
7075-
70767074
t->exit_info = NULL;
70777075
if (t->exit_count) {
70787076
/* reallocate exit_info into shared memory */
7079-
shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
7077+
t->exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc_copy(exit_info,
70807078
sizeof(zend_jit_trace_exit_info) * t->exit_count);
70817079

7082-
if (!shared_exit_info) {
7080+
if (!t->exit_info) {
70837081
if (t->stack_map) {
70847082
efree(t->stack_map);
70857083
t->stack_map = NULL;
70867084
}
70877085
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
70887086
goto exit;
70897087
}
7090-
memcpy(shared_exit_info, exit_info,
7091-
sizeof(zend_jit_trace_exit_info) * t->exit_count);
7092-
t->exit_info = shared_exit_info;
70937088
}
70947089

70957090
if (t->stack_map_size) {
7096-
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
7091+
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc_copy(
7092+
t->stack_map,
7093+
t->stack_map_size * sizeof(zend_jit_trace_stack));
7094+
efree(t->stack_map);
7095+
t->stack_map = shared_stack_map;
70977096
if (!shared_stack_map) {
7098-
efree(t->stack_map);
70997097
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
71007098
goto exit;
71017099
}
7102-
memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
7103-
efree(t->stack_map);
7104-
t->stack_map = shared_stack_map;
71057100
}
71067101

71077102
t->exit_counters = ZEND_JIT_EXIT_COUNTERS;
@@ -7812,37 +7807,32 @@ static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace
78127807
JIT_G(trigger) = orig_trigger;
78137808

78147809
if (handler) {
7815-
zend_jit_trace_exit_info *shared_exit_info = NULL;
7816-
78177810
t->exit_info = NULL;
78187811
if (t->exit_count) {
78197812
/* reallocate exit_info into shared memory */
7820-
shared_exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc(
7813+
t->exit_info = (zend_jit_trace_exit_info*)zend_shared_alloc_copy(exit_info,
78217814
sizeof(zend_jit_trace_exit_info) * t->exit_count);
78227815

7823-
if (!shared_exit_info) {
7816+
if (!t->exit_info) {
78247817
if (t->stack_map) {
78257818
efree(t->stack_map);
78267819
t->stack_map = NULL;
78277820
}
78287821
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
78297822
goto exit;
78307823
}
7831-
memcpy(shared_exit_info, exit_info,
7832-
sizeof(zend_jit_trace_exit_info) * t->exit_count);
7833-
t->exit_info = shared_exit_info;
78347824
}
78357825

78367826
if (t->stack_map_size) {
7837-
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc(t->stack_map_size * sizeof(zend_jit_trace_stack));
7827+
zend_jit_trace_stack *shared_stack_map = (zend_jit_trace_stack*)zend_shared_alloc_copy(
7828+
t->stack_map,
7829+
t->stack_map_size * sizeof(zend_jit_trace_stack));
7830+
efree(t->stack_map);
7831+
t->stack_map = shared_stack_map;
78387832
if (!shared_stack_map) {
7839-
efree(t->stack_map);
78407833
ret = ZEND_JIT_TRACE_STOP_NO_SHM;
78417834
goto exit;
78427835
}
7843-
memcpy(shared_stack_map, t->stack_map, t->stack_map_size * sizeof(zend_jit_trace_stack));
7844-
efree(t->stack_map);
7845-
t->stack_map = shared_stack_map;
78467836
}
78477837

78487838
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
@@ -366,6 +366,14 @@ void *zend_shared_alloc(size_t size)
366366
return NULL;
367367
}
368368

369+
void *zend_shared_alloc_copy(const void *src, size_t size)
370+
{
371+
void *dest = zend_shared_alloc(size);
372+
if (dest != NULL)
373+
memcpy(dest, src, size);
374+
return dest;
375+
}
376+
369377
static zend_always_inline zend_ulong zend_rotr3(zend_ulong key)
370378
{
371379
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)