You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
6.5 KiB

27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 0.91 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.zend.com/license/0_91.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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  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. static inline int zend_list_do_insert(HashTable *list,void *ptr, int type)
  26. {
  27. int index;
  28. list_entry le;
  29. index = zend_hash_next_free_element(list);
  30. if (index==0) index++;
  31. le.ptr=ptr;
  32. le.type=type;
  33. le.refcount=1;
  34. zend_hash_index_update(list, index, (void *) &le, sizeof(list_entry), NULL);
  35. return index;
  36. }
  37. static inline int zend_list_do_delete(HashTable *list,int id)
  38. {
  39. list_entry *le;
  40. ELS_FETCH();
  41. if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
  42. /* printf("del(%d): %d->%d\n", id, le->refcount, le->refcount-1); */
  43. if (--le->refcount<=0) {
  44. return zend_hash_index_del(&EG(regular_list), id);
  45. } else {
  46. return SUCCESS;
  47. }
  48. } else {
  49. return FAILURE;
  50. }
  51. }
  52. static inline void *zend_list_do_find(HashTable *list,int id, int *type)
  53. {
  54. list_entry *le;
  55. if (zend_hash_index_find(list, id, (void **) &le)==SUCCESS) {
  56. *type = le->type;
  57. return le->ptr;
  58. } else {
  59. *type = -1;
  60. return NULL;
  61. }
  62. }
  63. ZEND_API int zend_list_insert(void *ptr, int type)
  64. {
  65. ELS_FETCH();
  66. return zend_list_do_insert(&EG(regular_list), ptr, type);
  67. }
  68. ZEND_API int zend_plist_insert(void *ptr, int type)
  69. {
  70. ELS_FETCH();
  71. return zend_list_do_insert(&EG(persistent_list), ptr, type);
  72. }
  73. ZEND_API int zend_list_addref(int id)
  74. {
  75. list_entry *le;
  76. ELS_FETCH();
  77. if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
  78. /* printf("add(%d): %d->%d\n", id, le->refcount, le->refcount+1); */
  79. le->refcount++;
  80. return SUCCESS;
  81. } else {
  82. return FAILURE;
  83. }
  84. }
  85. ZEND_API int zend_list_delete(int id)
  86. {
  87. ELS_FETCH();
  88. return zend_list_do_delete(&EG(regular_list), id);
  89. }
  90. ZEND_API int zend_plist_delete(int id)
  91. {
  92. ELS_FETCH();
  93. return zend_list_do_delete(&EG(persistent_list), id);
  94. }
  95. ZEND_API void *zend_list_find(int id, int *type)
  96. {
  97. ELS_FETCH();
  98. return zend_list_do_find(&EG(regular_list), id, type);
  99. }
  100. ZEND_API void *zend_plist_find(int id, int *type)
  101. {
  102. ELS_FETCH();
  103. return zend_list_do_find(&EG(persistent_list), id, type);
  104. }
  105. ZEND_API int zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type)
  106. {
  107. int rsrc_id;
  108. rsrc_id = zend_list_insert(rsrc_pointer, rsrc_type);
  109. if (rsrc_result) {
  110. rsrc_result->value.lval = rsrc_id;
  111. rsrc_result->type = IS_RESOURCE;
  112. }
  113. return rsrc_id;
  114. }
  115. ZEND_API void *zend_fetch_resource(zval **passed_id, int default_id, char *resource_type_name, int *found_resource_type, int num_resource_types, ...)
  116. {
  117. int id;
  118. int actual_resource_type;
  119. void *resource;
  120. va_list resource_types;
  121. int i;
  122. if (default_id==-1) { /* use id */
  123. if (!passed_id) {
  124. if (resource_type_name) {
  125. zend_error(E_WARNING, "No %s resource supplied", resource_type_name);
  126. }
  127. return NULL;
  128. } else if ((*passed_id)->type != IS_RESOURCE) {
  129. if (resource_type_name) {
  130. zend_error(E_WARNING, "Supplied argument is not a valid %s resource", resource_type_name);
  131. }
  132. return NULL;
  133. }
  134. id = (*passed_id)->value.lval;
  135. } else {
  136. id = default_id;
  137. }
  138. resource = zend_list_find(id, &actual_resource_type);
  139. if (!resource) {
  140. if (resource_type_name)
  141. zend_error(E_WARNING, "%d is not a valid %s resource", id, resource_type_name);
  142. return NULL;
  143. }
  144. va_start(resource_types, num_resource_types);
  145. for (i=0; i<num_resource_types; i++) {
  146. if (actual_resource_type == va_arg(resource_types, int)) {
  147. va_end(resource_types);
  148. if (found_resource_type) {
  149. *found_resource_type = actual_resource_type;
  150. }
  151. return resource;
  152. }
  153. }
  154. va_end(resource_types);
  155. if (resource_type_name)
  156. zend_error(E_WARNING, "Supplied resource is not a valid %s resource", resource_type_name);
  157. return NULL;
  158. }
  159. void list_entry_destructor(void *ptr)
  160. {
  161. list_entry *le = (list_entry *) ptr;
  162. list_destructors_entry *ld;
  163. if (zend_hash_index_find(&list_destructors,le->type,(void **) &ld)==SUCCESS) {
  164. if (ld->list_destructor) {
  165. (ld->list_destructor)(le->ptr);
  166. }
  167. } else {
  168. zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)",le->type);
  169. }
  170. }
  171. void plist_entry_destructor(void *ptr)
  172. {
  173. list_entry *le = (list_entry *) ptr;
  174. list_destructors_entry *ld;
  175. if (zend_hash_index_find(&list_destructors,le->type,(void **) &ld)==SUCCESS) {
  176. if (ld->plist_destructor) {
  177. (ld->plist_destructor)(le->ptr);
  178. }
  179. } else {
  180. zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)",le->type);
  181. }
  182. }
  183. int init_resource_list(ELS_D)
  184. {
  185. return zend_hash_init(&EG(regular_list), 0, NULL, list_entry_destructor, 0);
  186. }
  187. int init_resource_plist(ELS_D)
  188. {
  189. return zend_hash_init(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1);
  190. }
  191. void destroy_resource_list(ELS_D)
  192. {
  193. zend_hash_graceful_destroy(&EG(regular_list));
  194. }
  195. void destroy_resource_plist(ELS_D)
  196. {
  197. zend_hash_graceful_destroy(&EG(persistent_list));
  198. }
  199. static int clean_module_resource(list_entry *le, int *resource_id)
  200. {
  201. if (le->type == *resource_id) {
  202. return 1;
  203. } else {
  204. return 0;
  205. }
  206. }
  207. int clean_module_resource_destructors(list_destructors_entry *ld, int *module_number)
  208. {
  209. if (ld->module_number == *module_number) {
  210. ELS_FETCH();
  211. zend_hash_apply_with_argument(&EG(persistent_list), (int (*)(void *,void *)) clean_module_resource, (void *) &(ld->resource_id));
  212. return 1;
  213. } else {
  214. return 0;
  215. }
  216. }
  217. /*
  218. * Local variables:
  219. * tab-width: 4
  220. * c-basic-offset: 4
  221. * End:
  222. */