xref: /PHP-7.4/Zend/zend_ts_hash.h (revision da3316ff)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Harald Radi <harald.radi@nme.at>                            |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef ZEND_TS_HASH_H
20 #define ZEND_TS_HASH_H
21 
22 #include "zend.h"
23 
24 typedef struct _zend_ts_hashtable {
25 	HashTable hash;
26 	uint32_t reader;
27 #ifdef ZTS
28 	MUTEX_T mx_reader;
29 	MUTEX_T mx_writer;
30 #endif
31 } TsHashTable;
32 
33 BEGIN_EXTERN_C()
34 
35 #define TS_HASH(table) (&(table->hash))
36 
37 /* startup/shutdown */
38 ZEND_API void _zend_ts_hash_init(TsHashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent);
39 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht);
40 ZEND_API void zend_ts_hash_clean(TsHashTable *ht);
41 
42 #define zend_ts_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)	\
43 	_zend_ts_hash_init(ht, nSize, pDestructor, persistent)
44 #define zend_ts_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)	\
45 	_zend_ts_hash_init(ht, nSize, pDestructor, persistent)
46 
47 
48 /* additions/updates/changes */
49 ZEND_API zval *zend_ts_hash_update(TsHashTable *ht, zend_string *key, zval *pData);
50 ZEND_API zval *zend_ts_hash_add(TsHashTable *ht, zend_string *key, zval *pData);
51 ZEND_API zval *zend_ts_hash_index_update(TsHashTable *ht, zend_ulong h, zval *pData);
52 ZEND_API zval *zend_ts_hash_next_index_insert(TsHashTable *ht, zval *pData);
53 ZEND_API zval* zend_ts_hash_add_empty_element(TsHashTable *ht, zend_string *key);
54 
55 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht);
56 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func);
57 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *);
58 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht, apply_func_args_t apply_func, int, ...);
59 
60 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func);
61 
62 
63 /* Deletes */
64 ZEND_API int zend_ts_hash_del(TsHashTable *ht, zend_string *key);
65 ZEND_API int zend_ts_hash_index_del(TsHashTable *ht, zend_ulong h);
66 
67 /* Data retrieval */
68 ZEND_API zval *zend_ts_hash_find(TsHashTable *ht, zend_string *key);
69 ZEND_API zval *zend_ts_hash_index_find(TsHashTable *ht, zend_ulong);
70 
71 /* Copying, merging and sorting */
72 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
73 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
74 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite);
75 ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
76 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber);
77 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
78 ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag);
79 
80 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);
81 
82 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht);
83 
84 #if ZEND_DEBUG
85 /* debug */
86 void zend_ts_hash_display_pListTail(TsHashTable *ht);
87 void zend_ts_hash_display(TsHashTable *ht);
88 #endif
89 
90 ZEND_API zval *zend_ts_hash_str_find(TsHashTable *ht, const char *key, size_t len);
91 ZEND_API zval *zend_ts_hash_str_update(TsHashTable *ht, const char *key, size_t len, zval *pData);
92 ZEND_API zval *zend_ts_hash_str_add(TsHashTable *ht, const char *key, size_t len, zval *pData);
93 
zend_ts_hash_str_find_ptr(TsHashTable * ht,const char * str,size_t len)94 static zend_always_inline void *zend_ts_hash_str_find_ptr(TsHashTable *ht, const char *str, size_t len)
95 {
96 	zval *zv;
97 
98 	zv = zend_ts_hash_str_find(ht, str, len);
99 	return zv ? Z_PTR_P(zv) : NULL;
100 }
101 
zend_ts_hash_str_update_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)102 static zend_always_inline void *zend_ts_hash_str_update_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
103 {
104 	zval tmp, *zv;
105 
106 	ZVAL_PTR(&tmp, pData);
107 	zv = zend_ts_hash_str_update(ht, str, len, &tmp);
108 	return zv ? Z_PTR_P(zv) : NULL;
109 }
110 
zend_ts_hash_str_add_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)111 static zend_always_inline void *zend_ts_hash_str_add_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
112 {
113 	zval tmp, *zv;
114 
115 	ZVAL_PTR(&tmp, pData);
116 	zv = zend_ts_hash_str_add(ht, str, len, &tmp);
117 	return zv ? Z_PTR_P(zv) : NULL;
118 }
119 
zend_ts_hash_exists(TsHashTable * ht,zend_string * key)120 static zend_always_inline int zend_ts_hash_exists(TsHashTable *ht, zend_string *key)
121 {
122 	return zend_ts_hash_find(ht, key) != NULL;
123 }
124 
zend_ts_hash_index_exists(TsHashTable * ht,zend_ulong h)125 static zend_always_inline int zend_ts_hash_index_exists(TsHashTable *ht, zend_ulong h)
126 {
127 	return zend_ts_hash_index_find(ht, h) != NULL;
128 }
129 
130 END_EXTERN_C()
131 
132 #define ZEND_TS_INIT_SYMTABLE(ht)								\
133 	ZEND_TS_INIT_SYMTABLE_EX(ht, 2, 0)
134 
135 #define ZEND_TS_INIT_SYMTABLE_EX(ht, n, persistent)			\
136 	zend_ts_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
137 
138 #endif							/* ZEND_HASH_H */
139