xref: /PHP-8.0/Zend/zend_ts_hash.h (revision ace876cd)
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 
43 /* additions/updates/changes */
44 ZEND_API zval *zend_ts_hash_update(TsHashTable *ht, zend_string *key, zval *pData);
45 ZEND_API zval *zend_ts_hash_add(TsHashTable *ht, zend_string *key, zval *pData);
46 ZEND_API zval *zend_ts_hash_index_update(TsHashTable *ht, zend_ulong h, zval *pData);
47 ZEND_API zval *zend_ts_hash_next_index_insert(TsHashTable *ht, zval *pData);
48 ZEND_API zval* zend_ts_hash_add_empty_element(TsHashTable *ht, zend_string *key);
49 
50 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht);
51 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func);
52 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *);
53 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht, apply_func_args_t apply_func, int, ...);
54 
55 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func);
56 
57 
58 /* Deletes */
59 ZEND_API zend_result zend_ts_hash_del(TsHashTable *ht, zend_string *key);
60 ZEND_API zend_result zend_ts_hash_index_del(TsHashTable *ht, zend_ulong h);
61 
62 /* Data retrieval */
63 ZEND_API zval *zend_ts_hash_find(TsHashTable *ht, zend_string *key);
64 ZEND_API zval *zend_ts_hash_index_find(TsHashTable *ht, zend_ulong);
65 
66 /* Copying, merging and sorting */
67 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
68 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
69 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite);
70 ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
71 ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, bool renumber);
72 ZEND_API int  zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
73 ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, bucket_compare_func_t compar, int flag);
74 
75 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);
76 
77 ZEND_API void zend_ts_hash_rehash(TsHashTable *ht);
78 
79 #if ZEND_DEBUG
80 /* debug */
81 void zend_ts_hash_display_pListTail(TsHashTable *ht);
82 void zend_ts_hash_display(TsHashTable *ht);
83 #endif
84 
85 ZEND_API zval *zend_ts_hash_str_find(TsHashTable *ht, const char *key, size_t len);
86 ZEND_API zval *zend_ts_hash_str_update(TsHashTable *ht, const char *key, size_t len, zval *pData);
87 ZEND_API zval *zend_ts_hash_str_add(TsHashTable *ht, const char *key, size_t len, zval *pData);
88 
zend_ts_hash_str_find_ptr(TsHashTable * ht,const char * str,size_t len)89 static zend_always_inline void *zend_ts_hash_str_find_ptr(TsHashTable *ht, const char *str, size_t len)
90 {
91 	zval *zv;
92 
93 	zv = zend_ts_hash_str_find(ht, str, len);
94 	return zv ? Z_PTR_P(zv) : NULL;
95 }
96 
zend_ts_hash_str_update_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)97 static zend_always_inline void *zend_ts_hash_str_update_ptr(TsHashTable *ht, const char *str, size_t len, void *pData)
98 {
99 	zval tmp, *zv;
100 
101 	ZVAL_PTR(&tmp, pData);
102 	zv = zend_ts_hash_str_update(ht, str, len, &tmp);
103 	return zv ? Z_PTR_P(zv) : NULL;
104 }
105 
zend_ts_hash_str_add_ptr(TsHashTable * ht,const char * str,size_t len,void * pData)106 static zend_always_inline void *zend_ts_hash_str_add_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_add(ht, str, len, &tmp);
112 	return zv ? Z_PTR_P(zv) : NULL;
113 }
114 
zend_ts_hash_exists(TsHashTable * ht,zend_string * key)115 static zend_always_inline bool zend_ts_hash_exists(TsHashTable *ht, zend_string *key)
116 {
117 	return zend_ts_hash_find(ht, key) != NULL;
118 }
119 
zend_ts_hash_index_exists(TsHashTable * ht,zend_ulong h)120 static zend_always_inline bool zend_ts_hash_index_exists(TsHashTable *ht, zend_ulong h)
121 {
122 	return zend_ts_hash_index_find(ht, h) != NULL;
123 }
124 
125 END_EXTERN_C()
126 
127 #define ZEND_TS_INIT_SYMTABLE(ht)								\
128 	ZEND_TS_INIT_SYMTABLE_EX(ht, 2, 0)
129 
130 #define ZEND_TS_INIT_SYMTABLE_EX(ht, n, persistent)			\
131 	zend_ts_hash_init(ht, n, ZVAL_PTR_DTOR, persistent)
132 
133 #endif							/* ZEND_HASH_H */
134