zend_stack.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_stack.h"
  21. #define ZEND_STACK_ELEMENT(stack, n) ((void *)((char *) (stack)->elements + (stack)->size * (n)))
  22. ZEND_API void zend_stack_init(zend_stack *stack, int size)
  23. {
  24. stack->size = size;
  25. stack->top = 0;
  26. stack->max = 0;
  27. stack->elements = NULL;
  28. }
  29. ZEND_API int zend_stack_push(zend_stack *stack, const void *element)
  30. {
  31. /* We need to allocate more memory */
  32. if (stack->top >= stack->max) {
  33. stack->max += STACK_BLOCK_SIZE;
  34. stack->elements = safe_erealloc(stack->elements, stack->size, stack->max, 0);
  35. }
  36. memcpy(ZEND_STACK_ELEMENT(stack, stack->top), element, stack->size);
  37. return stack->top++;
  38. }
  39. ZEND_API void *zend_stack_top(const zend_stack *stack)
  40. {
  41. if (stack->top > 0) {
  42. return ZEND_STACK_ELEMENT(stack, stack->top - 1);
  43. } else {
  44. return NULL;
  45. }
  46. }
  47. ZEND_API void zend_stack_del_top(zend_stack *stack)
  48. {
  49. --stack->top;
  50. }
  51. ZEND_API int zend_stack_int_top(const zend_stack *stack)
  52. {
  53. int *e = zend_stack_top(stack);
  54. if (e) {
  55. return *e;
  56. } else {
  57. return FAILURE;
  58. }
  59. }
  60. ZEND_API bool zend_stack_is_empty(const zend_stack *stack)
  61. {
  62. return stack->top == 0;
  63. }
  64. ZEND_API void zend_stack_destroy(zend_stack *stack)
  65. {
  66. if (stack->elements) {
  67. efree(stack->elements);
  68. stack->elements = NULL;
  69. }
  70. }
  71. ZEND_API void *zend_stack_base(const zend_stack *stack)
  72. {
  73. return stack->elements;
  74. }
  75. ZEND_API int zend_stack_count(const zend_stack *stack)
  76. {
  77. return stack->top;
  78. }
  79. ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
  80. {
  81. int i;
  82. switch (type) {
  83. case ZEND_STACK_APPLY_TOPDOWN:
  84. for (i=stack->top-1; i>=0; i--) {
  85. if (apply_function(ZEND_STACK_ELEMENT(stack, i))) {
  86. break;
  87. }
  88. }
  89. break;
  90. case ZEND_STACK_APPLY_BOTTOMUP:
  91. for (i=0; i<stack->top; i++) {
  92. if (apply_function(ZEND_STACK_ELEMENT(stack, i))) {
  93. break;
  94. }
  95. }
  96. break;
  97. }
  98. }
  99. ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  100. {
  101. int i;
  102. switch (type) {
  103. case ZEND_STACK_APPLY_TOPDOWN:
  104. for (i=stack->top-1; i>=0; i--) {
  105. if (apply_function(ZEND_STACK_ELEMENT(stack, i), arg)) {
  106. break;
  107. }
  108. }
  109. break;
  110. case ZEND_STACK_APPLY_BOTTOMUP:
  111. for (i=0; i<stack->top; i++) {
  112. if (apply_function(ZEND_STACK_ELEMENT(stack, i), arg)) {
  113. break;
  114. }
  115. }
  116. break;
  117. }
  118. }
  119. ZEND_API void zend_stack_clean(zend_stack *stack, void (*func)(void *), bool free_elements)
  120. {
  121. int i;
  122. if (func) {
  123. for (i = 0; i < stack->top; i++) {
  124. func(ZEND_STACK_ELEMENT(stack, i));
  125. }
  126. }
  127. if (free_elements) {
  128. if (stack->elements) {
  129. efree(stack->elements);
  130. stack->elements = NULL;
  131. }
  132. stack->top = stack->max = 0;
  133. }
  134. }