zend_list.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* resource lists */
  20. #include "zend.h"
  21. #include "zend_list.h"
  22. #include "zend_API.h"
  23. #include "zend_globals.h"
  24. ZEND_API int le_index_ptr;
  25. /* true global */
  26. static HashTable list_destructors;
  27. ZEND_API zval* ZEND_FASTCALL zend_list_insert(void *ptr, int type)
  28. {
  29. int index;
  30. zval zv;
  31. index = zend_hash_next_free_element(&EG(regular_list));
  32. if (index == 0) {
  33. index = 1;
  34. }
  35. ZVAL_NEW_RES(&zv, index, ptr, type);
  36. return zend_hash_index_add_new(&EG(regular_list), index, &zv);
  37. }
  38. ZEND_API int ZEND_FASTCALL zend_list_delete(zend_resource *res)
  39. {
  40. if (GC_DELREF(res) <= 0) {
  41. return zend_hash_index_del(&EG(regular_list), res->handle);
  42. } else {
  43. return SUCCESS;
  44. }
  45. }
  46. ZEND_API int ZEND_FASTCALL zend_list_free(zend_resource *res)
  47. {
  48. if (GC_REFCOUNT(res) <= 0) {
  49. return zend_hash_index_del(&EG(regular_list), res->handle);
  50. } else {
  51. return SUCCESS;
  52. }
  53. }
  54. static void zend_resource_dtor(zend_resource *res)
  55. {
  56. zend_rsrc_list_dtors_entry *ld;
  57. zend_resource r = *res;
  58. res->type = -1;
  59. res->ptr = NULL;
  60. ld = zend_hash_index_find_ptr(&list_destructors, r.type);
  61. if (ld) {
  62. if (ld->list_dtor_ex) {
  63. ld->list_dtor_ex(&r);
  64. }
  65. } else {
  66. zend_error(E_WARNING, "Unknown list entry type (%d)", r.type);
  67. }
  68. }
  69. ZEND_API int ZEND_FASTCALL zend_list_close(zend_resource *res)
  70. {
  71. if (GC_REFCOUNT(res) <= 0) {
  72. return zend_list_free(res);
  73. } else if (res->type >= 0) {
  74. zend_resource_dtor(res);
  75. }
  76. return SUCCESS;
  77. }
  78. ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
  79. {
  80. zval *zv;
  81. zv = zend_list_insert(rsrc_pointer, rsrc_type);
  82. return Z_RES_P(zv);
  83. }
  84. ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
  85. {
  86. if (res) {
  87. if (resource_type1 == res->type) {
  88. return res->ptr;
  89. }
  90. if (resource_type2 == res->type) {
  91. return res->ptr;
  92. }
  93. }
  94. if (resource_type_name) {
  95. const char *space;
  96. const char *class_name = get_active_class_name(&space);
  97. zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  98. }
  99. return NULL;
  100. }
  101. ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
  102. {
  103. if (resource_type == res->type) {
  104. return res->ptr;
  105. }
  106. if (resource_type_name) {
  107. const char *space;
  108. const char *class_name = get_active_class_name(&space);
  109. zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  110. }
  111. return NULL;
  112. }
  113. ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
  114. {
  115. const char *space, *class_name;
  116. if (res == NULL) {
  117. if (resource_type_name) {
  118. class_name = get_active_class_name(&space);
  119. zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
  120. }
  121. return NULL;
  122. }
  123. if (Z_TYPE_P(res) != IS_RESOURCE) {
  124. if (resource_type_name) {
  125. class_name = get_active_class_name(&space);
  126. zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  127. }
  128. return NULL;
  129. }
  130. return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
  131. }
  132. ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
  133. {
  134. const char *space, *class_name;
  135. if (res == NULL) {
  136. if (resource_type_name) {
  137. class_name = get_active_class_name(&space);
  138. zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
  139. }
  140. return NULL;
  141. }
  142. if (Z_TYPE_P(res) != IS_RESOURCE) {
  143. if (resource_type_name) {
  144. class_name = get_active_class_name(&space);
  145. zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  146. }
  147. return NULL;
  148. }
  149. return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
  150. }
  151. void list_entry_destructor(zval *zv)
  152. {
  153. zend_resource *res = Z_RES_P(zv);
  154. ZVAL_UNDEF(zv);
  155. if (res->type >= 0) {
  156. zend_resource_dtor(res);
  157. }
  158. efree_size(res, sizeof(zend_resource));
  159. }
  160. void plist_entry_destructor(zval *zv)
  161. {
  162. zend_resource *res = Z_RES_P(zv);
  163. if (res->type >= 0) {
  164. zend_rsrc_list_dtors_entry *ld;
  165. ld = zend_hash_index_find_ptr(&list_destructors, res->type);
  166. if (ld) {
  167. if (ld->plist_dtor_ex) {
  168. ld->plist_dtor_ex(res);
  169. }
  170. } else {
  171. zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
  172. }
  173. }
  174. free(res);
  175. }
  176. int zend_init_rsrc_list(void)
  177. {
  178. zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
  179. return SUCCESS;
  180. }
  181. int zend_init_rsrc_plist(void)
  182. {
  183. zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
  184. return SUCCESS;
  185. }
  186. static int zend_close_rsrc(zval *zv)
  187. {
  188. zend_resource *res = Z_PTR_P(zv);
  189. if (res->type >= 0) {
  190. zend_resource_dtor(res);
  191. }
  192. return ZEND_HASH_APPLY_KEEP;
  193. }
  194. void zend_close_rsrc_list(HashTable *ht)
  195. {
  196. zend_hash_reverse_apply(ht, zend_close_rsrc);
  197. }
  198. void zend_destroy_rsrc_list(HashTable *ht)
  199. {
  200. zend_hash_graceful_reverse_destroy(ht);
  201. }
  202. static int clean_module_resource(zval *zv, void *arg)
  203. {
  204. int resource_id = *(int *)arg;
  205. if (Z_RES_TYPE_P(zv) == resource_id) {
  206. return 1;
  207. } else {
  208. return 0;
  209. }
  210. }
  211. static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
  212. {
  213. zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
  214. int module_number = *(int *)arg;
  215. if (ld->module_number == module_number) {
  216. zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
  217. return 1;
  218. } else {
  219. return 0;
  220. }
  221. }
  222. void zend_clean_module_rsrc_dtors(int module_number)
  223. {
  224. zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
  225. }
  226. ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
  227. {
  228. zend_rsrc_list_dtors_entry *lde;
  229. zval zv;
  230. lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
  231. lde->list_dtor_ex = ld;
  232. lde->plist_dtor_ex = pld;
  233. lde->module_number = module_number;
  234. lde->resource_id = list_destructors.nNextFreeElement;
  235. lde->type_name = type_name;
  236. ZVAL_PTR(&zv, lde);
  237. if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
  238. return FAILURE;
  239. }
  240. return list_destructors.nNextFreeElement-1;
  241. }
  242. ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
  243. {
  244. zend_rsrc_list_dtors_entry *lde;
  245. ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
  246. if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
  247. return lde->resource_id;
  248. }
  249. } ZEND_HASH_FOREACH_END();
  250. return 0;
  251. }
  252. static void list_destructors_dtor(zval *zv)
  253. {
  254. free(Z_PTR_P(zv));
  255. }
  256. int zend_init_rsrc_list_dtors(void)
  257. {
  258. zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
  259. list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
  260. return SUCCESS;
  261. }
  262. void zend_destroy_rsrc_list_dtors(void)
  263. {
  264. zend_hash_destroy(&list_destructors);
  265. }
  266. const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
  267. {
  268. zend_rsrc_list_dtors_entry *lde;
  269. lde = zend_hash_index_find_ptr(&list_destructors, res->type);
  270. if (lde) {
  271. return lde->type_name;
  272. } else {
  273. return NULL;
  274. }
  275. }
  276. ZEND_API zend_resource* zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
  277. {
  278. zval *zv;
  279. zval tmp;
  280. ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
  281. GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(tmp));
  282. GC_MAKE_PERSISTENT_LOCAL(key);
  283. zv = zend_hash_update(&EG(persistent_list), key, &tmp);
  284. return Z_RES_P(zv);
  285. }
  286. ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
  287. {
  288. zend_string *str = zend_string_init(key, key_len, 1);
  289. zend_resource *ret = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
  290. zend_string_release_ex(str, 1);
  291. return ret;
  292. }
  293. /*
  294. * Local variables:
  295. * tab-width: 4
  296. * c-basic-offset: 4
  297. * indent-tabs-mode: t
  298. * End:
  299. * vim600: sw=4 ts=4 fdm=marker
  300. * vim<600: sw=4 ts=4
  301. */