Skip to content

Commit

Permalink
New PHP C Extension based on upb_msg (protocolbuffers#7614)
Browse files Browse the repository at this point in the history
* Added code for the new PHP extension.

* Removed a bunch of unused variables shown by compiler warnings.

* Test protobuf2 in the tests.

* Updated upb version to fix a goldenfile test.

* Added pure-PHP changes. Extension now passes all tests.

* Enabled protobuf2 for all C extension tests.

* Fixed pure=PHP lib: full names no longer start with '.'.

* Added files for new extension to Makefile.am.

* Downgraded make-preload.py to python 2, since python3 isn't available in the php_all Kokoro image.

* Disable tests of new C extension with PHP 5.x.

* Also do not compile the extension for PHP5.

* Accept version 5.*.*, and use /usr/bin/python.

* Addressed PR comments.

* Addressed PR comments.

* Added "const" to a parameter and fixed a memory leak seen in Valgrind.

* Stop testing the C extension for PHP5.

The next release of protobuf will deprecate the C extension
for PHP5, see:
protocolbuffers#7525

* Made the PHP5.6 Mac test only exercise pure-PHP.

* Build protoc for PHP 5.6 test.

* Rewrote bundling script in PHP to avoid dependency on Python.

* A few more fixes.

* Fixed int32/int64 behavior for 32-bit builds.

* Match more PHP versions in testing script.

* Use phpize --clean before building extension.

* Force-delete configure.in to avoid phpize problems cross-version.

* Delete both configure.ac and configure.in.
  • Loading branch information
haberman authored Jun 25, 2020
1 parent 2b7b7f7 commit 093faeb
Show file tree
Hide file tree
Showing 28 changed files with 16,922 additions and 108 deletions.
21 changes: 21 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,27 @@ php_EXTRA_DIST= \
php/ext/google/protobuf/upb.h \
php/ext/google/protobuf/utf8.c \
php/ext/google/protobuf/utf8.h \
php/ext/google/protobuf2/arena.c \
php/ext/google/protobuf2/arena.h \
php/ext/google/protobuf2/array.c \
php/ext/google/protobuf2/array.h \
php/ext/google/protobuf2/bundled_php.h \
php/ext/google/protobuf2/config.m4 \
php/ext/google/protobuf2/convert.c \
php/ext/google/protobuf2/convert.h \
php/ext/google/protobuf2/def.c \
php/ext/google/protobuf2/def.h \
php/ext/google/protobuf2/make-preload.php \
php/ext/google/protobuf2/map.c \
php/ext/google/protobuf2/map.h \
php/ext/google/protobuf2/message.c \
php/ext/google/protobuf2/message.h \
php/ext/google/protobuf2/names.c \
php/ext/google/protobuf2/names.h \
php/ext/google/protobuf2/php-upb.c \
php/ext/google/protobuf2/php-upb.h \
php/ext/google/protobuf2/protobuf.c \
php/ext/google/protobuf2/protobuf.h \
php/generate_descriptor_protos.sh \
php/phpunit.xml \
php/release.sh \
Expand Down
95 changes: 95 additions & 0 deletions php/ext/google/protobuf2/arena.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <Zend/zend_API.h>

#include "php-upb.h"

// -----------------------------------------------------------------------------
// Arena
// -----------------------------------------------------------------------------

typedef struct Arena {
zend_object std;
upb_arena* arena;
} Arena;

zend_class_entry *Arena_class_entry;
static zend_object_handlers Arena_object_handlers;

// PHP Object Handlers /////////////////////////////////////////////////////////

static zend_object* Arena_Create(zend_class_entry *class_type) {
Arena *intern = emalloc(sizeof(Arena));
zend_object_std_init(&intern->std, class_type);
intern->std.handlers = &Arena_object_handlers;
intern->arena = upb_arena_new();
// Skip object_properties_init(), we don't allow derived classes.
return &intern->std;
}

static void Arena_Free(zend_object* obj) {
Arena* intern = (Arena*)obj;
upb_arena_free(intern->arena);
zend_object_std_dtor(&intern->std);
}

// C Functions from arena.h ////////////////////////////////////////////////////

void Arena_Init(zval* val) {
ZVAL_OBJ(val, Arena_Create(Arena_class_entry));
}

upb_arena *Arena_Get(zval *val) {
Arena *a = (Arena*)Z_OBJ_P(val);
return a->arena;
}

// -----------------------------------------------------------------------------
// Module init.
// -----------------------------------------------------------------------------

// No public methods.
static const zend_function_entry Arena_methods[] = {
ZEND_FE_END
};

void Arena_ModuleInit() {
zend_class_entry tmp_ce;

INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\Internal\\Arena", Arena_methods);
Arena_class_entry = zend_register_internal_class(&tmp_ce);
Arena_class_entry->create_object = Arena_Create;
Arena_class_entry->ce_flags |= ZEND_ACC_FINAL;

memcpy(&Arena_object_handlers, &std_object_handlers,
sizeof(zend_object_handlers));
Arena_object_handlers.free_obj = Arena_Free;
}
47 changes: 47 additions & 0 deletions php/ext/google/protobuf2/arena.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef PHP_PROTOBUF_ARENA_H_
#define PHP_PROTOBUF_ARENA_H_

#include <php.h>

#include "php-upb.h"

// Registers the PHP Arena class.
void Arena_ModuleInit();

// Creates and returns a new arena object that wraps a new upb_arena*.
void Arena_Init(zval *val);

// Gets the underlying upb_arena from this arena object.
upb_arena *Arena_Get(zval *arena);

#endif // PHP_PROTOBUF_ARENA_H_
Loading

0 comments on commit 093faeb

Please sign in to comment.