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.

174 lines
4.0 KiB

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