Coverage Report

Created: 2025-06-13 06:43

/src/php-src/Zend/zend_ptr_stack.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend.h"
21
#include "zend_ptr_stack.h"
22
#include <stdarg.h>
23
24
ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, bool persistent)
25
637k
{
26
637k
  stack->top_element = stack->elements = NULL;
27
637k
  stack->top = stack->max = 0;
28
637k
  stack->persistent = persistent;
29
637k
}
30
31
ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
32
637k
{
33
637k
  zend_ptr_stack_init_ex(stack, 0);
34
637k
}
35
36
37
ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
38
0
{
39
0
  va_list ptr;
40
0
  void *elem;
41
42
0
  ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
43
44
0
  va_start(ptr, count);
45
0
  while (count>0) {
46
0
    elem = va_arg(ptr, void *);
47
0
    stack->top++;
48
0
    *(stack->top_element++) = elem;
49
0
    count--;
50
0
  }
51
0
  va_end(ptr);
52
0
}
53
54
55
ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
56
0
{
57
0
  va_list ptr;
58
0
  void **elem;
59
60
0
  va_start(ptr, count);
61
0
  while (count>0) {
62
0
    elem = va_arg(ptr, void **);
63
0
    *elem = *(--stack->top_element);
64
0
    stack->top--;
65
0
    count--;
66
0
  }
67
0
  va_end(ptr);
68
0
}
69
70
71
72
ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
73
631k
{
74
631k
  if (stack->elements) {
75
197k
    pefree(stack->elements, stack->persistent);
76
197k
  }
77
631k
}
78
79
80
ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
81
631k
{
82
631k
  int i = stack->top;
83
84
5.61M
  while (--i >= 0) {
85
4.97M
    func(stack->elements[i]);
86
4.97M
  }
87
631k
}
88
89
ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
90
165k
{
91
165k
  int i = 0;
92
93
2.31M
  while (i < stack->top) {
94
2.15M
    func(stack->elements[i++]);
95
2.15M
  }
96
165k
}
97
98
99
ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), bool free_elements)
100
631k
{
101
631k
  zend_ptr_stack_apply(stack, func);
102
631k
  if (free_elements) {
103
631k
    int i = stack->top;
104
105
5.61M
    while (--i >= 0) {
106
4.97M
      pefree(stack->elements[i], stack->persistent);
107
4.97M
    }
108
631k
  }
109
631k
  stack->top = 0;
110
631k
  stack->top_element = stack->elements;
111
631k
}
112
113
114
ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
115
0
{
116
0
  return stack->top;
117
0
}