/src/php-src/main/poll/poll_fd_table.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Jakub Zelenka <bukka@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #include "php_poll_internal.h" |
16 | | |
17 | | php_poll_fd_table *php_poll_fd_table_init(int initial_capacity, bool persistent) |
18 | 0 | { |
19 | 0 | php_poll_fd_table *table = php_poll_calloc(1, sizeof(php_poll_fd_table), persistent); |
20 | 0 | if (!table) { |
21 | 0 | return NULL; |
22 | 0 | } |
23 | | |
24 | 0 | if (initial_capacity <= 0) { |
25 | 0 | initial_capacity = 64; |
26 | 0 | } |
27 | |
|
28 | 0 | _zend_hash_init(&table->entries_ht, initial_capacity, NULL, persistent); |
29 | 0 | table->persistent = persistent; |
30 | |
|
31 | 0 | return table; |
32 | 0 | } |
33 | | |
34 | | void php_poll_fd_table_cleanup(php_poll_fd_table *table) |
35 | 0 | { |
36 | 0 | if (table) { |
37 | 0 | ZEND_HASH_FOREACH_VAL(&table->entries_ht, zval *zv) |
38 | 0 | { |
39 | 0 | php_poll_fd_entry *entry = Z_PTR_P(zv); |
40 | 0 | pefree(entry, table->persistent); |
41 | 0 | } |
42 | 0 | ZEND_HASH_FOREACH_END(); |
43 | |
|
44 | 0 | zend_hash_destroy(&table->entries_ht); |
45 | 0 | pefree(table, table->persistent); |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | | php_poll_fd_entry *php_poll_fd_table_find(php_poll_fd_table *table, int fd) |
50 | 0 | { |
51 | 0 | zval *zv = zend_hash_index_find(&table->entries_ht, (zend_ulong) fd); |
52 | 0 | return zv ? Z_PTR_P(zv) : NULL; |
53 | 0 | } |
54 | | |
55 | | php_poll_fd_entry *php_poll_fd_table_get_new(php_poll_fd_table *table, int fd) |
56 | 0 | { |
57 | 0 | php_poll_fd_entry *entry = php_poll_calloc(1, sizeof(php_poll_fd_entry), table->persistent); |
58 | 0 | if (!entry) { |
59 | 0 | return NULL; |
60 | 0 | } |
61 | | |
62 | 0 | entry->fd = fd; |
63 | 0 | entry->active = true; |
64 | 0 | entry->events = 0; |
65 | 0 | entry->data = NULL; |
66 | 0 | entry->last_revents = 0; |
67 | |
|
68 | 0 | zval zv; |
69 | 0 | ZVAL_PTR(&zv, entry); |
70 | 0 | if (!zend_hash_index_add(&table->entries_ht, (zend_ulong) fd, &zv)) { |
71 | 0 | pefree(entry, table->persistent); |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 0 | return entry; |
76 | 0 | } |
77 | | |
78 | | php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd) |
79 | 0 | { |
80 | 0 | php_poll_fd_entry *entry = php_poll_fd_table_find(table, fd); |
81 | 0 | if (entry) { |
82 | 0 | return entry; |
83 | 0 | } |
84 | | |
85 | 0 | return php_poll_fd_table_get_new(table, fd); |
86 | 0 | } |
87 | | |
88 | | bool php_poll_fd_table_remove(php_poll_fd_table *table, int fd) |
89 | 0 | { |
90 | 0 | zval *zv = zend_hash_index_find(&table->entries_ht, (zend_ulong) fd); |
91 | 0 | if (zv == NULL) { |
92 | 0 | return false; |
93 | 0 | } |
94 | 0 | php_poll_fd_entry *entry = Z_PTR_P(zv); |
95 | 0 | pefree(entry, table->persistent); |
96 | 0 | return zend_hash_index_del(&table->entries_ht, (zend_ulong) fd) == SUCCESS; |
97 | 0 | } |
98 | | |
99 | | /* Helper function for backends that need to iterate over all entries */ |
100 | | typedef bool (*php_poll_fd_iterator_func_t)(int fd, php_poll_fd_entry *entry, void *user_data); |
101 | | |
102 | | /* Iterate over all active FD entries */ |
103 | | void php_poll_fd_table_foreach( |
104 | | php_poll_fd_table *table, php_poll_fd_iterator_func_t callback, void *user_data) |
105 | 0 | { |
106 | 0 | ZEND_HASH_FOREACH_NUM_KEY_VAL(&table->entries_ht, zend_ulong fd, zval *zv) |
107 | 0 | { |
108 | 0 | php_poll_fd_entry *entry = Z_PTR_P(zv); |
109 | 0 | if (entry->active && !callback((int) fd, entry, user_data)) { |
110 | 0 | break; /* Callback returned false, stop iteration */ |
111 | 0 | } |
112 | 0 | } |
113 | 0 | ZEND_HASH_FOREACH_END(); |
114 | 0 | } |