Back to index

php5  5.3.10
zend_ptr_stack.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_ptr_stack.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef ZEND_PTR_STACK_H
00023 #define ZEND_PTR_STACK_H
00024 
00025 typedef struct _zend_ptr_stack {
00026        int top, max;
00027        void **elements;
00028        void **top_element;
00029        zend_bool persistent;
00030 } zend_ptr_stack;
00031 
00032 
00033 #define PTR_STACK_BLOCK_SIZE 64
00034 
00035 BEGIN_EXTERN_C()
00036 ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack);
00037 ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent);
00038 ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...);
00039 ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...);
00040 ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack);
00041 ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *));
00042 ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements);
00043 ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack);
00044 END_EXTERN_C()
00045 
00046 #define ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)          \
00047        if (stack->top+count > stack->max) {                                  \
00048               /* we need to allocate more memory */                          \
00049               stack->max *= 2;                                                             \
00050               stack->max += count;                                                  \
00051               stack->elements = (void **) perealloc(stack->elements, (sizeof(void *) * (stack->max)), stack->persistent);     \
00052               stack->top_element = stack->elements+stack->top; \
00053        }
00054 
00055 /*     Not doing this with a macro because of the loop unrolling in the element assignment.
00056        Just using a macro for 3 in the body for readability sake. */
00057 static inline void zend_ptr_stack_3_push(zend_ptr_stack *stack, void *a, void *b, void *c)
00058 {
00059 #define ZEND_PTR_STACK_NUM_ARGS 3
00060 
00061        ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
00062 
00063        stack->top += ZEND_PTR_STACK_NUM_ARGS;
00064        *(stack->top_element++) = a;
00065        *(stack->top_element++) = b;
00066        *(stack->top_element++) = c;
00067 
00068 #undef ZEND_PTR_STACK_NUM_ARGS
00069 }
00070 
00071 static inline void zend_ptr_stack_2_push(zend_ptr_stack *stack, void *a, void *b)
00072 {
00073 #define ZEND_PTR_STACK_NUM_ARGS 2
00074 
00075        ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
00076 
00077        stack->top += ZEND_PTR_STACK_NUM_ARGS;
00078        *(stack->top_element++) = a;
00079        *(stack->top_element++) = b;
00080 
00081 #undef ZEND_PTR_STACK_NUM_ARGS
00082 }
00083 
00084 static inline void zend_ptr_stack_3_pop(zend_ptr_stack *stack, void **a, void **b, void **c)
00085 {
00086        *a = *(--stack->top_element);
00087        *b = *(--stack->top_element);
00088        *c = *(--stack->top_element);
00089        stack->top -= 3;
00090 }
00091 
00092 static inline void zend_ptr_stack_2_pop(zend_ptr_stack *stack, void **a, void **b)
00093 {
00094        *a = *(--stack->top_element);
00095        *b = *(--stack->top_element);
00096        stack->top -= 2;
00097 }
00098 
00099 static inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
00100 {
00101        ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, 1)
00102 
00103        stack->top++;
00104        *(stack->top_element++) = ptr;
00105 }
00106 
00107 static inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
00108 {
00109        stack->top--;
00110        return *(--stack->top_element);
00111 }
00112 
00113 #endif /* ZEND_PTR_STACK_H */
00114 
00115 /*
00116  * Local variables:
00117  * tab-width: 4
00118  * c-basic-offset: 4
00119  * indent-tabs-mode: t
00120  * End:
00121  */