zend_ptr_stack.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef ZEND_PTR_STACK_H
  20. #define ZEND_PTR_STACK_H
  21. typedef struct _zend_ptr_stack {
  22. int top, max;
  23. void **elements;
  24. void **top_element;
  25. bool persistent;
  26. } zend_ptr_stack;
  27. #define PTR_STACK_BLOCK_SIZE 64
  28. BEGIN_EXTERN_C()
  29. ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack);
  30. ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, bool persistent);
  31. ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...);
  32. ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...);
  33. ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack);
  34. ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *));
  35. ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *));
  36. ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), 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. static zend_always_inline void *zend_ptr_stack_top(zend_ptr_stack *stack)
  94. {
  95. return stack->elements[stack->top - 1];
  96. }
  97. #endif /* ZEND_PTR_STACK_H */