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.

122 lines
4.1 KiB

28 years ago
14 years ago
28 years ago
25 years ago
27 years ago
25 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2013 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. #ifndef ZEND_PTR_STACK_H
  21. #define ZEND_PTR_STACK_H
  22. typedef struct _zend_ptr_stack {
  23. int top, max;
  24. void **elements;
  25. void **top_element;
  26. zend_bool persistent;
  27. } zend_ptr_stack;
  28. #define PTR_STACK_BLOCK_SIZE 64
  29. BEGIN_EXTERN_C()
  30. ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack);
  31. ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent);
  32. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...);
  33. ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...);
  34. ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack);
  35. ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *));
  36. ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements);
  37. ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack);
  38. END_EXTERN_C()
  39. #define ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count) \
  40. if (stack->top+count > stack->max) { \
  41. /* we need to allocate more memory */ \
  42. do { \
  43. stack->max += PTR_STACK_BLOCK_SIZE; \
  44. } while (stack->top+count > stack->max); \
  45. stack->elements = (void **) perealloc(stack->elements, (sizeof(void *) * (stack->max)), stack->persistent); \
  46. stack->top_element = stack->elements+stack->top; \
  47. }
  48. /* Not doing this with a macro because of the loop unrolling in the element assignment.
  49. Just using a macro for 3 in the body for readability sake. */
  50. static zend_always_inline void zend_ptr_stack_3_push(zend_ptr_stack *stack, void *a, void *b, void *c)
  51. {
  52. #define ZEND_PTR_STACK_NUM_ARGS 3
  53. ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
  54. stack->top += ZEND_PTR_STACK_NUM_ARGS;
  55. *(stack->top_element++) = a;
  56. *(stack->top_element++) = b;
  57. *(stack->top_element++) = c;
  58. #undef ZEND_PTR_STACK_NUM_ARGS
  59. }
  60. static zend_always_inline void zend_ptr_stack_2_push(zend_ptr_stack *stack, void *a, void *b)
  61. {
  62. #define ZEND_PTR_STACK_NUM_ARGS 2
  63. ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
  64. stack->top += ZEND_PTR_STACK_NUM_ARGS;
  65. *(stack->top_element++) = a;
  66. *(stack->top_element++) = b;
  67. #undef ZEND_PTR_STACK_NUM_ARGS
  68. }
  69. static zend_always_inline void zend_ptr_stack_3_pop(zend_ptr_stack *stack, void **a, void **b, void **c)
  70. {
  71. *a = *(--stack->top_element);
  72. *b = *(--stack->top_element);
  73. *c = *(--stack->top_element);
  74. stack->top -= 3;
  75. }
  76. static zend_always_inline void zend_ptr_stack_2_pop(zend_ptr_stack *stack, void **a, void **b)
  77. {
  78. *a = *(--stack->top_element);
  79. *b = *(--stack->top_element);
  80. stack->top -= 2;
  81. }
  82. static zend_always_inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
  83. {
  84. ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, 1)
  85. stack->top++;
  86. *(stack->top_element++) = ptr;
  87. }
  88. static zend_always_inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
  89. {
  90. stack->top--;
  91. return *(--stack->top_element);
  92. }
  93. #endif /* ZEND_PTR_STACK_H */
  94. /*
  95. * Local variables:
  96. * tab-width: 4
  97. * c-basic-offset: 4
  98. * indent-tabs-mode: t
  99. * End:
  100. */