1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 | https://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: Andy Wingo <wingo@igalia.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifndef _ZEND_WORKLIST_H_
20 #define _ZEND_WORKLIST_H_
21
22 #include "zend_arena.h"
23 #include "zend_bitset.h"
24
25 typedef struct _zend_worklist_stack {
26 int *buf;
27 int len;
28 int capacity;
29 } zend_worklist_stack;
30
31 #define ZEND_WORKLIST_STACK_ALLOCA(s, _len, use_heap) do { \
32 (s)->buf = (int*)do_alloca(sizeof(int) * _len, use_heap); \
33 (s)->len = 0; \
34 (s)->capacity = _len; \
35 } while (0)
36
37 #define ZEND_WORKLIST_STACK_FREE_ALLOCA(s, use_heap) \
38 free_alloca((s)->buf, use_heap)
39
zend_worklist_stack_prepare(zend_arena ** arena,zend_worklist_stack * stack,int len)40 static inline void zend_worklist_stack_prepare(zend_arena **arena, zend_worklist_stack *stack, int len)
41 {
42 ZEND_ASSERT(len >= 0);
43
44 stack->buf = (int*)zend_arena_calloc(arena, sizeof(*stack->buf), len);
45 stack->len = 0;
46 stack->capacity = len;
47 }
48
zend_worklist_stack_push(zend_worklist_stack * stack,int i)49 static inline void zend_worklist_stack_push(zend_worklist_stack *stack, int i)
50 {
51 ZEND_ASSERT(stack->len < stack->capacity);
52 stack->buf[stack->len++] = i;
53 }
54
zend_worklist_stack_peek(const zend_worklist_stack * stack)55 static inline int zend_worklist_stack_peek(const zend_worklist_stack *stack)
56 {
57 ZEND_ASSERT(stack->len);
58 return stack->buf[stack->len - 1];
59 }
60
zend_worklist_stack_pop(zend_worklist_stack * stack)61 static inline int zend_worklist_stack_pop(zend_worklist_stack *stack)
62 {
63 ZEND_ASSERT(stack->len);
64 return stack->buf[--stack->len];
65 }
66
67 typedef struct _zend_worklist {
68 zend_bitset visited;
69 zend_worklist_stack stack;
70 } zend_worklist;
71
72 #define ZEND_WORKLIST_ALLOCA(w, _len, use_heap) do { \
73 (w)->stack.buf = (int*)do_alloca(ZEND_MM_ALIGNED_SIZE(sizeof(int) * _len) + sizeof(zend_ulong) * zend_bitset_len(_len), use_heap); \
74 (w)->stack.len = 0; \
75 (w)->stack.capacity = _len; \
76 (w)->visited = (zend_bitset)((char*)(w)->stack.buf + ZEND_MM_ALIGNED_SIZE(sizeof(int) * _len)); \
77 memset((w)->visited, 0, sizeof(zend_ulong) * zend_bitset_len(_len)); \
78 } while (0)
79
80 #define ZEND_WORKLIST_FREE_ALLOCA(w, use_heap) \
81 free_alloca((w)->stack.buf, use_heap)
82
zend_worklist_prepare(zend_arena ** arena,zend_worklist * worklist,int len)83 static inline void zend_worklist_prepare(zend_arena **arena, zend_worklist *worklist, int len)
84 {
85 ZEND_ASSERT(len >= 0);
86 worklist->visited = (zend_bitset)zend_arena_calloc(arena, sizeof(zend_ulong), zend_bitset_len(len));
87 zend_worklist_stack_prepare(arena, &worklist->stack, len);
88 }
89
zend_worklist_len(const zend_worklist * worklist)90 static inline int zend_worklist_len(const zend_worklist *worklist)
91 {
92 return worklist->stack.len;
93 }
94
zend_worklist_push(zend_worklist * worklist,int i)95 static inline bool zend_worklist_push(zend_worklist *worklist, int i)
96 {
97 ZEND_ASSERT(i >= 0 && i < worklist->stack.capacity);
98
99 if (zend_bitset_in(worklist->visited, i)) {
100 return 0;
101 }
102
103 zend_bitset_incl(worklist->visited, i);
104 zend_worklist_stack_push(&worklist->stack, i);
105 return 1;
106 }
107
zend_worklist_peek(const zend_worklist * worklist)108 static inline int zend_worklist_peek(const zend_worklist *worklist)
109 {
110 return zend_worklist_stack_peek(&worklist->stack);
111 }
112
zend_worklist_pop(zend_worklist * worklist)113 static inline int zend_worklist_pop(zend_worklist *worklist)
114 {
115 /* Does not clear visited flag */
116 return zend_worklist_stack_pop(&worklist->stack);
117 }
118
119 #endif /* _ZEND_WORKLIST_H_ */
120