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    | http://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 int 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 	return SUCCESS;
49 }
50 
zend_worklist_stack_push(zend_worklist_stack * stack,int i)51 static inline void zend_worklist_stack_push(zend_worklist_stack *stack, int i)
52 {
53 	ZEND_ASSERT(stack->len < stack->capacity);
54 	stack->buf[stack->len++] = i;
55 }
56 
zend_worklist_stack_peek(zend_worklist_stack * stack)57 static inline int zend_worklist_stack_peek(zend_worklist_stack *stack)
58 {
59 	ZEND_ASSERT(stack->len);
60 	return stack->buf[stack->len - 1];
61 }
62 
zend_worklist_stack_pop(zend_worklist_stack * stack)63 static inline int zend_worklist_stack_pop(zend_worklist_stack *stack)
64 {
65 	ZEND_ASSERT(stack->len);
66 	return stack->buf[--stack->len];
67 }
68 
69 typedef struct _zend_worklist {
70 	zend_bitset visited;
71 	zend_worklist_stack stack;
72 } zend_worklist;
73 
74 #define ZEND_WORKLIST_ALLOCA(w, _len, use_heap) do { \
75 		(w)->stack.buf = (int*)do_alloca(ZEND_MM_ALIGNED_SIZE(sizeof(int) * _len) + sizeof(zend_ulong) * zend_bitset_len(_len), use_heap); \
76 		(w)->stack.len = 0; \
77 		(w)->stack.capacity = _len; \
78 		(w)->visited = (zend_bitset)((char*)(w)->stack.buf + ZEND_MM_ALIGNED_SIZE(sizeof(int) * _len)); \
79 		memset((w)->visited, 0, sizeof(zend_ulong) * zend_bitset_len(_len)); \
80 	} while (0)
81 
82 #define ZEND_WORKLIST_FREE_ALLOCA(w, use_heap) \
83 	free_alloca((w)->stack.buf, use_heap)
84 
zend_worklist_prepare(zend_arena ** arena,zend_worklist * worklist,int len)85 static inline int zend_worklist_prepare(zend_arena **arena, zend_worklist *worklist, int len)
86 {
87 	ZEND_ASSERT(len >= 0);
88 	worklist->visited = (zend_bitset)zend_arena_calloc(arena, sizeof(zend_ulong), zend_bitset_len(len));
89 	return zend_worklist_stack_prepare(arena, &worklist->stack, len);
90 }
91 
zend_worklist_len(zend_worklist * worklist)92 static inline int zend_worklist_len(zend_worklist *worklist)
93 {
94 	return worklist->stack.len;
95 }
96 
zend_worklist_push(zend_worklist * worklist,int i)97 static inline int zend_worklist_push(zend_worklist *worklist, int i)
98 {
99 	ZEND_ASSERT(i >= 0 && i < worklist->stack.capacity);
100 
101 	if (zend_bitset_in(worklist->visited, i)) {
102 		return 0;
103 	}
104 
105 	zend_bitset_incl(worklist->visited, i);
106 	zend_worklist_stack_push(&worklist->stack, i);
107 	return 1;
108 }
109 
zend_worklist_peek(zend_worklist * worklist)110 static inline int zend_worklist_peek(zend_worklist *worklist)
111 {
112 	return zend_worklist_stack_peek(&worklist->stack);
113 }
114 
zend_worklist_pop(zend_worklist * worklist)115 static inline int zend_worklist_pop(zend_worklist *worklist)
116 {
117 	/* Does not clear visited flag */
118 	return zend_worklist_stack_pop(&worklist->stack);
119 }
120 
121 #endif /* _ZEND_WORKLIST_H_ */
122