zend_ptr_stack.c 2.9 KB

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