zend_ptr_stack.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "zend.h"
  20. #include "zend_ptr_stack.h"
  21. #include <stdarg.h>
  22. ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, bool persistent)
  23. {
  24. stack->top_element = stack->elements = NULL;
  25. stack->top = stack->max = 0;
  26. stack->persistent = persistent;
  27. }
  28. ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
  29. {
  30. zend_ptr_stack_init_ex(stack, 0);
  31. }
  32. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
  33. {
  34. va_list ptr;
  35. void *elem;
  36. ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
  37. va_start(ptr, count);
  38. while (count>0) {
  39. elem = va_arg(ptr, void *);
  40. stack->top++;
  41. *(stack->top_element++) = elem;
  42. count--;
  43. }
  44. va_end(ptr);
  45. }
  46. ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
  47. {
  48. va_list ptr;
  49. void **elem;
  50. va_start(ptr, count);
  51. while (count>0) {
  52. elem = va_arg(ptr, void **);
  53. *elem = *(--stack->top_element);
  54. stack->top--;
  55. count--;
  56. }
  57. va_end(ptr);
  58. }
  59. ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
  60. {
  61. if (stack->elements) {
  62. pefree(stack->elements, stack->persistent);
  63. }
  64. }
  65. ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
  66. {
  67. int i = stack->top;
  68. while (--i >= 0) {
  69. func(stack->elements[i]);
  70. }
  71. }
  72. ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
  73. {
  74. int i = 0;
  75. while (i < stack->top) {
  76. func(stack->elements[i++]);
  77. }
  78. }
  79. ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), bool free_elements)
  80. {
  81. zend_ptr_stack_apply(stack, func);
  82. if (free_elements) {
  83. int i = stack->top;
  84. while (--i >= 0) {
  85. pefree(stack->elements[i], stack->persistent);
  86. }
  87. }
  88. stack->top = 0;
  89. stack->top_element = stack->elements;
  90. }
  91. ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
  92. {
  93. return stack->top;
  94. }