New PHP5 APC - version 3.0.19, using PHP5 5.2.0-8+etch11,
[php5-apc.git] / apc_stack.c
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2008 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Daniel Cowgill <dcowgill@communityconnect.com>              |
16   +----------------------------------------------------------------------+
17
18    This software was contributed to PHP by Community Connect Inc. in 2002
19    and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
20    Future revisions and derivatives of this source code must acknowledge
21    Community Connect Inc. as the original contributor of this module by
22    leaving this note intact in the source code.
23
24    All other licensing and usage conditions are those of the PHP Group.
25
26  */
27
28 /* $Id: apc_stack.c,v 3.4.2.1 2008/05/11 18:57:00 rasmus Exp $ */
29
30 #include "apc_stack.h"
31 #include "apc.h"
32
33 struct apc_stack_t {
34     void** data;
35     int capacity;
36     int size;
37 };
38
39 apc_stack_t* apc_stack_create(int size_hint)
40 {
41     apc_stack_t* stack = (apc_stack_t*) apc_emalloc(sizeof(apc_stack_t));
42
43     stack->capacity = (size_hint > 0) ? size_hint : 10;
44     stack->size = 0;
45     stack->data = (void**) apc_emalloc(sizeof(void*) * stack->capacity);
46
47     return stack;
48 }
49
50 void apc_stack_destroy(apc_stack_t* stack)
51 {
52     if (stack != NULL) {
53         apc_efree(stack->data);
54         apc_efree(stack);
55     }
56 }
57
58 void apc_stack_clear(apc_stack_t* stack)
59 {
60     assert(stack != NULL);
61     stack->size = 0;
62 }
63
64 void apc_stack_push(apc_stack_t* stack, void* item)
65 {
66     assert(stack != NULL);
67     if (stack->size == stack->capacity) {
68         stack->capacity *= 2;
69         stack->data = apc_erealloc(stack->data, sizeof(void*)*stack->capacity);
70     }
71     stack->data[stack->size++] = item;
72 }
73
74 void* apc_stack_pop(apc_stack_t* stack)
75 {
76     assert(stack != NULL && stack->size > 0);
77     return stack->data[--stack->size];
78 }
79
80 void* apc_stack_top(apc_stack_t* stack)
81 {
82     assert(stack != NULL && stack->size > 0);
83     return stack->data[stack->size-1];
84 }
85
86 void* apc_stack_get(apc_stack_t* stack, int n)
87 {
88     assert(stack != NULL && stack->size > n);
89     return stack->data[n];
90 }
91
92 int apc_stack_size(apc_stack_t* stack)
93 {
94     assert(stack != NULL);
95     return stack->size;
96 }
97
98 /*
99  * Local variables:
100  * tab-width: 4
101  * c-basic-offset: 4
102  * End:
103  * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
104  * vim<600: expandtab sw=4 ts=4 sts=4
105  */