OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #include "protobuf.h" | 1 #include "protobuf.h" |
32 | 2 |
33 #include <zend_hash.h> | 3 #include <zend_hash.h> |
34 | 4 |
35 ZEND_DECLARE_MODULE_GLOBALS(protobuf) | 5 ZEND_DECLARE_MODULE_GLOBALS(protobuf) |
36 static PHP_GINIT_FUNCTION(protobuf); | 6 static PHP_GINIT_FUNCTION(protobuf); |
37 static PHP_GSHUTDOWN_FUNCTION(protobuf); | 7 static PHP_GSHUTDOWN_FUNCTION(protobuf); |
38 static PHP_RINIT_FUNCTION(protobuf); | |
39 static PHP_RSHUTDOWN_FUNCTION(protobuf); | |
40 static PHP_MINIT_FUNCTION(protobuf); | |
41 static PHP_MSHUTDOWN_FUNCTION(protobuf); | |
42 | 8 |
| 9 // ----------------------------------------------------------------------------- |
43 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor | 10 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor |
44 // instances. | 11 // instances. |
45 static HashTable* upb_def_to_php_obj_map; | |
46 // Global map from message/enum's php class entry to corresponding wrapper | |
47 // Descriptor/EnumDescriptor instances. | |
48 static HashTable* ce_to_php_obj_map; | |
49 | |
50 // ----------------------------------------------------------------------------- | |
51 // Global maps. | |
52 // ----------------------------------------------------------------------------- | 12 // ----------------------------------------------------------------------------- |
53 | 13 |
54 static void add_to_table(HashTable* t, const void* def, void* value) { | 14 void add_def_obj(const void* def, zval* value) { |
55 uint nIndex = (ulong)def & t->nTableMask; | 15 uint nIndex = (ulong)def & PROTOBUF_G(upb_def_to_php_obj_map).nTableMask; |
56 | 16 |
57 zval* pDest = NULL; | 17 zval* pDest = NULL; |
58 zend_hash_index_update(t, (zend_ulong)def, &value, sizeof(zval*), (void**)&pDe
st); | 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); |
59 } | 21 } |
60 | 22 |
61 static void* get_from_table(const HashTable* t, const void* def) { | 23 zval* get_def_obj(const void* def) { |
62 void** value; | 24 zval** value; |
63 if (zend_hash_index_find(t, (zend_ulong)def, (void**)&value) == FAILURE) { | 25 if (zend_hash_index_find(&PROTOBUF_G(upb_def_to_php_obj_map), (zend_ulong)def, |
| 26 &value) == FAILURE) { |
64 zend_error(E_ERROR, "PHP object not found for given definition.\n"); | 27 zend_error(E_ERROR, "PHP object not found for given definition.\n"); |
65 return NULL; | 28 return NULL; |
66 } | 29 } |
67 return *value; | 30 return *value; |
68 } | 31 } |
69 | 32 |
70 static void add_to_list(HashTable* t, void* value) { | |
71 zval* pDest = NULL; | |
72 zend_hash_next_index_insert(t, &value, sizeof(void*), (void**)&pDest); | |
73 } | |
74 | |
75 void add_def_obj(const void* def, zval* value) { | |
76 Z_ADDREF_P(value); | |
77 add_to_table(upb_def_to_php_obj_map, def, value); | |
78 } | |
79 | |
80 zval* get_def_obj(const void* def) { | |
81 return (zval*)get_from_table(upb_def_to_php_obj_map, def); | |
82 } | |
83 | |
84 void add_ce_obj(const void* ce, zval* value) { | |
85 Z_ADDREF_P(value); | |
86 add_to_table(ce_to_php_obj_map, ce, value); | |
87 } | |
88 | |
89 zval* get_ce_obj(const void* ce) { | |
90 return (zval*)get_from_table(ce_to_php_obj_map, ce); | |
91 } | |
92 | |
93 // ----------------------------------------------------------------------------- | 33 // ----------------------------------------------------------------------------- |
94 // Utilities. | 34 // Utilities. |
95 // ----------------------------------------------------------------------------- | 35 // ----------------------------------------------------------------------------- |
96 | 36 |
| 37 // define the function(s) we want to add |
97 zend_function_entry protobuf_functions[] = { | 38 zend_function_entry protobuf_functions[] = { |
| 39 ZEND_FE(get_generated_pool, NULL) |
98 ZEND_FE_END | 40 ZEND_FE_END |
99 }; | 41 }; |
100 | 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. |
101 zend_module_entry protobuf_module_entry = { | 46 zend_module_entry protobuf_module_entry = { |
102 STANDARD_MODULE_HEADER, | 47 STANDARD_MODULE_HEADER, |
103 PHP_PROTOBUF_EXTNAME, // extension name | 48 PHP_PROTOBUF_EXTNAME, // extension name |
104 protobuf_functions, // function list | 49 protobuf_functions, // function list |
105 PHP_MINIT(protobuf), // process startup | 50 PHP_MINIT(protobuf), // process startup |
106 PHP_MSHUTDOWN(protobuf), // process shutdown | 51 NULL, // process shutdown |
107 PHP_RINIT(protobuf), // request shutdown | 52 NULL, // request startup |
108 PHP_RSHUTDOWN(protobuf), // request shutdown | 53 NULL, // request shutdown |
109 NULL, // extension info | 54 NULL, // extension info |
110 PHP_PROTOBUF_VERSION, // extension version | 55 PHP_PROTOBUF_VERSION, // extension version |
111 PHP_MODULE_GLOBALS(protobuf), // globals descriptor | 56 PHP_MODULE_GLOBALS(protobuf), // globals descriptor |
112 PHP_GINIT(protobuf), // globals ctor | 57 PHP_GINIT(protobuf), // globals ctor |
113 PHP_GSHUTDOWN(protobuf), // globals dtor | 58 PHP_GSHUTDOWN(protobuf), // globals dtor |
114 NULL, // post deactivate | 59 NULL, // post deactivate |
115 STANDARD_MODULE_PROPERTIES_EX | 60 STANDARD_MODULE_PROPERTIES_EX |
116 }; | 61 }; |
117 | 62 |
118 // install module | 63 // install module |
119 ZEND_GET_MODULE(protobuf) | 64 ZEND_GET_MODULE(protobuf) |
120 | 65 |
121 // global variables | 66 // global variables |
122 static PHP_GINIT_FUNCTION(protobuf) { | 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); |
123 } | 73 } |
124 | 74 |
125 static PHP_GSHUTDOWN_FUNCTION(protobuf) { | 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); |
126 } | 83 } |
127 | 84 |
128 static PHP_RINIT_FUNCTION(protobuf) { | 85 PHP_MINIT_FUNCTION(protobuf) { |
129 ALLOC_HASHTABLE(upb_def_to_php_obj_map); | |
130 zend_hash_init(upb_def_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0); | |
131 | |
132 ALLOC_HASHTABLE(ce_to_php_obj_map); | |
133 zend_hash_init(ce_to_php_obj_map, 16, NULL, ZVAL_PTR_DTOR, 0); | |
134 | |
135 generated_pool = NULL; | |
136 generated_pool_php = NULL; | |
137 | |
138 return 0; | |
139 } | |
140 | |
141 static PHP_RSHUTDOWN_FUNCTION(protobuf) { | |
142 zend_hash_destroy(upb_def_to_php_obj_map); | |
143 FREE_HASHTABLE(upb_def_to_php_obj_map); | |
144 | |
145 zend_hash_destroy(ce_to_php_obj_map); | |
146 FREE_HASHTABLE(ce_to_php_obj_map); | |
147 | |
148 if (generated_pool_php != NULL) { | |
149 zval_dtor(generated_pool_php); | |
150 FREE_ZVAL(generated_pool_php); | |
151 } | |
152 | |
153 return 0; | |
154 } | |
155 | |
156 static PHP_MINIT_FUNCTION(protobuf) { | |
157 map_field_init(TSRMLS_C); | |
158 repeated_field_init(TSRMLS_C); | |
159 repeated_field_iter_init(TSRMLS_C); | |
160 gpb_type_init(TSRMLS_C); | |
161 message_init(TSRMLS_C); | |
162 descriptor_pool_init(TSRMLS_C); | 86 descriptor_pool_init(TSRMLS_C); |
163 descriptor_init(TSRMLS_C); | 87 descriptor_init(TSRMLS_C); |
164 enum_descriptor_init(TSRMLS_C); | 88 message_builder_context_init(TSRMLS_C); |
165 util_init(TSRMLS_C); | |
166 | |
167 return 0; | |
168 } | 89 } |
169 | |
170 static PHP_MSHUTDOWN_FUNCTION(protobuf) { | |
171 PEFREE(message_handlers); | |
172 PEFREE(repeated_field_handlers); | |
173 PEFREE(map_field_handlers); | |
174 | |
175 return 0; | |
176 } | |
OLD | NEW |