Coverage Report

Created: 2026-06-02 06:36

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
6.77k
{
25
6.77k
  stack->top_element = stack->elements = NULL;
26
6.77k
  stack->top = stack->max = 0;
27
6.77k
  stack->persistent = persistent;
28
6.77k
}
29
30
ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
31
6.77k
{
32
6.77k
  zend_ptr_stack_init_ex(stack, false);
33
6.77k
}
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
6.77k
{
73
6.77k
  if (stack->elements) {
74
0
    pefree(stack->elements, stack->persistent);
75
0
  }
76
6.77k
}
77
78
79
ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
80
6.77k
{
81
6.77k
  int i = stack->top;
82
83
6.77k
  while (--i >= 0) {
84
0
    func(stack->elements[i]);
85
0
  }
86
6.77k
}
87
88
ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
89
0
{
90
0
  int i = 0;
91
92
0
  while (i < stack->top) {
93
0
    func(stack->elements[i++]);
94
0
  }
95
0
}
96
97
98
ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), bool free_elements)
99
6.77k
{
100
6.77k
  zend_ptr_stack_apply(stack, func);
101
6.77k
  if (free_elements) {
102
6.77k
    int i = stack->top;
103
104
6.77k
    while (--i >= 0) {
105
0
      pefree(stack->elements[i], stack->persistent);
106
0
    }
107
6.77k
  }
108
6.77k
  stack->top = 0;
109
6.77k
  stack->top_element = stack->elements;
110
6.77k
}
111
112
113
ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
114
0
{
115
0
  return stack->top;
116
0
}