xref: /PHP-7.3/Zend/zend_ts_hash.h (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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 retreival */
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 /* Misc */
72 ZEND_API int zend_ts_hash_exists(TsHashTable *ht, zend_string *key);
73 ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, zend_ulong h);
74 
75 /* Copying, merging and sorting */
76 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
77 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
78 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite);
79 ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
80 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber);
81 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
82 ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag);
83 
84 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);
85 
86 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht);
87 
88 #if ZEND_DEBUG
89 /* debug */
90 void zend_ts_hash_display_pListTail(TsHashTable *ht);
91 void zend_ts_hash_display(TsHashTable *ht);
92 #endif
93 
94 ZEND_API zval *zend_ts_hash_str_find(TsHashTable *ht, const char *key, size_t len);
95 ZEND_API zval *zend_ts_hash_str_update(TsHashTable *ht, const char *key, size_t len, zval *pData);
96 ZEND_API zval *zend_ts_hash_str_add(TsHashTable *ht, const char *key, size_t len, zval *pData);
97 
zend_ts_hash_str_find_ptr(TsHashTable * ht,const char * str,size_t len)98 static zend_always_inline void *zend_ts_hash_str_find_ptr(TsHashTable *ht, const char *str, size_t len)
99 {
100 	zval *zv;
101 
102 	zv = zend_ts_hash_str_find(ht, str, len);
103 	return zv ? Z_PTR_P(zv) : NULL;
104 }
105 
zend_ts_hash_str_update_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)106 static zend_always_inline void *zend_ts_hash_str_update_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
107 {
108 	zval tmp, *zv;
109 
110 	ZVAL_PTR(&tmp, pData);
111 	zv = zend_ts_hash_str_update(ht, str, len, &tmp);
112 	return zv ? Z_PTR_P(zv) : NULL;
113 }
114 
zend_ts_hash_str_add_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)115 static zend_always_inline void *zend_ts_hash_str_add_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
116 {
117 	zval tmp, *zv;
118 
119 	ZVAL_PTR(&tmp, pData);
120 	zv = zend_ts_hash_str_add(ht, str, len, &tmp);
121 	return zv ? Z_PTR_P(zv) : NULL;
122 }
123 
124 END_EXTERN_C()
125 
126 #define ZEND_TS_INIT_SYMTABLE(ht)								\
127 	ZEND_TS_INIT_SYMTABLE_EX(ht, 2, 0)
128 
129 #define ZEND_TS_INIT_SYMTABLE_EX(ht, n, persistent)			\
130 	zend_ts_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
131 
132 #endif							/* ZEND_HASH_H */
133 
134 /*
135  * Local variables:
136  * tab-width: 4
137  * c-basic-offset: 4
138  * indent-tabs-mode: t
139  * End:
140  * vim600: sw=4 ts=4 fdm=marker
141  * vim<600: sw=4 ts=4
142  */
143