Skip to content

Commit 920d049

Browse files
ibobom6w6
authored andcommitted
Allow using msgpack as serializer for APCu
1 parent 1cdabdf commit 920d049

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

config.m4

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ Make sure that the comment is aligned:
55
[ --with-msgpack Include msgpack support])
66

77
if test "$PHP_MSGPACK" != "no"; then
8+
AC_MSG_CHECKING([for APC/APCU includes])
9+
if test -f "$phpincludedir/ext/apcu/apc_serializer.h"; then
10+
apc_inc_path="$phpincludedir"
11+
AC_MSG_RESULT([APCU in $apc_inc_path])
12+
AC_DEFINE(HAVE_APCU_SUPPORT,1,[Whether to enable apcu support])
13+
else
14+
AC_MSG_RESULT([not found])
15+
fi
16+
817
PHP_NEW_EXTENSION(msgpack, msgpack.c msgpack_pack.c msgpack_unpack.c msgpack_class.c msgpack_convert.c, $ext_shared)
918

1019
ifdef([PHP_INSTALL_HEADERS],

msgpack.c

+68
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "ext/session/php_session.h" /* for php_session_register_serializer */
1414
#endif
1515

16+
#if defined(HAVE_APCU_SUPPORT)
17+
#include "ext/apcu/apc_serializer.h"
18+
#endif /* HAVE_APCU_SUPPORT */
19+
1620
#include "php_msgpack.h"
1721
#include "msgpack_pack.h"
1822
#include "msgpack_unpack.h"
@@ -57,6 +61,12 @@ PHP_INI_END()
5761
PS_SERIALIZER_FUNCS(msgpack);
5862
#endif
5963

64+
#if defined(HAVE_APCU_SUPPORT)
65+
/** Apc serializer function prototypes */
66+
static int APC_SERIALIZER_NAME(msgpack) (APC_SERIALIZER_ARGS);
67+
static int APC_UNSERIALIZER_NAME(msgpack) (APC_UNSERIALIZER_ARGS);
68+
#endif
69+
6070
static zend_function_entry msgpack_functions[] = {
6171
ZEND_FE(msgpack_serialize, arginfo_msgpack_serialize)
6272
ZEND_FE(msgpack_unserialize, arginfo_msgpack_unserialize)
@@ -92,6 +102,13 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
92102
php_session_register_serializer("msgpack", PS_SERIALIZER_ENCODE_NAME(msgpack), PS_SERIALIZER_DECODE_NAME(msgpack));
93103
#endif
94104

105+
#if defined(HAVE_APCU_SUPPORT)
106+
apc_register_serializer("msgpack",
107+
APC_SERIALIZER_NAME(msgpack),
108+
APC_UNSERIALIZER_NAME(msgpack),
109+
NULL TSRMLS_CC);
110+
#endif
111+
95112
msgpack_init_class();
96113

97114
REGISTER_LONG_CONSTANT("MESSAGEPACK_OPT_PHPONLY",
@@ -115,6 +132,9 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
115132
php_info_print_table_row(2, "MessagePack Support", "enabled");
116133
#if HAVE_PHP_SESSION
117134
php_info_print_table_row(2, "Session Support", "enabled" );
135+
#endif
136+
#if defined(HAVE_APCU_SUPPORT)
137+
php_info_print_table_row(2, "APCu Serializer Support", "enabled" );
118138
#endif
119139
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
120140
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
@@ -306,6 +326,54 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
306326
}
307327
/* }}} */
308328

329+
#if defined(HAVE_APCU_SUPPORT)
330+
/* {{{ apc_serialize function */
331+
static int APC_SERIALIZER_NAME(msgpack) ( APC_SERIALIZER_ARGS ) {
332+
(void)config;
333+
334+
smart_str res = {0};
335+
msgpack_serialize_data_t var_hash;
336+
337+
msgpack_serialize_var_init(&var_hash);
338+
msgpack_serialize_zval(&res, (zval *) value, var_hash);
339+
msgpack_serialize_var_destroy(&var_hash);
340+
341+
smart_str_0(&res);
342+
343+
*buf = (unsigned char *) estrndup(ZSTR_VAL(res.s), ZSTR_LEN(res.s));
344+
*buf_len = ZSTR_LEN(res.s);
345+
346+
return 1;
347+
}
348+
/* }}} */
349+
/* {{{ apc_unserialize function */
350+
static int APC_UNSERIALIZER_NAME(msgpack) ( APC_UNSERIALIZER_ARGS ) {
351+
(void)config;
352+
353+
int ret;
354+
msgpack_unpack_t mp;
355+
msgpack_unserialize_data_t var_hash;
356+
size_t off = 0;
357+
358+
template_init(&mp);
359+
360+
msgpack_unserialize_var_init(&var_hash);
361+
362+
mp.user.retval = value;
363+
mp.user.var_hash = &var_hash;
364+
365+
ret = template_execute(&mp, (char *) buf, buf_len, &off);
366+
if (Z_TYPE_P(mp.user.retval) == IS_REFERENCE) {
367+
ZVAL_DEREF(mp.user.retval);
368+
}
369+
370+
msgpack_unserialize_var_destroy(&var_hash, 0);
371+
372+
return ret == MSGPACK_UNPACK_EXTRA_BYTES || ret == MSGPACK_UNPACK_SUCCESS;
373+
}
374+
/* }}} */
375+
#endif
376+
309377
/*
310378
* Local variables:
311379
* tab-width: 4

0 commit comments

Comments
 (0)