zend_stack.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_stack.h"
  22. ZEND_API int zend_stack_init(zend_stack *stack)
  23. {
  24. stack->top = 0;
  25. stack->max = 0;
  26. stack->elements = NULL;
  27. return SUCCESS;
  28. }
  29. ZEND_API int zend_stack_push(zend_stack *stack, const void *element, int size)
  30. {
  31. if (stack->top >= stack->max) { /* we need to allocate more memory */
  32. stack->elements = (void **) erealloc(stack->elements,
  33. (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
  34. if (!stack->elements) {
  35. return FAILURE;
  36. }
  37. }
  38. stack->elements[stack->top] = (void *) emalloc(size);
  39. memcpy(stack->elements[stack->top], element, size);
  40. return stack->top++;
  41. }
  42. ZEND_API int zend_stack_top(const zend_stack *stack, void **element)
  43. {
  44. if (stack->top > 0) {
  45. *element = stack->elements[stack->top - 1];
  46. return SUCCESS;
  47. } else {
  48. *element = NULL;
  49. return FAILURE;
  50. }
  51. }
  52. ZEND_API int zend_stack_del_top(zend_stack *stack)
  53. {
  54. if (stack->top > 0) {
  55. efree(stack->elements[--stack->top]);
  56. }
  57. return SUCCESS;
  58. }
  59. ZEND_API int zend_stack_int_top(const zend_stack *stack)
  60. {
  61. int *e;
  62. if (zend_stack_top(stack, (void **) &e) == FAILURE) {
  63. return FAILURE; /* this must be a negative number, since negative numbers can't be address numbers */
  64. } else {
  65. return *e;
  66. }
  67. }
  68. ZEND_API int zend_stack_is_empty(const zend_stack *stack)
  69. {
  70. if (stack->top == 0) {
  71. return 1;
  72. } else {
  73. return 0;
  74. }
  75. }
  76. ZEND_API int zend_stack_destroy(zend_stack *stack)
  77. {
  78. int i;
  79. if (stack->elements) {
  80. for (i = 0; i < stack->top; i++) {
  81. efree(stack->elements[i]);
  82. }
  83. efree(stack->elements);
  84. stack->elements = NULL;
  85. }
  86. return SUCCESS;
  87. }
  88. ZEND_API void **zend_stack_base(const zend_stack *stack)
  89. {
  90. return stack->elements;
  91. }
  92. ZEND_API int zend_stack_count(const zend_stack *stack)
  93. {
  94. return stack->top;
  95. }
  96. ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
  97. {
  98. int i;
  99. switch (type) {
  100. case ZEND_STACK_APPLY_TOPDOWN:
  101. for (i=stack->top-1; i>=0; i--) {
  102. if (apply_function(stack->elements[i])) {
  103. break;
  104. }
  105. }
  106. break;
  107. case ZEND_STACK_APPLY_BOTTOMUP:
  108. for (i=0; i<stack->top; i++) {
  109. if (apply_function(stack->elements[i])) {
  110. break;
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  117. {
  118. int i;
  119. switch (type) {
  120. case ZEND_STACK_APPLY_TOPDOWN:
  121. for (i=stack->top-1; i>=0; i--) {
  122. if (apply_function(stack->elements[i], arg)) {
  123. break;
  124. }
  125. }
  126. break;
  127. case ZEND_STACK_APPLY_BOTTOMUP:
  128. for (i=0; i<stack->top; i++) {
  129. if (apply_function(stack->elements[i], arg)) {
  130. break;
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. /*
  137. * Local variables:
  138. * tab-width: 4
  139. * c-basic-offset: 4
  140. * indent-tabs-mode: t
  141. * End:
  142. */