Back to index

php5  5.3.10
zend_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_stack.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include "zend_stack.h"
00024 
00025 ZEND_API int zend_stack_init(zend_stack *stack)
00026 {
00027        stack->top = 0;
00028        stack->elements = (void **) emalloc(sizeof(void **) * STACK_BLOCK_SIZE);
00029        if (!stack->elements) {
00030               return FAILURE;
00031        } else {
00032               stack->max = STACK_BLOCK_SIZE;
00033               return SUCCESS;
00034        }
00035 }
00036 
00037 ZEND_API int zend_stack_push(zend_stack *stack, const void *element, int size)
00038 {
00039        if (stack->top >= stack->max) {           /* we need to allocate more memory */
00040               stack->elements = (void **) erealloc(stack->elements,
00041                                (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
00042               if (!stack->elements) {
00043                      return FAILURE;
00044               }
00045        }
00046        stack->elements[stack->top] = (void *) emalloc(size);
00047        memcpy(stack->elements[stack->top], element, size);
00048        return stack->top++;
00049 }
00050 
00051 
00052 ZEND_API int zend_stack_top(const zend_stack *stack, void **element)
00053 {
00054        if (stack->top > 0) {
00055               *element = stack->elements[stack->top - 1];
00056               return SUCCESS;
00057        } else {
00058               *element = NULL;
00059               return FAILURE;
00060        }
00061 }
00062 
00063 
00064 ZEND_API int zend_stack_del_top(zend_stack *stack)
00065 {
00066        if (stack->top > 0) {
00067               efree(stack->elements[--stack->top]);
00068        }
00069        return SUCCESS;
00070 }
00071 
00072 
00073 ZEND_API int zend_stack_int_top(const zend_stack *stack)
00074 {
00075        int *e;
00076 
00077        if (zend_stack_top(stack, (void **) &e) == FAILURE) {
00078               return FAILURE;                    /* this must be a negative number, since negative numbers can't be address numbers */
00079        } else {
00080               return *e;
00081        }
00082 }
00083 
00084 
00085 ZEND_API int zend_stack_is_empty(const zend_stack *stack)
00086 {
00087        if (stack->top == 0) {
00088               return 1;
00089        } else {
00090               return 0;
00091        }
00092 }
00093 
00094 
00095 ZEND_API int zend_stack_destroy(zend_stack *stack)
00096 {
00097        int i;
00098 
00099        if (stack->elements) {
00100               for (i = 0; i < stack->top; i++) {
00101                      efree(stack->elements[i]);
00102               }
00103 
00104               efree(stack->elements);
00105        }
00106 
00107        return SUCCESS;
00108 }
00109 
00110 
00111 ZEND_API void **zend_stack_base(const zend_stack *stack)
00112 {
00113        return stack->elements;
00114 }
00115 
00116 
00117 ZEND_API int zend_stack_count(const zend_stack *stack)
00118 {
00119        return stack->top;
00120 }
00121 
00122 
00123 ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
00124 {
00125        int i;
00126 
00127        switch (type) {
00128               case ZEND_STACK_APPLY_TOPDOWN:
00129                      for (i=stack->top-1; i>=0; i--) {
00130                             if (apply_function(stack->elements[i])) {
00131                                    break;
00132                             }
00133                      }
00134                      break;
00135               case ZEND_STACK_APPLY_BOTTOMUP:
00136                      for (i=0; i<stack->top; i++) {
00137                             if (apply_function(stack->elements[i])) {
00138                                    break;
00139                             }
00140                      }
00141                      break;
00142        }
00143 }
00144 
00145 
00146 ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
00147 {
00148        int i;
00149 
00150        switch (type) {
00151               case ZEND_STACK_APPLY_TOPDOWN:
00152                      for (i=stack->top-1; i>=0; i--) {
00153                             if (apply_function(stack->elements[i], arg)) {
00154                                    break;
00155                             }
00156                      }
00157                      break;
00158               case ZEND_STACK_APPLY_BOTTOMUP:
00159                      for (i=0; i<stack->top; i++) {
00160                             if (apply_function(stack->elements[i], arg)) {
00161                                    break;
00162                             }
00163                      }
00164                      break;
00165        }
00166 }
00167 
00168 /*
00169  * Local variables:
00170  * tab-width: 4
00171  * c-basic-offset: 4
00172  * indent-tabs-mode: t
00173  * End:
00174  */