Skip to content

Commit 4baf9e9

Browse files
committed
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.
1 parent 3929456 commit 4baf9e9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

config.m4

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ int main ()
174174
V8_SEARCH_BLOB([snapshot_blob.bin], [PHP_V8_SNAPSHOT_BLOB_PATH])
175175

176176

177+
dnl
178+
dnl Check for v8::ArrayBuffer::Allocator::NewDefaultAllocator
179+
dnl
180+
AC_CACHE_CHECK([for v8::ArrayBuffer::Allocator::NewDefaultAllocator], ac_cv_has_default_allocator, [
181+
AC_LINK_IFELSE([AC_LANG_PROGRAM([
182+
#include <v8.h>
183+
], [ v8::ArrayBuffer::Allocator::NewDefaultAllocator(); ])], [
184+
ac_cv_has_default_allocator=yes
185+
], [
186+
ac_cv_has_default_allocator=no
187+
])
188+
])
189+
if test "x$ac_cv_has_default_allocator" = "xno"; then
190+
AC_DEFINE([USE_INTERNAL_ALLOCATOR], [1],
191+
[Define unless v8::ArrayBuffer::Allocator::NewDefaultAllocator is usable.])
192+
fi
193+
177194
AC_LANG_RESTORE
178195
LIBS=$old_LIBS
179196
LDFLAGS="$old_LDFLAGS"

v8js_class.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct v8js_jsext {
7171
};
7272
/* }}} */
7373

74+
#ifdef USE_INTERNAL_ALLOCATOR
7475
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
7576
public:
7677
virtual void* Allocate(size_t length) {
@@ -80,6 +81,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
8081
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
8182
virtual void Free(void* data, size_t) { free(data); }
8283
};
84+
#endif /** USE_INTERNAL_ALLOCATOR */
8385

8486

8587
static void v8js_free_storage(zend_object *object) /* {{{ */
@@ -201,6 +203,10 @@ static void v8js_free_storage(zend_object *object) /* {{{ */
201203
c->modules_base.~vector();
202204

203205
zval_ptr_dtor(&c->zval_snapshot_blob);
206+
207+
#ifndef USE_INTERNAL_ALLOCATOR
208+
delete c->create_params.array_buffer_allocator;
209+
#endif
204210
}
205211
/* }}} */
206212

@@ -353,8 +359,12 @@ static PHP_METHOD(V8Js, __construct)
353359

354360
new (&c->create_params) v8::Isolate::CreateParams();
355361

362+
#ifdef USE_INTERNAL_ALLOCATOR
356363
static ArrayBufferAllocator array_buffer_allocator;
357364
c->create_params.array_buffer_allocator = &array_buffer_allocator;
365+
#else
366+
c->create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
367+
#endif
358368

359369
new (&c->snapshot_blob) v8::StartupData();
360370
if (snapshot_blob) {

0 commit comments

Comments
 (0)