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.

110 lines
2.9 KiB

28 years ago
24 years ago
28 years ago
25 years ago
27 years ago
25 years ago
27 years ago
28 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2003 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 at through the world-wide-web at |
  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. #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(zend_ptr_stack *stack)
  25. {
  26. stack->top_element = stack->elements = (void **) emalloc(sizeof(void *)*PTR_STACK_BLOCK_SIZE);
  27. stack->max = PTR_STACK_BLOCK_SIZE;
  28. stack->top = 0;
  29. }
  30. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
  31. {
  32. va_list ptr;
  33. void *elem;
  34. if (stack->top+count > stack->max) { /* we need to allocate more memory */
  35. stack->max *= 2;
  36. stack->max += count;
  37. stack->elements = (void **) erealloc(stack->elements, (sizeof(void *) * (stack->max)));
  38. stack->top_element = stack->elements+stack->top;
  39. }
  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. efree(stack->elements);
  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. efree(stack->elements[i]);
  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. }