zend_list.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. zval zv;
  30. zend_long index = zend_hash_next_free_element(&EG(regular_list));
  31. if (index == 0) {
  32. index = 1;
  33. } else if (index == ZEND_LONG_MAX) {
  34. zend_error_noreturn(E_ERROR, "Resource ID space overflow");
  35. }
  36. ZVAL_NEW_RES(&zv, index, ptr, type);
  37. return zend_hash_index_add_new(&EG(regular_list), index, &zv);
  38. }
  39. ZEND_API zend_result ZEND_FASTCALL zend_list_delete(zend_resource *res)
  40. {
  41. if (GC_DELREF(res) <= 0) {
  42. return zend_hash_index_del(&EG(regular_list), res->handle);
  43. } else {
  44. return SUCCESS;
  45. }
  46. }
  47. ZEND_API void ZEND_FASTCALL zend_list_free(zend_resource *res)
  48. {
  49. ZEND_ASSERT(GC_REFCOUNT(res) == 0);
  50. zend_hash_index_del(&EG(regular_list), res->handle);
  51. }
  52. static void zend_resource_dtor(zend_resource *res)
  53. {
  54. zend_rsrc_list_dtors_entry *ld;
  55. zend_resource r = *res;
  56. res->type = -1;
  57. res->ptr = NULL;
  58. ld = zend_hash_index_find_ptr(&list_destructors, r.type);
  59. ZEND_ASSERT(ld && "Unknown list entry type");
  60. if (ld->list_dtor_ex) {
  61. ld->list_dtor_ex(&r);
  62. }
  63. }
  64. ZEND_API void ZEND_FASTCALL zend_list_close(zend_resource *res)
  65. {
  66. if (GC_REFCOUNT(res) <= 0) {
  67. zend_list_free(res);
  68. } else if (res->type >= 0) {
  69. zend_resource_dtor(res);
  70. }
  71. }
  72. ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
  73. {
  74. zval *zv;
  75. zv = zend_list_insert(rsrc_pointer, rsrc_type);
  76. return Z_RES_P(zv);
  77. }
  78. ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
  79. {
  80. if (res) {
  81. if (resource_type1 == res->type) {
  82. return res->ptr;
  83. }
  84. if (resource_type2 == res->type) {
  85. return res->ptr;
  86. }
  87. }
  88. if (resource_type_name) {
  89. const char *space;
  90. const char *class_name = get_active_class_name(&space);
  91. zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  92. }
  93. return NULL;
  94. }
  95. ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
  96. {
  97. if (resource_type == res->type) {
  98. return res->ptr;
  99. }
  100. if (resource_type_name) {
  101. const char *space;
  102. const char *class_name = get_active_class_name(&space);
  103. zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  104. }
  105. return NULL;
  106. }
  107. ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
  108. {
  109. const char *space, *class_name;
  110. if (res == NULL) {
  111. if (resource_type_name) {
  112. class_name = get_active_class_name(&space);
  113. zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
  114. }
  115. return NULL;
  116. }
  117. if (Z_TYPE_P(res) != IS_RESOURCE) {
  118. if (resource_type_name) {
  119. class_name = get_active_class_name(&space);
  120. zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  121. }
  122. return NULL;
  123. }
  124. return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
  125. }
  126. ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
  127. {
  128. const char *space, *class_name;
  129. if (res == NULL) {
  130. if (resource_type_name) {
  131. class_name = get_active_class_name(&space);
  132. zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
  133. }
  134. return NULL;
  135. }
  136. if (Z_TYPE_P(res) != IS_RESOURCE) {
  137. if (resource_type_name) {
  138. class_name = get_active_class_name(&space);
  139. zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
  140. }
  141. return NULL;
  142. }
  143. return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
  144. }
  145. void list_entry_destructor(zval *zv)
  146. {
  147. zend_resource *res = Z_RES_P(zv);
  148. ZVAL_UNDEF(zv);
  149. if (res->type >= 0) {
  150. zend_resource_dtor(res);
  151. }
  152. efree_size(res, sizeof(zend_resource));
  153. }
  154. void plist_entry_destructor(zval *zv)
  155. {
  156. zend_resource *res = Z_RES_P(zv);
  157. if (res->type >= 0) {
  158. zend_rsrc_list_dtors_entry *ld;
  159. ld = zend_hash_index_find_ptr(&list_destructors, res->type);
  160. ZEND_ASSERT(ld && "Unknown list entry type");
  161. if (ld->plist_dtor_ex) {
  162. ld->plist_dtor_ex(res);
  163. }
  164. }
  165. free(res);
  166. }
  167. ZEND_API void zend_init_rsrc_list(void)
  168. {
  169. zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
  170. EG(regular_list).nNextFreeElement = 0;
  171. }
  172. void zend_init_rsrc_plist(void)
  173. {
  174. zend_hash_init(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1);
  175. }
  176. void zend_close_rsrc_list(HashTable *ht)
  177. {
  178. /* Reload ht->arData on each iteration, as it may be reallocated. */
  179. uint32_t i = ht->nNumUsed;
  180. while (i-- > 0) {
  181. Bucket *p = &ht->arData[i];
  182. if (Z_TYPE(p->val) != IS_UNDEF) {
  183. zend_resource *res = Z_PTR(p->val);
  184. if (res->type >= 0) {
  185. zend_resource_dtor(res);
  186. }
  187. }
  188. }
  189. }
  190. void zend_destroy_rsrc_list(HashTable *ht)
  191. {
  192. zend_hash_graceful_reverse_destroy(ht);
  193. }
  194. /* int return due to HashTable API */
  195. static int clean_module_resource(zval *zv, void *arg)
  196. {
  197. int resource_id = *(int *)arg;
  198. return Z_RES_TYPE_P(zv) == resource_id;
  199. }
  200. /* int return due to HashTable API */
  201. static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
  202. {
  203. zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
  204. int module_number = *(int *)arg;
  205. if (ld->module_number == module_number) {
  206. zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
  207. return 1;
  208. } else {
  209. return 0;
  210. }
  211. }
  212. void zend_clean_module_rsrc_dtors(int module_number)
  213. {
  214. zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
  215. }
  216. 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)
  217. {
  218. zend_rsrc_list_dtors_entry *lde;
  219. zval zv;
  220. lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
  221. lde->list_dtor_ex = ld;
  222. lde->plist_dtor_ex = pld;
  223. lde->module_number = module_number;
  224. lde->resource_id = list_destructors.nNextFreeElement;
  225. lde->type_name = type_name;
  226. ZVAL_PTR(&zv, lde);
  227. if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
  228. return FAILURE;
  229. }
  230. return list_destructors.nNextFreeElement-1;
  231. }
  232. ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
  233. {
  234. zend_rsrc_list_dtors_entry *lde;
  235. ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
  236. if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
  237. return lde->resource_id;
  238. }
  239. } ZEND_HASH_FOREACH_END();
  240. return 0;
  241. }
  242. static void list_destructors_dtor(zval *zv)
  243. {
  244. free(Z_PTR_P(zv));
  245. }
  246. void zend_init_rsrc_list_dtors(void)
  247. {
  248. zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
  249. list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
  250. }
  251. void zend_destroy_rsrc_list_dtors(void)
  252. {
  253. zend_hash_destroy(&list_destructors);
  254. }
  255. const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
  256. {
  257. zend_rsrc_list_dtors_entry *lde;
  258. lde = zend_hash_index_find_ptr(&list_destructors, res->type);
  259. if (lde) {
  260. return lde->type_name;
  261. } else {
  262. return NULL;
  263. }
  264. }
  265. ZEND_API zend_resource* zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
  266. {
  267. zval *zv;
  268. zval tmp;
  269. ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
  270. GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(tmp));
  271. GC_MAKE_PERSISTENT_LOCAL(key);
  272. zv = zend_hash_update(&EG(persistent_list), key, &tmp);
  273. return Z_RES_P(zv);
  274. }
  275. ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
  276. {
  277. zend_string *str = zend_string_init(key, key_len, 1);
  278. zend_resource *ret = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
  279. zend_string_release_ex(str, 1);
  280. return ret;
  281. }