Skip to content

Commit 6a2b459

Browse files
iboboandypost
authored andcommitted
Allow using msgpack as serializer for APCu
1 parent 0cd46b1 commit 6a2b459

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

config.m4

Lines changed: 9 additions & 0 deletions
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

Lines changed: 68 additions & 0 deletions
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"
@@ -54,6 +58,12 @@ PHP_INI_END()
5458
PS_SERIALIZER_FUNCS(msgpack);
5559
#endif
5660

61+
#if defined(HAVE_APCU_SUPPORT)
62+
/** Apc serializer function prototypes */
63+
static int APC_SERIALIZER_NAME(msgpack) (APC_SERIALIZER_ARGS);
64+
static int APC_UNSERIALIZER_NAME(msgpack) (APC_UNSERIALIZER_ARGS);
65+
#endif
66+
5767
static zend_function_entry msgpack_functions[] = {
5868
ZEND_FE(msgpack_serialize, arginfo_msgpack_serialize)
5969
ZEND_FE(msgpack_unserialize, arginfo_msgpack_unserialize)
@@ -88,6 +98,13 @@ static ZEND_MINIT_FUNCTION(msgpack) /* {{{ */ {
8898
php_session_register_serializer("msgpack", PS_SERIALIZER_ENCODE_NAME(msgpack), PS_SERIALIZER_DECODE_NAME(msgpack));
8999
#endif
90100

101+
#if defined(HAVE_APCU_SUPPORT)
102+
apc_register_serializer("msgpack",
103+
APC_SERIALIZER_NAME(msgpack),
104+
APC_UNSERIALIZER_NAME(msgpack),
105+
NULL TSRMLS_CC);
106+
#endif
107+
91108
msgpack_init_class();
92109

93110
REGISTER_LONG_CONSTANT("MESSAGEPACK_OPT_PHPONLY",
@@ -109,6 +126,9 @@ static ZEND_MINFO_FUNCTION(msgpack) /* {{{ */ {
109126
php_info_print_table_row(2, "MessagePack Support", "enabled");
110127
#if HAVE_PHP_SESSION
111128
php_info_print_table_row(2, "Session Support", "enabled" );
129+
#endif
130+
#if defined(HAVE_APCU_SUPPORT)
131+
php_info_print_table_row(2, "APCu Serializer Support", "enabled" );
112132
#endif
113133
php_info_print_table_row(2, "extension Version", PHP_MSGPACK_VERSION);
114134
php_info_print_table_row(2, "header Version", MSGPACK_VERSION);
@@ -300,6 +320,54 @@ static ZEND_FUNCTION(msgpack_unserialize) /* {{{ */ {
300320
}
301321
/* }}} */
302322

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

0 commit comments

Comments
 (0)