Back to index

php5  5.3.10
zend_dynamic_array.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_dynamic_array.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 
00024 typedef struct _dynamic_array {
00025         char *array;
00026         unsigned int element_size;
00027         unsigned int current;
00028         unsigned int allocated;
00029 } dynamic_array;
00030 
00031 ZEND_API int zend_dynamic_array_init(dynamic_array *da, unsigned int element_size, unsigned int size)
00032 {
00033        da->element_size = element_size;
00034        da->allocated = size;
00035        da->current = 0;
00036        da->array = (char *) emalloc(size*element_size);
00037        if (da->array == NULL) {
00038               return 1;
00039        }
00040        return 0;
00041 }
00042 
00043 ZEND_API void *zend_dynamic_array_push(dynamic_array *da)
00044 {
00045        if (da->current == da->allocated) {
00046               da->allocated *= 2;
00047               da->array = (char *) erealloc(da->array, da->allocated*da->element_size);
00048        }
00049        return (void *)(da->array+(da->current++)*da->element_size);
00050 }
00051 
00052 ZEND_API void *zend_dynamic_array_pop(dynamic_array *da)
00053 {
00054        return (void *)(da->array+(--(da->current))*da->element_size);
00055 
00056 }
00057 
00058 ZEND_API void *zend_dynamic_array_get_element(dynamic_array *da, unsigned int index)
00059 {
00060        if (index >= da->current) {
00061               return NULL;
00062        }
00063        return (void *)(da->array+index*da->element_size);
00064 }
00065 
00066 /*
00067  * Local variables:
00068  * tab-width: 4
00069  * c-basic-offset: 4
00070  * indent-tabs-mode: t
00071  * End:
00072  */