Back to index

php5  5.3.10
zend_ptr_stack.c
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.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include "zend_ptr_stack.h"
00024 #ifdef HAVE_STDARG_H
00025 # include <stdarg.h>
00026 #endif
00027 
00028 ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent)
00029 {
00030        stack->top_element = stack->elements = (void **) pemalloc(sizeof(void *)*PTR_STACK_BLOCK_SIZE, persistent);
00031        stack->max = PTR_STACK_BLOCK_SIZE;
00032        stack->top = 0;
00033        stack->persistent = persistent;
00034 }
00035 
00036 ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
00037 {
00038        zend_ptr_stack_init_ex(stack, 0);
00039 }
00040 
00041 
00042 ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
00043 {
00044        va_list ptr;
00045        void *elem;
00046        
00047        ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
00048 
00049        va_start(ptr, count);
00050        while (count>0) {
00051               elem = va_arg(ptr, void *);
00052               stack->top++;
00053               *(stack->top_element++) = elem;
00054               count--;
00055        }
00056        va_end(ptr);
00057 }
00058 
00059 
00060 ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
00061 {
00062        va_list ptr;
00063        void **elem;
00064        
00065        va_start(ptr, count);
00066        while (count>0) {
00067               elem = va_arg(ptr, void **);
00068               *elem = *(--stack->top_element);
00069               stack->top--;
00070               count--;
00071        }
00072        va_end(ptr);
00073 }
00074 
00075 
00076 
00077 ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
00078 {
00079        if (stack->elements) {
00080               pefree(stack->elements, stack->persistent);
00081        }
00082 }
00083 
00084 
00085 ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
00086 {
00087        int i = stack->top;
00088 
00089        while (--i >= 0) {
00090               func(stack->elements[i]);
00091        }
00092 }
00093 
00094 
00095 ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements)
00096 {
00097        zend_ptr_stack_apply(stack, func);
00098        if (free_elements) {
00099               int i = stack->top;
00100 
00101               while (--i >= 0) {
00102                      pefree(stack->elements[i], stack->persistent);
00103               }
00104        }
00105        stack->top = 0;
00106        stack->top_element = stack->elements;
00107 }
00108 
00109 
00110 ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
00111 {
00112        return stack->top;
00113 }
00114 
00115 /*
00116  * Local variables:
00117  * tab-width: 4
00118  * c-basic-offset: 4
00119  * indent-tabs-mode: t
00120  * End:
00121  */