Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_ptr_stack.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend.h"
20
#include "zend_ptr_stack.h"
21
#include <stdarg.h>
22
23
ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, bool persistent)
24
104k
{
25
104k
  stack->top_element = stack->elements = NULL;
26
104k
  stack->top = stack->max = 0;
27
104k
  stack->persistent = persistent;
28
104k
}
29
30
ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
31
104k
{
32
104k
  zend_ptr_stack_init_ex(stack, false);
33
104k
}
34
35
36
ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
37
0
{
38
0
  va_list ptr;
39
0
  void *elem;
40
41
0
  ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
42
43
0
  va_start(ptr, count);
44
0
  while (count>0) {
45
0
    elem = va_arg(ptr, void *);
46
0
    stack->top++;
47
0
    *(stack->top_element++) = elem;
48
0
    count--;
49
0
  }
50
0
  va_end(ptr);
51
0
}
52
53
54
ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
55
0
{
56
0
  va_list ptr;
57
0
  void **elem;
58
59
0
  va_start(ptr, count);
60
0
  while (count>0) {
61
0
    elem = va_arg(ptr, void **);
62
0
    *elem = *(--stack->top_element);
63
0
    stack->top--;
64
0
    count--;
65
0
  }
66
0
  va_end(ptr);
67
0
}
68
69
70
71
ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
72
102k
{
73
102k
  if (stack->elements) {
74
24.5k
    pefree(stack->elements, stack->persistent);
75
24.5k
  }
76
102k
}
77
78
79
ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
80
102k
{
81
102k
  int i = stack->top;
82
83
635k
  while (--i >= 0) {
84
533k
    func(stack->elements[i]);
85
533k
  }
86
102k
}
87
88
ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
89
19.7k
{
90
19.7k
  int i = 0;
91
92
268k
  while (i < stack->top) {
93
248k
    func(stack->elements[i++]);
94
248k
  }
95
19.7k
}
96
97
98
ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), bool free_elements)
99
102k
{
100
102k
  zend_ptr_stack_apply(stack, func);
101
102k
  if (free_elements) {
102
102k
    int i = stack->top;
103
104
635k
    while (--i >= 0) {
105
533k
      pefree(stack->elements[i], stack->persistent);
106
533k
    }
107
102k
  }
108
102k
  stack->top = 0;
109
102k
  stack->top_element = stack->elements;
110
102k
}
111
112
113
ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
114
0
{
115
0
  return stack->top;
116
0
}