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.

114 lines
3.2 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
  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_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 inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
  31. {
  32. if (stack->top >= stack->max) { /* we need to allocate more memory */
  33. stack->elements = (void **) erealloc(stack->elements, (sizeof(void *) * (stack->max *= 2 )));
  34. stack->top_element = stack->elements+stack->top;
  35. }
  36. stack->top++;
  37. *(stack->top_element++) = ptr;
  38. }
  39. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
  40. {
  41. va_list ptr;
  42. void *elem;
  43. if (stack->top+count > stack->max) { /* we need to allocate more memory */
  44. stack->max *= 2;
  45. stack->max += count;
  46. stack->elements = (void **) erealloc(stack->elements, (sizeof(void *) * (stack->max)));
  47. stack->top_element = stack->elements+stack->top;
  48. }
  49. va_start(ptr, count);
  50. while (count>0) {
  51. elem = va_arg(ptr, void *);
  52. stack->top++;
  53. *(stack->top_element++) = elem;
  54. count--;
  55. }
  56. va_end(ptr);
  57. }
  58. ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
  59. {
  60. va_list ptr;
  61. void **elem;
  62. va_start(ptr, count);
  63. while (count>0) {
  64. elem = va_arg(ptr, void **);
  65. *elem = *(--stack->top_element);
  66. stack->top--;
  67. count--;
  68. }
  69. va_end(ptr);
  70. }
  71. ZEND_API inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
  72. {
  73. stack->top--;
  74. return *(--stack->top_element);
  75. }
  76. ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
  77. {
  78. if (stack->elements) {
  79. efree(stack->elements);
  80. }
  81. }
  82. ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
  83. {
  84. int i = stack->top;
  85. while (--i >= 0) {
  86. func(stack->elements[i]);
  87. }
  88. }
  89. ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *))
  90. {
  91. zend_ptr_stack_apply(stack, func);
  92. stack->top = 0;
  93. stack->top_element = stack->elements;
  94. }