Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 72 lines (56 sloc) 2.178 kb
573b460 andigutmans Zend Library
andigutmans authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski |
6 +----------------------------------------------------------------------+
7 | This source file is subject to the Zend license, that is bundled |
8 | with this package in the file LICENSE. If you did not receive a |
9 | copy of the Zend license, please mail us at zend@zend.com so we can |
10 | send you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Authors: Andi Gutmans <andi@zend.com> |
13 | Zeev Suraski <zeev@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #include "zend.h"
18 #include "zend_ptr_stack.h"
19
20
21 ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
22 {
23 stack->top_element = stack->elements = (void **) emalloc(sizeof(void *)*STACK_BLOCK_SIZE);
24 stack->max = STACK_BLOCK_SIZE;
25 stack->top = 0;
26 }
27
28
29 ZEND_API inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
30 {
31 if (stack->top >= stack->max) { /* we need to allocate more memory */
32 short diff = stack->top_element-stack->elements;
33
34 stack->elements = (void **) erealloc(stack->elements, (sizeof(void *) * (stack->max *= 2 )));
35 stack->top_element = stack->elements+diff;
36 }
37 stack->top++;
38 *(stack->top_element++) = ptr;
39 }
40
41
42 ZEND_API inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
43 {
44 stack->top--;
45 return *(--stack->top_element);
46 }
47
48
49 ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
50 {
51 if (stack->elements) {
52 efree(stack->elements);
53 }
54 }
55
56
57 ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
58 {
59 int i = stack->top;
60
61 while (--i >= 0) {
62 func(stack->elements[i]);
63 }
64 }
65
66
67 ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *))
68 {
69 zend_ptr_stack_apply(stack, func);
70 stack->top = 0;
71 }
Something went wrong with that request. Please try again.