Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created December 13, 2012 03:38
Show Gist options
  • Select an option

  • Save krakjoe/4273826 to your computer and use it in GitHub Desktop.

Select an option

Save krakjoe/4273826 to your computer and use it in GitHub Desktop.
custom-allocators apply on top of patch from earlier today, tested with jemalloc, tcmalloc, GC_, should be flexible enough to give them all a good testing ...
diff -ur a/php-src-jemalloc-zendmm/Zend/zend_alloc.c b/php-src-jemalloc-zendmm/Zend/zend_alloc.c
--- a/php-src-jemalloc-zendmm/Zend/zend_alloc.c 2012-12-12 06:59:10.000000000 +0100
+++ b/php-src-jemalloc-zendmm/Zend/zend_alloc.c 2012-12-13 03:00:41.000000000 +0100
@@ -37,18 +37,19 @@
# include <process.h>
#endif
-#ifdef HAVE_MEM_JEMALLOC
-# ifndef JEMALLOC_NO_DEMANGLE
-# define JEMALLOC_NO_DEMANGLE
-# endif
-#include <jemalloc/jemalloc.h>
+#ifdef ZEND_MM_CUSTOM
+# include "zend_mm_custom.h"
+# ifndef ZEND_MM_CUSTOM_FUNCTION
+# define ZEND_MM_CUSTOM_FUNCTION(entry) entry
+# endif
+# define ZEND_MM_CUSTOM_CALL(entry) ZEND_MM_CUSTOM_FUNCTION(entry)
#endif
#ifndef ZEND_MM_HEAP_PROTECTION
# define ZEND_MM_HEAP_PROTECTION ZEND_DEBUG
#endif
-#ifndef ZEND_MM_SAFE_UNLINKING
+#ifndef ZEND_MM_SAFE_UNLINKING
# define ZEND_MM_SAFE_UNLINKING 1
#endif
@@ -288,10 +289,9 @@
#endif
#ifdef HAVE_MEM_MALLOC
-
static zend_mm_segment* zend_mm_mem_malloc_alloc(zend_mm_storage *storage, size_t size)
{
- return (zend_mm_segment*)malloc(size);
+ return (zend_mm_segment*)malloc(size);
}
static zend_mm_segment* zend_mm_mem_malloc_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
@@ -307,38 +307,37 @@
# define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_malloc_alloc, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
#endif
-#ifdef HAVE_MEM_JEMALLOC
-
-static zend_mm_storage* zend_mm_mem_jemalloc_init(void *params)
+#ifdef ZEND_MM_CUSTOM
+static zend_mm_storage* zend_mm_mem_custom_init(void *params)
{
- return je_malloc(sizeof(zend_mm_storage));
+ return ZEND_MM_CUSTOM_CALL(malloc)(sizeof(zend_mm_storage));
}
-static void zend_mm_mem_jemalloc_dtor(zend_mm_storage *storage)
+static void zend_mm_mem_custom_dtor(zend_mm_storage *storage)
{
- je_free(storage);
+ ZEND_MM_CUSTOM_CALL(free)(storage);
}
-static void zend_mm_mem_jemalloc_compact(zend_mm_storage *storage)
+static void zend_mm_mem_custom_compact(zend_mm_storage *storage)
{
}
-static zend_mm_segment* zend_mm_mem_jemalloc_alloc(zend_mm_storage *storage, size_t size)
+static zend_mm_segment* zend_mm_mem_custom_alloc(zend_mm_storage *storage, size_t size)
{
- return (zend_mm_segment*)je_malloc(size);
+ return (zend_mm_segment*)ZEND_MM_CUSTOM_CALL(malloc)(size);
}
-static zend_mm_segment* zend_mm_mem_jemalloc_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
+static zend_mm_segment* zend_mm_mem_custom_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
{
- return (zend_mm_segment*)je_realloc(ptr, size);
+ return (zend_mm_segment*)ZEND_MM_CUSTOM_CALL(realloc)(ptr, size);
}
-static void zend_mm_mem_jemalloc_free(zend_mm_storage *storage, zend_mm_segment *ptr)
+static void zend_mm_mem_custom_free(zend_mm_storage *storage, zend_mm_segment *ptr)
{
- je_free(ptr);
+ ZEND_MM_CUSTOM_CALL(free)(ptr);
}
-#define ZEND_MM_MEM_JEMALLOC_DSC {"jemalloc", zend_mm_mem_jemalloc_init, zend_mm_mem_jemalloc_dtor, zend_mm_mem_jemalloc_compact, zend_mm_mem_jemalloc_alloc, zend_mm_mem_jemalloc_realloc, zend_mm_mem_jemalloc_free}
+#define ZEND_MM_MEM_ZEND_MM_CUSTOM_DSC {"custom", zend_mm_mem_custom_init, zend_mm_mem_custom_dtor, zend_mm_mem_custom_compact, zend_mm_mem_custom_alloc, zend_mm_mem_custom_realloc, zend_mm_mem_custom_free}
#endif
@@ -355,8 +354,8 @@
#ifdef HAVE_MEM_MMAP_ZERO
ZEND_MM_MEM_MMAP_ZERO_DSC,
#endif
-#ifdef HAVE_MEM_JEMALLOC
- ZEND_MM_MEM_JEMALLOC_DSC,
+#ifdef ZEND_MM_CUSTOM
+ ZEND_MM_MEM_ZEND_MM_CUSTOM_DSC,
#endif
{NULL, NULL, NULL, NULL, NULL, NULL}
};
diff -ur a/php-src-jemalloc-zendmm/Zend/Zend.m4 b/php-src-jemalloc-zendmm/Zend/Zend.m4
--- a/php-src-jemalloc-zendmm/Zend/Zend.m4 2012-12-12 06:59:10.000000000 +0100
+++ b/php-src-jemalloc-zendmm/Zend/Zend.m4 2012-12-13 02:54:42.000000000 +0100
@@ -392,31 +392,71 @@
AC_CHECK_FUNCS(mremap)
-AC_MSG_CHECKING(if jemalloc handlers should be used)
-
-AC_ARG_ENABLE(jemalloc,
-[ --enable-jemalloc Use jemalloc for malloc replacement],[
-AC_TRY_RUN([
-#include <stdlib.h>
-#include <jemalloc/jemalloc.h>
-
-int main() {
-#ifdef JEMALLOC_VERSION
- return 0;
-#else
- return 1;
-#endif
-}
+AC_MSG_CHECKING(for custom allocator)
+AC_ARG_WITH(custom-allocator,
+[ --with-custom-allocator use custom allocator library, ex. --with-custom-allocator=jemalloc],[
+ MM_CUSTOM=$withval
+ AC_DEFINE_UNQUOTED([ZEND_MM_CUSTOM], [$MM_CUSTOM], [custom allocator library])
+ AC_MSG_RESULT([$MM_CUSTOM])
+],[AC_MSG_RESULT([default])])
+
+if test "$MM_CUSTOM" != "no"; then
+AC_MSG_CHECKING(for custom allocator header)
+AC_ARG_WITH(custom-allocator-header,
+[ --with-custom-allocator-header use custom allocator header file], [
+ MM_CUSTOM_HEADER=$withval
+ AC_DEFINE_UNQUOTED([ZEND_MM_CUSTOM_HEADER], [$MM_CUSTOM_HEADER], [custom allocator header file])
+ AC_MSG_RESULT([$MM_CUSTOM_HEADER])
], [
- AC_DEFINE(HAVE_MEM_JEMALLOC, 1, [jemalloc malloc implementation is available])
- AC_MSG_RESULT([found])
- LIBS="$LIBS -ljemalloc"
+ MM_CUSTOM_HEADER="$MM_CUSTOM.h"
+ AC_DEFINE_UNQUOTED([ZEND_MM_CUSTOM_HEADER], [$MM_CUSTOM_HEADER], [custom allocator header file])
+ AC_MSG_RESULT([defaulting to $MM_CUSTOM.h])
+])
+AC_MSG_CHECKING(for custom allocator include path)
+AC_ARG_WITH(custom-allocator-incpath,
+[ --with-custom-allocator-incpath include path for allocator], [
+ MM_CUSTOM_INCPATH=$withval
+ AC_DEFINE_UNQUOTED(ZEND_MM_CUSTOM_INCPATH, [$MM_CUSTOM_INCPATH], [custom allocator include path])
+ INCLUDES="-I$MM_CUSTOM_INCPATH $INCLUDES"
+ AC_MSG_RESULT([$MM_CUSTOM_INCPATH])
], [
- AC_MSG_FAILURE([failed to find libjemalloc])
+ AC_MSG_RESULT([default])
])
-],[
- AC_MSG_RESULT([no])
+AC_MSG_CHECKING(for custom allocator libpath)
+AC_ARG_WITH(custom-allocator-libpath,
+[ --with-custom-allocator-libpath library path for allocator], [
+ MM_CUSTOM_LIBPATH=$withval
+ AC_DEFINE_UNQUOTED(ZEND_MM_CUSTOM_LIBPATH, [$MM_CUSTOM_LIBPATH], [custom allocator libpath])
+ LIBS="-L$MM_CUSTOM_LIBPATH -l$MM_CUSTOM $LIBS"
+ AC_MSG_RESULT([$MM_CUSTOM_LIBPATH])
+], [
+ LIBS="-l$MM_CUSTOM $LIBS"
+ AC_MSG_RESULT([default])
])
+AC_MSG_CHECKING(for custom allocator prefix)
+AC_ARG_WITH(custom-allocator-prefix,
+[ --with-custom-allocator-prefix prefix for calls to custom allocator functions], [
+ MM_CUSTOM_PREFIX=$withval
+ AC_DEFINE_UNQUOTED(ZEND_MM_CUSTOM_PREFIX, [$MM_CUSTOM_PREFIX], [custom allocator prefix])
+ AC_MSG_RESULT([$MM_CUSTOM_PREFIX])
+], [
+ AC_MSG_RESULT([none])
+])
+cat > Zend/zend_mm_custom.h <<EOF
+#ifndef HAVE_ZEND_MM_CUSTOM_H
+#define HAVE_ZEND_MM_CUSTOM_H
+# ifdef ZEND_MM_CUSTOM
+# include <$MM_CUSTOM_HEADER>
+# ifdef ZEND_MM_CUSTOM_PREFIX
+# ifndef ZEND_MM_CUSTOM_FUNCTION
+# define ZEND_MM_CUSTOM_FUNCTION(entry) $MM_CUSTOM_PREFIX##entry
+# endif
+# endif
+# endif
+#endif
+EOF
+
+fi
AC_ARG_ENABLE(zend-signals,
[ --enable-zend-signals Use zend signal handling],[
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment