zend_ptr_stack.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "zend.h"
  20. #include "zend_ptr_stack.h"
  21. #ifdef HAVE_STDARG_H
  22. # include <stdarg.h>
  23. #endif
  24. ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent)
  25. {
  26. stack->top_element = stack->elements = NULL;
  27. stack->top = stack->max = 0;
  28. stack->persistent = persistent;
  29. }
  30. ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
  31. {
  32. zend_ptr_stack_init_ex(stack, 0);
  33. }
  34. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
  35. {
  36. va_list ptr;
  37. void *elem;
  38. ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
  39. va_start(ptr, count);
  40. while (count>0) {
  41. elem = va_arg(ptr, void *);
  42. stack->top++;
  43. *(stack->top_element++) = elem;
  44. count--;
  45. }
  46. va_end(ptr);
  47. }
  48. ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
  49. {
  50. va_list ptr;
  51. void **elem;
  52. va_start(ptr, count);
  53. while (count>0) {
  54. elem = va_arg(ptr, void **);
  55. *elem = *(--stack->top_element);
  56. stack->top--;
  57. count--;
  58. }
  59. va_end(ptr);
  60. }
  61. ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
  62. {
  63. if (stack->elements) {
  64. pefree(stack->elements, stack->persistent);
  65. }
  66. }
  67. ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
  68. {
  69. int i = stack->top;
  70. while (--i >= 0) {
  71. func(stack->elements[i]);
  72. }
  73. }
  74. ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
  75. {
  76. int i = 0;
  77. while (i < stack->top) {
  78. func(stack->elements[i++]);
  79. }
  80. }
  81. ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements)
  82. {
  83. zend_ptr_stack_apply(stack, func);
  84. if (free_elements) {
  85. int i = stack->top;
  86. while (--i >= 0) {
  87. pefree(stack->elements[i], stack->persistent);
  88. }
  89. }
  90. stack->top = 0;
  91. stack->top_element = stack->elements;
  92. }
  93. ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
  94. {
  95. return stack->top;
  96. }
  97. /*
  98. * Local variables:
  99. * tab-width: 4
  100. * c-basic-offset: 4
  101. * indent-tabs-mode: t
  102. * End:
  103. * vim600: sw=4 ts=4 fdm=marker
  104. * vim<600: sw=4 ts=4
  105. */