From 4baf9e93b28d97a38117faca20ab2d2b24681fe6 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Mon, 29 May 2017 21:41:07 +0200 Subject: [PATCH] Use v8::ArrayBuffer::Allocator::NewDefaultAllocator if available It isn't provided by older (still supported) versions of V8, keep using the shim in that case. --- config.m4 | 17 +++++++++++++++++ v8js_class.cc | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/config.m4 b/config.m4 index 65cc029..7b7b9a7 100644 --- a/config.m4 +++ b/config.m4 @@ -174,6 +174,23 @@ int main () V8_SEARCH_BLOB([snapshot_blob.bin], [PHP_V8_SNAPSHOT_BLOB_PATH]) + dnl + dnl Check for v8::ArrayBuffer::Allocator::NewDefaultAllocator + dnl + AC_CACHE_CHECK([for v8::ArrayBuffer::Allocator::NewDefaultAllocator], ac_cv_has_default_allocator, [ + AC_LINK_IFELSE([AC_LANG_PROGRAM([ + #include + ], [ v8::ArrayBuffer::Allocator::NewDefaultAllocator(); ])], [ + ac_cv_has_default_allocator=yes + ], [ + ac_cv_has_default_allocator=no + ]) + ]) + if test "x$ac_cv_has_default_allocator" = "xno"; then + AC_DEFINE([USE_INTERNAL_ALLOCATOR], [1], + [Define unless v8::ArrayBuffer::Allocator::NewDefaultAllocator is usable.]) + fi + AC_LANG_RESTORE LIBS=$old_LIBS LDFLAGS="$old_LDFLAGS" diff --git a/v8js_class.cc b/v8js_class.cc index 43f8bfe..970ca39 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -71,6 +71,7 @@ struct v8js_jsext { }; /* }}} */ +#ifdef USE_INTERNAL_ALLOCATOR class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { public: virtual void* Allocate(size_t length) { @@ -80,6 +81,7 @@ public: virtual void* AllocateUninitialized(size_t length) { return malloc(length); } virtual void Free(void* data, size_t) { free(data); } }; +#endif /** USE_INTERNAL_ALLOCATOR */ static void v8js_free_storage(zend_object *object) /* {{{ */ @@ -201,6 +203,10 @@ static void v8js_free_storage(zend_object *object) /* {{{ */ c->modules_base.~vector(); zval_ptr_dtor(&c->zval_snapshot_blob); + +#ifndef USE_INTERNAL_ALLOCATOR + delete c->create_params.array_buffer_allocator; +#endif } /* }}} */ @@ -353,8 +359,12 @@ static PHP_METHOD(V8Js, __construct) new (&c->create_params) v8::Isolate::CreateParams(); +#ifdef USE_INTERNAL_ALLOCATOR static ArrayBufferAllocator array_buffer_allocator; c->create_params.array_buffer_allocator = &array_buffer_allocator; +#else + c->create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); +#endif new (&c->snapshot_blob) v8::StartupData(); if (snapshot_blob) {