xref: /PHP-7.1/Zend/zend_ts_hash.h (revision ccd4716e)
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 /* $Id$ */
20 
21 #ifndef ZEND_TS_HASH_H
22 #define ZEND_TS_HASH_H
23 
24 #include "zend.h"
25 
26 typedef struct _zend_ts_hashtable {
27 	HashTable hash;
28 	uint32_t reader;
29 #ifdef ZTS
30 	MUTEX_T mx_reader;
31 	MUTEX_T mx_writer;
32 #endif
33 } TsHashTable;
34 
35 BEGIN_EXTERN_C()
36 
37 #define TS_HASH(table) (&(table->hash))
38 
39 /* startup/shutdown */
40 ZEND_API void _zend_ts_hash_init(TsHashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
41 ZEND_API void _zend_ts_hash_init_ex(TsHashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC);
42 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht);
43 ZEND_API void zend_ts_hash_clean(TsHashTable *ht);
44 
45 #define zend_ts_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)	\
46 	_zend_ts_hash_init(ht, nSize, pDestructor, persistent ZEND_FILE_LINE_CC)
47 #define zend_ts_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)	\
48 	_zend_ts_hash_init_ex(ht, nSize, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_CC)
49 
50 
51 /* additions/updates/changes */
52 ZEND_API zval *_zend_ts_hash_add_or_update(TsHashTable *ht, zend_string *key, zval *pData, int flag ZEND_FILE_LINE_DC);
53 #define zend_ts_hash_update(ht, key, pData) \
54 		_zend_ts_hash_add_or_update(ht, key, pData, HASH_UPDATE ZEND_FILE_LINE_CC)
55 #define zend_ts_hash_add(ht, key, pData) \
56 		_zend_ts_hash_add_or_update(ht, key, pData, HASH_ADD ZEND_FILE_LINE_CC)
57 
58 ZEND_API zval *_zend_ts_hash_index_add_or_update(TsHashTable *ht, zend_ulong h, zval *pData, int flag ZEND_FILE_LINE_DC);
59 #define zend_ts_hash_index_update(ht, h, pData) \
60 		_zend_ts_hash_index_add_or_update(ht, h, pData, HASH_UPDATE ZEND_FILE_LINE_CC)
61 #define zend_ts_hash_next_index_insert(ht, pData) \
62 		_zend_ts_hash_index_add_or_update(ht, ht->nNextFreeElement, pData, HASH_ADD ZEND_FILE_LINE_CC)
63 
64 ZEND_API zval* zend_ts_hash_add_empty_element(TsHashTable *ht, zend_string *key);
65 
66 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht);
67 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func);
68 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *);
69 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht, apply_func_args_t apply_func, int, ...);
70 
71 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func);
72 
73 
74 /* Deletes */
75 ZEND_API int zend_ts_hash_del(TsHashTable *ht, zend_string *key);
76 ZEND_API int zend_ts_hash_index_del(TsHashTable *ht, zend_ulong h);
77 
78 /* Data retreival */
79 ZEND_API zval *zend_ts_hash_find(TsHashTable *ht, zend_string *key);
80 ZEND_API zval *zend_ts_hash_index_find(TsHashTable *ht, zend_ulong);
81 
82 /* Misc */
83 ZEND_API int zend_ts_hash_exists(TsHashTable *ht, zend_string *key);
84 ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, zend_ulong h);
85 
86 /* Copying, merging and sorting */
87 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
88 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
89 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite);
90 ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
91 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber);
92 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
93 ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag);
94 
95 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);
96 
97 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht);
98 
99 #if ZEND_DEBUG
100 /* debug */
101 void zend_ts_hash_display_pListTail(TsHashTable *ht);
102 void zend_ts_hash_display(TsHashTable *ht);
103 #endif
104 
105 ZEND_API zval *zend_ts_hash_str_find(TsHashTable *ht, const char *key, size_t len);
106 ZEND_API zval *_zend_ts_hash_str_update(TsHashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
107 ZEND_API zval *_zend_ts_hash_str_add(TsHashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
108 
109 #define zend_ts_hash_str_update(ht, key, len, pData) \
110 		_zend_ts_hash_str_update(ht, key, len, pData ZEND_FILE_LINE_CC)
111 #define zend_ts_hash_str_add(ht, key, len, pData) \
112 		_zend_ts_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
113 
zend_ts_hash_str_find_ptr(TsHashTable * ht,const char * str,size_t len)114 static zend_always_inline void *zend_ts_hash_str_find_ptr(TsHashTable *ht, const char *str, size_t len)
115 {
116 	zval *zv;
117 
118 	zv = zend_ts_hash_str_find(ht, str, len);
119 	return zv ? Z_PTR_P(zv) : NULL;
120 }
121 
zend_ts_hash_str_update_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)122 static zend_always_inline void *zend_ts_hash_str_update_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
123 {
124 	zval tmp, *zv;
125 
126 	ZVAL_PTR(&tmp, pData);
127 	zv = zend_ts_hash_str_update(ht, str, len, &tmp);
128 	return zv ? Z_PTR_P(zv) : NULL;
129 }
130 
zend_ts_hash_str_add_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)131 static zend_always_inline void *zend_ts_hash_str_add_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
132 {
133 	zval tmp, *zv;
134 
135 	ZVAL_PTR(&tmp, pData);
136 	zv = zend_ts_hash_str_add(ht, str, len, &tmp);
137 	return zv ? Z_PTR_P(zv) : NULL;
138 }
139 
140 END_EXTERN_C()
141 
142 #define ZEND_TS_INIT_SYMTABLE(ht)								\
143 	ZEND_TS_INIT_SYMTABLE_EX(ht, 2, 0)
144 
145 #define ZEND_TS_INIT_SYMTABLE_EX(ht, n, persistent)			\
146 	zend_ts_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
147 
148 #endif							/* ZEND_HASH_H */
149 
150 /*
151  * Local variables:
152  * tab-width: 4
153  * c-basic-offset: 4
154  * indent-tabs-mode: t
155  * End:
156  */
157