Skip to content

Commit 5b4e67a

Browse files
committed
ext/{libxml,openssl}: jemalloc support
1 parent e745b71 commit 5b4e67a

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,20 @@ AS_VAR_IF([PHP_DMALLOC], [yes],
953953
AS_VAR_APPEND([CPPFLAGS], [" -DDMALLOC_FUNC_CHECK"])],
954954
[AC_MSG_FAILURE([The dmalloc check failed. Cannot enable dmalloc.])])])
955955

956+
PHP_ARG_ENABLE([jemalloc],
957+
[whether to enable jemalloc],
958+
[AS_HELP_STRING([--enable-jemalloc],
959+
[Enable jemalloc])],
960+
[no],
961+
[no])
962+
963+
if test "$PHP_JEMALLOC" = "yes"; then
964+
PKG_CHECK_MODULES([JEMALLOC], [jemalloc],, [AC_MSG_ERROR([jemalloc not found])])
965+
AC_DEFINE(HAVE_JEMALLOC, 1, [Whether you have jemalloc])
966+
PHP_EVAL_LIBLINE($JEMALLOC_LIBS)
967+
PHP_EVAL_INCLINE($JEMALLOC_CFLAGS)
968+
fi
969+
956970
PHP_ARG_ENABLE([ipv6],
957971
[whether to enable IPv6 support],
958972
[AS_HELP_STRING([--disable-ipv6],

ext/libxml/libxml.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include <libxml/xmlschemas.h>
4444
#endif
4545

46+
#ifdef HAVE_JEMALLOC
47+
#include <jemalloc/jemalloc.h>
48+
#endif // HAVE_JEMALLOC
49+
4650
#include "php_libxml.h"
4751

4852
#define PHP_LIBXML_LOADED_VERSION ((char *)xmlParserVersion)
@@ -901,9 +905,27 @@ static void php_libxml_exports_dtor(zval *zv)
901905
free(Z_PTR_P(zv));
902906
}
903907

908+
#ifdef HAVE_JEMALLOC
909+
910+
static char *
911+
je_xmlMemStrdup(const char *src)
912+
{
913+
const size_t size = strlen(src) + 1;
914+
char *dest = je_malloc(size);
915+
if (dest != NULL)
916+
memcpy(dest, src, size);
917+
return dest;
918+
}
919+
920+
#endif // HAVE_JEMALLOC
921+
904922
PHP_LIBXML_API void php_libxml_initialize(void)
905923
{
906924
if (!_php_libxml_initialized) {
925+
#ifdef HAVE_JEMALLOC
926+
xmlMemSetup(je_free, je_malloc, je_realloc, je_xmlMemStrdup);
927+
#endif
928+
907929
/* we should be the only one's to ever init!! */
908930
ZEND_IGNORE_LEAKS_BEGIN();
909931
xmlInitParser();

ext/openssl/openssl.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
/* Common */
7070
#include <time.h>
7171

72+
#ifdef HAVE_JEMALLOC
73+
#include <jemalloc/jemalloc.h>
74+
#endif // HAVE_JEMALLOC
75+
7276
#if (defined(PHP_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1900)
7377
#define timezone _timezone /* timezone is called _timezone in LibC */
7478
#endif
@@ -1249,6 +1253,37 @@ PHP_INI_BEGIN()
12491253
PHP_INI_END()
12501254
/* }}} */
12511255

1256+
#ifdef HAVE_JEMALLOC
1257+
1258+
static void *
1259+
je_CRYPTO_malloc(size_t num, const char *file, int line)
1260+
{
1261+
(void)file;
1262+
(void)line;
1263+
1264+
return je_malloc(num);
1265+
}
1266+
1267+
static void *
1268+
je_CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
1269+
{
1270+
(void)file;
1271+
(void)line;
1272+
1273+
return je_realloc(addr, num);
1274+
}
1275+
1276+
static void
1277+
je_CRYPTO_free(void *addr, const char *file, int line)
1278+
{
1279+
(void)file;
1280+
(void)line;
1281+
1282+
je_free(addr);
1283+
}
1284+
1285+
#endif // HAVE_JEMALLOC
1286+
12521287
/* {{{ PHP_MINIT_FUNCTION */
12531288
PHP_MINIT_FUNCTION(openssl)
12541289
{
@@ -1287,6 +1322,10 @@ PHP_MINIT_FUNCTION(openssl)
12871322
php_openssl_pkey_object_handlers.clone_obj = NULL;
12881323
php_openssl_pkey_object_handlers.compare = zend_objects_not_comparable;
12891324

1325+
#ifdef HAVE_JEMALLOC
1326+
CRYPTO_set_mem_functions(je_CRYPTO_malloc, je_CRYPTO_realloc, je_CRYPTO_free);
1327+
#endif // HAVE_JEMALLOC
1328+
12901329
#ifdef LIBRESSL_VERSION_NUMBER
12911330
OPENSSL_config(NULL);
12921331
SSL_library_init();

0 commit comments

Comments
 (0)