Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 1 | #include "protobuf.h" |
| 2 | |
| 3 | #include <zend_hash.h> |
| 4 | |
| 5 | ZEND_DECLARE_MODULE_GLOBALS(protobuf) |
| 6 | static PHP_GINIT_FUNCTION(protobuf); |
| 7 | static PHP_GSHUTDOWN_FUNCTION(protobuf); |
| 8 | |
| 9 | // ----------------------------------------------------------------------------- |
| 10 | // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor |
| 11 | // instances. |
| 12 | // ----------------------------------------------------------------------------- |
| 13 | |
| 14 | void add_def_obj(const void* def, zval* value) { |
| 15 | uint nIndex = (ulong)def & PROTOBUF_G(upb_def_to_php_obj_map).nTableMask; |
| 16 | |
| 17 | zval* pDest = NULL; |
| 18 | Z_ADDREF_P(value); |
| 19 | zend_hash_index_update(&PROTOBUF_G(upb_def_to_php_obj_map), (zend_ulong)def, |
| 20 | &value, sizeof(zval*), &pDest); |
| 21 | } |
| 22 | |
| 23 | zval* get_def_obj(const void* def) { |
| 24 | zval** value; |
| 25 | if (zend_hash_index_find(&PROTOBUF_G(upb_def_to_php_obj_map), (zend_ulong)def, |
| 26 | &value) == FAILURE) { |
| 27 | zend_error(E_ERROR, "PHP object not found for given definition.\n"); |
| 28 | return NULL; |
| 29 | } |
| 30 | return *value; |
| 31 | } |
| 32 | |
| 33 | // ----------------------------------------------------------------------------- |
| 34 | // Utilities. |
| 35 | // ----------------------------------------------------------------------------- |
| 36 | |
| 37 | // define the function(s) we want to add |
| 38 | zend_function_entry protobuf_functions[] = { |
| 39 | ZEND_FE(get_generated_pool, NULL) |
| 40 | ZEND_FE_END |
| 41 | }; |
| 42 | |
| 43 | // "protobuf_functions" refers to the struct defined above |
| 44 | // we'll be filling in more of this later: you can use this to specify |
| 45 | // globals, php.ini info, startup and teardown functions, etc. |
| 46 | zend_module_entry protobuf_module_entry = { |
| 47 | STANDARD_MODULE_HEADER, |
| 48 | PHP_PROTOBUF_EXTNAME, // extension name |
| 49 | protobuf_functions, // function list |
| 50 | PHP_MINIT(protobuf), // process startup |
| 51 | NULL, // process shutdown |
| 52 | NULL, // request startup |
| 53 | NULL, // request shutdown |
| 54 | NULL, // extension info |
| 55 | PHP_PROTOBUF_VERSION, // extension version |
| 56 | PHP_MODULE_GLOBALS(protobuf), // globals descriptor |
| 57 | PHP_GINIT(protobuf), // globals ctor |
| 58 | PHP_GSHUTDOWN(protobuf), // globals dtor |
| 59 | NULL, // post deactivate |
| 60 | STANDARD_MODULE_PROPERTIES_EX |
| 61 | }; |
| 62 | |
| 63 | // install module |
| 64 | ZEND_GET_MODULE(protobuf) |
| 65 | |
| 66 | // global variables |
| 67 | static PHP_GINIT_FUNCTION(protobuf) { |
| 68 | protobuf_globals->generated_pool = NULL; |
| 69 | generated_pool = NULL; |
| 70 | protobuf_globals->message_handlers = NULL; |
| 71 | zend_hash_init(&protobuf_globals->upb_def_to_php_obj_map, 16, NULL, |
| 72 | ZVAL_PTR_DTOR, 0); |
| 73 | } |
| 74 | |
| 75 | static PHP_GSHUTDOWN_FUNCTION(protobuf) { |
| 76 | if (protobuf_globals->generated_pool != NULL) { |
| 77 | FREE_ZVAL(protobuf_globals->generated_pool); |
| 78 | } |
| 79 | if (protobuf_globals->message_handlers != NULL) { |
| 80 | FREE(protobuf_globals->message_handlers); |
| 81 | } |
| 82 | zend_hash_destroy(&protobuf_globals->upb_def_to_php_obj_map); |
| 83 | } |
| 84 | |
| 85 | PHP_MINIT_FUNCTION(protobuf) { |
| 86 | descriptor_pool_init(TSRMLS_C); |
| 87 | descriptor_init(TSRMLS_C); |
| 88 | message_builder_context_init(TSRMLS_C); |
| 89 | } |