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.

228 lines
4.9 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
  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. #include "zend.h"
  20. #include "zend_list.h"
  21. #include "zend_API.h"
  22. #include "zend_globals.h"
  23. ZEND_API int le_index_ptr;
  24. static inline int zend_list_do_insert(HashTable *list,void *ptr, int type)
  25. {
  26. int index;
  27. list_entry le;
  28. index = zend_hash_next_free_element(list);
  29. if (index==0) index++;
  30. le.ptr=ptr;
  31. le.type=type;
  32. le.refcount=1;
  33. zend_hash_index_update(list, index, (void *) &le, sizeof(list_entry), NULL);
  34. return index;
  35. }
  36. static inline int zend_list_do_delete(HashTable *list,int id)
  37. {
  38. list_entry *le;
  39. ELS_FETCH();
  40. if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
  41. /* printf("del(%d): %d->%d\n", id, le->refcount, le->refcount-1); */
  42. if (--le->refcount<=0) {
  43. return zend_hash_index_del(&EG(regular_list), id);
  44. } else {
  45. return SUCCESS;
  46. }
  47. } else {
  48. return FAILURE;
  49. }
  50. }
  51. static inline void *zend_list_do_find(HashTable *list,int id, int *type)
  52. {
  53. list_entry *le;
  54. if (zend_hash_index_find(list, id, (void **) &le)==SUCCESS) {
  55. *type = le->type;
  56. return le->ptr;
  57. } else {
  58. *type = -1;
  59. return NULL;
  60. }
  61. }
  62. ZEND_API int zend_list_insert(void *ptr, int type)
  63. {
  64. ELS_FETCH();
  65. return zend_list_do_insert(&EG(regular_list), ptr, type);
  66. }
  67. ZEND_API int zend_plist_insert(void *ptr, int type)
  68. {
  69. ELS_FETCH();
  70. return zend_list_do_insert(&EG(persistent_list), ptr, type);
  71. }
  72. ZEND_API int zend_list_addref(int id)
  73. {
  74. list_entry *le;
  75. ELS_FETCH();
  76. if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
  77. /* printf("add(%d): %d->%d\n", id, le->refcount, le->refcount+1); */
  78. le->refcount++;
  79. return SUCCESS;
  80. } else {
  81. return FAILURE;
  82. }
  83. }
  84. ZEND_API int zend_list_delete(int id)
  85. {
  86. ELS_FETCH();
  87. return zend_list_do_delete(&EG(regular_list), id);
  88. }
  89. ZEND_API int zend_plist_delete(int id)
  90. {
  91. ELS_FETCH();
  92. return zend_list_do_delete(&EG(persistent_list), id);
  93. }
  94. ZEND_API void *zend_list_find(int id, int *type)
  95. {
  96. ELS_FETCH();
  97. return zend_list_do_find(&EG(regular_list), id, type);
  98. }
  99. ZEND_API void *zend_plist_find(int id, int *type)
  100. {
  101. ELS_FETCH();
  102. return zend_list_do_find(&EG(persistent_list), id, type);
  103. }
  104. int list_entry_destructor(void *ptr)
  105. {
  106. list_entry *le = (list_entry *) ptr;
  107. list_destructors_entry *ld;
  108. if (zend_hash_index_find(&list_destructors,le->type,(void **) &ld)==SUCCESS) {
  109. if (ld->list_destructor) {
  110. (ld->list_destructor)(le->ptr);
  111. }
  112. } else {
  113. zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)",le->type);
  114. }
  115. return 1;
  116. }
  117. int plist_entry_destructor(void *ptr)
  118. {
  119. list_entry *le = (list_entry *) ptr;
  120. list_destructors_entry *ld;
  121. if (zend_hash_index_find(&list_destructors,le->type,(void **) &ld)==SUCCESS) {
  122. if (ld->plist_destructor) {
  123. (ld->plist_destructor)(le->ptr);
  124. }
  125. } else {
  126. zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)",le->type);
  127. }
  128. return 1;
  129. }
  130. int init_resource_list(ELS_D)
  131. {
  132. return zend_hash_init(&EG(regular_list), 0, NULL, list_entry_destructor, 0);
  133. }
  134. int init_resource_plist(ELS_D)
  135. {
  136. return zend_hash_init(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1);
  137. }
  138. void destroy_resource_list(void)
  139. {
  140. ELS_FETCH();
  141. zend_hash_destroy(&EG(regular_list));
  142. }
  143. void destroy_resource_plist(void)
  144. {
  145. ELS_FETCH();
  146. zend_hash_destroy(&EG(persistent_list));
  147. }
  148. static int clean_module_resource(list_entry *le, int *resource_id)
  149. {
  150. if (le->type == *resource_id) {
  151. return 1;
  152. } else {
  153. return 0;
  154. }
  155. }
  156. int clean_module_resource_destructors(list_destructors_entry *ld, int *module_number)
  157. {
  158. if (ld->module_number == *module_number) {
  159. ELS_FETCH();
  160. zend_hash_apply_with_argument(&EG(persistent_list), (int (*)(void *,void *)) clean_module_resource, (void *) &(ld->resource_id));
  161. return 1;
  162. } else {
  163. return 0;
  164. }
  165. }
  166. /*
  167. * Local variables:
  168. * tab-width: 4
  169. * c-basic-offset: 4
  170. * End:
  171. */