zend_stack.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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 int 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. return SUCCESS;
  29. }
  30. ZEND_API int zend_stack_push(zend_stack *stack, const void *element)
  31. {
  32. /* We need to allocate more memory */
  33. if (stack->top >= stack->max) {
  34. stack->max += STACK_BLOCK_SIZE;
  35. stack->elements = safe_erealloc(stack->elements, stack->size, stack->max, 0);
  36. }
  37. memcpy(ZEND_STACK_ELEMENT(stack, stack->top), element, stack->size);
  38. return stack->top++;
  39. }
  40. ZEND_API void *zend_stack_top(const zend_stack *stack)
  41. {
  42. if (stack->top > 0) {
  43. return ZEND_STACK_ELEMENT(stack, stack->top - 1);
  44. } else {
  45. return NULL;
  46. }
  47. }
  48. ZEND_API int zend_stack_del_top(zend_stack *stack)
  49. {
  50. --stack->top;
  51. return SUCCESS;
  52. }
  53. ZEND_API int zend_stack_int_top(const zend_stack *stack)
  54. {
  55. int *e = zend_stack_top(stack);
  56. if (e) {
  57. return *e;
  58. } else {
  59. return FAILURE;
  60. }
  61. }
  62. ZEND_API int zend_stack_is_empty(const zend_stack *stack)
  63. {
  64. return stack->top == 0;
  65. }
  66. ZEND_API int zend_stack_destroy(zend_stack *stack)
  67. {
  68. if (stack->elements) {
  69. efree(stack->elements);
  70. stack->elements = NULL;
  71. }
  72. return SUCCESS;
  73. }
  74. ZEND_API void *zend_stack_base(const zend_stack *stack)
  75. {
  76. return stack->elements;
  77. }
  78. ZEND_API int zend_stack_count(const zend_stack *stack)
  79. {
  80. return stack->top;
  81. }
  82. ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
  83. {
  84. int i;
  85. switch (type) {
  86. case ZEND_STACK_APPLY_TOPDOWN:
  87. for (i=stack->top-1; i>=0; i--) {
  88. if (apply_function(ZEND_STACK_ELEMENT(stack, i))) {
  89. break;
  90. }
  91. }
  92. break;
  93. case ZEND_STACK_APPLY_BOTTOMUP:
  94. for (i=0; i<stack->top; i++) {
  95. if (apply_function(ZEND_STACK_ELEMENT(stack, i))) {
  96. break;
  97. }
  98. }
  99. break;
  100. }
  101. }
  102. ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  103. {
  104. int i;
  105. switch (type) {
  106. case ZEND_STACK_APPLY_TOPDOWN:
  107. for (i=stack->top-1; i>=0; i--) {
  108. if (apply_function(ZEND_STACK_ELEMENT(stack, i), arg)) {
  109. break;
  110. }
  111. }
  112. break;
  113. case ZEND_STACK_APPLY_BOTTOMUP:
  114. for (i=0; i<stack->top; i++) {
  115. if (apply_function(ZEND_STACK_ELEMENT(stack, i), arg)) {
  116. break;
  117. }
  118. }
  119. break;
  120. }
  121. }
  122. ZEND_API void zend_stack_clean(zend_stack *stack, void (*func)(void *), zend_bool free_elements)
  123. {
  124. int i;
  125. if (func) {
  126. for (i = 0; i < stack->top; i++) {
  127. func(ZEND_STACK_ELEMENT(stack, i));
  128. }
  129. }
  130. if (free_elements) {
  131. if (stack->elements) {
  132. efree(stack->elements);
  133. stack->elements = NULL;
  134. }
  135. stack->top = stack->max = 0;
  136. }
  137. }
  138. /*
  139. * Local variables:
  140. * tab-width: 4
  141. * c-basic-offset: 4
  142. * indent-tabs-mode: t
  143. * End:
  144. * vim600: sw=4 ts=4 fdm=marker
  145. * vim<600: sw=4 ts=4
  146. */