zend_hash.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifndef ZEND_HASH_H
  21. #define ZEND_HASH_H
  22. #include "zend.h"
  23. #define HASH_KEY_IS_STRING 1
  24. #define HASH_KEY_IS_LONG 2
  25. #define HASH_KEY_NON_EXISTENT 3
  26. #define HASH_UPDATE (1<<0)
  27. #define HASH_ADD (1<<1)
  28. #define HASH_UPDATE_INDIRECT (1<<2)
  29. #define HASH_ADD_NEW (1<<3)
  30. #define HASH_ADD_NEXT (1<<4)
  31. #define HASH_LOOKUP (1<<5)
  32. #define HASH_FLAG_CONSISTENCY ((1<<0) | (1<<1))
  33. #define HASH_FLAG_PACKED (1<<2)
  34. #define HASH_FLAG_UNINITIALIZED (1<<3)
  35. #define HASH_FLAG_STATIC_KEYS (1<<4) /* long and interned strings */
  36. #define HASH_FLAG_HAS_EMPTY_IND (1<<5)
  37. #define HASH_FLAG_ALLOW_COW_VIOLATION (1<<6)
  38. /* Only the low byte are real flags */
  39. #define HASH_FLAG_MASK 0xff
  40. #define HT_FLAGS(ht) (ht)->u.flags
  41. #define HT_INVALIDATE(ht) do { \
  42. HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED; \
  43. } while (0)
  44. #define HT_IS_INITIALIZED(ht) \
  45. ((HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) == 0)
  46. #define HT_IS_PACKED(ht) \
  47. ((HT_FLAGS(ht) & HASH_FLAG_PACKED) != 0)
  48. #define HT_IS_WITHOUT_HOLES(ht) \
  49. ((ht)->nNumUsed == (ht)->nNumOfElements)
  50. #define HT_HAS_STATIC_KEYS_ONLY(ht) \
  51. ((HT_FLAGS(ht) & (HASH_FLAG_PACKED|HASH_FLAG_STATIC_KEYS)) != 0)
  52. #if ZEND_DEBUG
  53. # define HT_ALLOW_COW_VIOLATION(ht) HT_FLAGS(ht) |= HASH_FLAG_ALLOW_COW_VIOLATION
  54. #else
  55. # define HT_ALLOW_COW_VIOLATION(ht)
  56. #endif
  57. #define HT_ITERATORS_COUNT(ht) (ht)->u.v.nIteratorsCount
  58. #define HT_ITERATORS_OVERFLOW(ht) (HT_ITERATORS_COUNT(ht) == 0xff)
  59. #define HT_HAS_ITERATORS(ht) (HT_ITERATORS_COUNT(ht) != 0)
  60. #define HT_SET_ITERATORS_COUNT(ht, iters) \
  61. do { HT_ITERATORS_COUNT(ht) = (iters); } while (0)
  62. #define HT_INC_ITERATORS_COUNT(ht) \
  63. HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) + 1)
  64. #define HT_DEC_ITERATORS_COUNT(ht) \
  65. HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) - 1)
  66. extern ZEND_API const HashTable zend_empty_array;
  67. #define ZVAL_EMPTY_ARRAY(z) do { \
  68. zval *__z = (z); \
  69. Z_ARR_P(__z) = (zend_array*)&zend_empty_array; \
  70. Z_TYPE_INFO_P(__z) = IS_ARRAY; \
  71. } while (0)
  72. typedef struct _zend_hash_key {
  73. zend_ulong h;
  74. zend_string *key;
  75. } zend_hash_key;
  76. typedef bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
  77. BEGIN_EXTERN_C()
  78. /* startup/shutdown */
  79. ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent);
  80. ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht);
  81. ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht);
  82. #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) \
  83. _zend_hash_init((ht), (nSize), (pDestructor), (persistent))
  84. ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, bool packed);
  85. ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht);
  86. ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht);
  87. ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht);
  88. ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht);
  89. ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, bool packed);
  90. ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed);
  91. ZEND_API void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht);
  92. /* additions/updates/changes */
  93. ZEND_API zval* ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag);
  94. ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key,zval *pData);
  95. ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData);
  96. ZEND_API zval* ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key,zval *pData);
  97. ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData);
  98. ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag);
  99. ZEND_API zval* ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
  100. ZEND_API zval* ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData);
  101. ZEND_API zval* ZEND_FASTCALL zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData);
  102. ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData);
  103. ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag);
  104. ZEND_API zval* ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData);
  105. ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData);
  106. ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData);
  107. ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData);
  108. ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData);
  109. ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
  110. ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key);
  111. ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
  112. ZEND_API zval* ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *p, zend_string *key);
  113. #define ZEND_HASH_APPLY_KEEP 0
  114. #define ZEND_HASH_APPLY_REMOVE 1<<0
  115. #define ZEND_HASH_APPLY_STOP 1<<1
  116. typedef int (*apply_func_t)(zval *pDest);
  117. typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
  118. typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
  119. ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht);
  120. ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht);
  121. ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func);
  122. ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
  123. ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
  124. /* This function should be used with special care (in other words,
  125. * it should usually not be used). When used with the ZEND_HASH_APPLY_STOP
  126. * return value, it assumes things about the order of the elements in the hash.
  127. * Also, it does not provide the same kind of reentrancy protection that
  128. * the standard apply functions do.
  129. */
  130. ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
  131. /* Deletes */
  132. ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key);
  133. ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key);
  134. ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *key, size_t len);
  135. ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
  136. ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h);
  137. ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p);
  138. /* Data retrieval */
  139. ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key);
  140. ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
  141. ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h);
  142. ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h);
  143. /* The same as zend_hash_find(), but hash value of the key must be already calculated. */
  144. ZEND_API zval* ZEND_FASTCALL zend_hash_find_known_hash(const HashTable *ht, zend_string *key);
  145. static zend_always_inline zval *zend_hash_find_ex(const HashTable *ht, zend_string *key, bool known_hash)
  146. {
  147. if (known_hash) {
  148. return zend_hash_find_known_hash(ht, key);
  149. } else {
  150. return zend_hash_find(ht, key);
  151. }
  152. }
  153. #define ZEND_HASH_INDEX_FIND(_ht, _h, _ret, _not_found) do { \
  154. if (EXPECTED(HT_FLAGS(_ht) & HASH_FLAG_PACKED)) { \
  155. if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
  156. _ret = &_ht->arData[_h].val; \
  157. if (UNEXPECTED(Z_TYPE_P(_ret) == IS_UNDEF)) { \
  158. goto _not_found; \
  159. } \
  160. } else { \
  161. goto _not_found; \
  162. } \
  163. } else { \
  164. _ret = _zend_hash_index_find(_ht, _h); \
  165. if (UNEXPECTED(_ret == NULL)) { \
  166. goto _not_found; \
  167. } \
  168. } \
  169. } while (0)
  170. /* Find or add NULL, if doesn't exist */
  171. ZEND_API zval* ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key);
  172. ZEND_API zval* ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h);
  173. #define ZEND_HASH_INDEX_LOOKUP(_ht, _h, _ret) do { \
  174. if (EXPECTED(HT_FLAGS(_ht) & HASH_FLAG_PACKED)) { \
  175. if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
  176. _ret = &_ht->arData[_h].val; \
  177. if (EXPECTED(Z_TYPE_P(_ret) != IS_UNDEF)) { \
  178. break; \
  179. } \
  180. } \
  181. } \
  182. _ret = zend_hash_index_lookup(_ht, _h); \
  183. } while (0)
  184. /* Misc */
  185. static zend_always_inline bool zend_hash_exists(const HashTable *ht, zend_string *key)
  186. {
  187. return zend_hash_find(ht, key) != NULL;
  188. }
  189. static zend_always_inline bool zend_hash_str_exists(const HashTable *ht, const char *str, size_t len)
  190. {
  191. return zend_hash_str_find(ht, str, len) != NULL;
  192. }
  193. static zend_always_inline bool zend_hash_index_exists(const HashTable *ht, zend_ulong h)
  194. {
  195. return zend_hash_index_find(ht, h) != NULL;
  196. }
  197. /* traversing */
  198. ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht);
  199. #define zend_hash_has_more_elements_ex(ht, pos) \
  200. (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
  201. ZEND_API zend_result ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
  202. ZEND_API zend_result ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
  203. ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos);
  204. ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
  205. ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
  206. ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
  207. ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
  208. ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
  209. #define zend_hash_has_more_elements(ht) \
  210. zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
  211. #define zend_hash_move_forward(ht) \
  212. zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
  213. #define zend_hash_move_backwards(ht) \
  214. zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
  215. #define zend_hash_get_current_key(ht, str_index, num_index) \
  216. zend_hash_get_current_key_ex(ht, str_index, num_index, &(ht)->nInternalPointer)
  217. #define zend_hash_get_current_key_zval(ht, key) \
  218. zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
  219. #define zend_hash_get_current_key_type(ht) \
  220. zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
  221. #define zend_hash_get_current_data(ht) \
  222. zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
  223. #define zend_hash_internal_pointer_reset(ht) \
  224. zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
  225. #define zend_hash_internal_pointer_end(ht) \
  226. zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
  227. /* Copying, merging and sorting */
  228. ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
  229. ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite);
  230. ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
  231. ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q);
  232. ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
  233. ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
  234. typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
  235. ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered);
  236. ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, bool renumber);
  237. ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag);
  238. #define zend_hash_sort(ht, compare_func, renumber) \
  239. zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
  240. #define zend_hash_num_elements(ht) \
  241. (ht)->nNumOfElements
  242. #define zend_hash_next_free_element(ht) \
  243. (ht)->nNextFreeElement
  244. ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
  245. #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
  246. # define zend_new_array(size) \
  247. (__builtin_constant_p(size) ? \
  248. ((((uint32_t)(size)) <= HT_MIN_SIZE) ? \
  249. _zend_new_array_0() \
  250. : \
  251. _zend_new_array((size)) \
  252. ) \
  253. : \
  254. _zend_new_array((size)) \
  255. )
  256. #else
  257. # define zend_new_array(size) \
  258. _zend_new_array(size)
  259. #endif
  260. ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
  261. ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
  262. ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2);
  263. ZEND_API uint32_t zend_array_count(HashTable *ht);
  264. ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
  265. ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
  266. ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
  267. ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht);
  268. ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate);
  269. ZEND_API bool ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
  270. ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos);
  271. ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht);
  272. ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array);
  273. ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx);
  274. ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start);
  275. ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to);
  276. ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(HashTable *ht, HashPosition step);
  277. static zend_always_inline void zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
  278. {
  279. if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
  280. _zend_hash_iterators_update(ht, from, to);
  281. }
  282. }
  283. /* For regular arrays (non-persistent, storing zvals). */
  284. static zend_always_inline void zend_array_release(zend_array *array)
  285. {
  286. if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
  287. if (GC_DELREF(array) == 0) {
  288. zend_array_destroy(array);
  289. }
  290. }
  291. }
  292. /* For general hashes (possibly persistent, storing any kind of value). */
  293. static zend_always_inline void zend_hash_release(zend_array *array)
  294. {
  295. if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
  296. if (GC_DELREF(array) == 0) {
  297. zend_hash_destroy(array);
  298. pefree(array, GC_FLAGS(array) & IS_ARRAY_PERSISTENT);
  299. }
  300. }
  301. }
  302. END_EXTERN_C()
  303. #define ZEND_INIT_SYMTABLE(ht) \
  304. ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
  305. #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent) \
  306. zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
  307. static zend_always_inline bool _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
  308. {
  309. const char *tmp = key;
  310. if (EXPECTED(*tmp > '9')) {
  311. return 0;
  312. } else if (*tmp < '0') {
  313. if (*tmp != '-') {
  314. return 0;
  315. }
  316. tmp++;
  317. if (*tmp > '9' || *tmp < '0') {
  318. return 0;
  319. }
  320. }
  321. return _zend_handle_numeric_str_ex(key, length, idx);
  322. }
  323. #define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
  324. _zend_handle_numeric_str(key, length, &idx)
  325. #define ZEND_HANDLE_NUMERIC(key, idx) \
  326. ZEND_HANDLE_NUMERIC_STR(ZSTR_VAL(key), ZSTR_LEN(key), idx)
  327. static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
  328. {
  329. zval *zv;
  330. zv = zend_hash_find(ht, key);
  331. return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
  332. ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
  333. }
  334. static zend_always_inline zval *zend_hash_find_ex_ind(const HashTable *ht, zend_string *key, bool known_hash)
  335. {
  336. zval *zv;
  337. zv = zend_hash_find_ex(ht, key, known_hash);
  338. return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
  339. ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
  340. }
  341. static zend_always_inline bool zend_hash_exists_ind(const HashTable *ht, zend_string *key)
  342. {
  343. zval *zv;
  344. zv = zend_hash_find(ht, key);
  345. return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
  346. Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
  347. }
  348. static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
  349. {
  350. zval *zv;
  351. zv = zend_hash_str_find(ht, str, len);
  352. return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
  353. ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
  354. }
  355. static zend_always_inline bool zend_hash_str_exists_ind(const HashTable *ht, const char *str, size_t len)
  356. {
  357. zval *zv;
  358. zv = zend_hash_str_find(ht, str, len);
  359. return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
  360. Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
  361. }
  362. static zend_always_inline zval *zend_symtable_add_new(HashTable *ht, zend_string *key, zval *pData)
  363. {
  364. zend_ulong idx;
  365. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  366. return zend_hash_index_add_new(ht, idx, pData);
  367. } else {
  368. return zend_hash_add_new(ht, key, pData);
  369. }
  370. }
  371. static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
  372. {
  373. zend_ulong idx;
  374. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  375. return zend_hash_index_update(ht, idx, pData);
  376. } else {
  377. return zend_hash_update(ht, key, pData);
  378. }
  379. }
  380. static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
  381. {
  382. zend_ulong idx;
  383. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  384. return zend_hash_index_update(ht, idx, pData);
  385. } else {
  386. return zend_hash_update_ind(ht, key, pData);
  387. }
  388. }
  389. static zend_always_inline zend_result zend_symtable_del(HashTable *ht, zend_string *key)
  390. {
  391. zend_ulong idx;
  392. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  393. return zend_hash_index_del(ht, idx);
  394. } else {
  395. return zend_hash_del(ht, key);
  396. }
  397. }
  398. static zend_always_inline zend_result zend_symtable_del_ind(HashTable *ht, zend_string *key)
  399. {
  400. zend_ulong idx;
  401. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  402. return zend_hash_index_del(ht, idx);
  403. } else {
  404. return zend_hash_del_ind(ht, key);
  405. }
  406. }
  407. static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
  408. {
  409. zend_ulong idx;
  410. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  411. return zend_hash_index_find(ht, idx);
  412. } else {
  413. return zend_hash_find(ht, key);
  414. }
  415. }
  416. static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
  417. {
  418. zend_ulong idx;
  419. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  420. return zend_hash_index_find(ht, idx);
  421. } else {
  422. return zend_hash_find_ind(ht, key);
  423. }
  424. }
  425. static zend_always_inline bool zend_symtable_exists(HashTable *ht, zend_string *key)
  426. {
  427. zend_ulong idx;
  428. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  429. return zend_hash_index_exists(ht, idx);
  430. } else {
  431. return zend_hash_exists(ht, key);
  432. }
  433. }
  434. static zend_always_inline bool zend_symtable_exists_ind(HashTable *ht, zend_string *key)
  435. {
  436. zend_ulong idx;
  437. if (ZEND_HANDLE_NUMERIC(key, idx)) {
  438. return zend_hash_index_exists(ht, idx);
  439. } else {
  440. return zend_hash_exists_ind(ht, key);
  441. }
  442. }
  443. static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
  444. {
  445. zend_ulong idx;
  446. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  447. return zend_hash_index_update(ht, idx, pData);
  448. } else {
  449. return zend_hash_str_update(ht, str, len, pData);
  450. }
  451. }
  452. static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
  453. {
  454. zend_ulong idx;
  455. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  456. return zend_hash_index_update(ht, idx, pData);
  457. } else {
  458. return zend_hash_str_update_ind(ht, str, len, pData);
  459. }
  460. }
  461. static zend_always_inline zend_result zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
  462. {
  463. zend_ulong idx;
  464. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  465. return zend_hash_index_del(ht, idx);
  466. } else {
  467. return zend_hash_str_del(ht, str, len);
  468. }
  469. }
  470. static zend_always_inline zend_result zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
  471. {
  472. zend_ulong idx;
  473. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  474. return zend_hash_index_del(ht, idx);
  475. } else {
  476. return zend_hash_str_del_ind(ht, str, len);
  477. }
  478. }
  479. static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
  480. {
  481. zend_ulong idx;
  482. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  483. return zend_hash_index_find(ht, idx);
  484. } else {
  485. return zend_hash_str_find(ht, str, len);
  486. }
  487. }
  488. static zend_always_inline bool zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
  489. {
  490. zend_ulong idx;
  491. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  492. return zend_hash_index_exists(ht, idx);
  493. } else {
  494. return zend_hash_str_exists(ht, str, len);
  495. }
  496. }
  497. static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
  498. {
  499. zval tmp, *zv;
  500. ZVAL_PTR(&tmp, pData);
  501. zv = zend_hash_add(ht, key, &tmp);
  502. if (zv) {
  503. ZEND_ASSUME(Z_PTR_P(zv));
  504. return Z_PTR_P(zv);
  505. } else {
  506. return NULL;
  507. }
  508. }
  509. static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
  510. {
  511. zval tmp, *zv;
  512. ZVAL_PTR(&tmp, pData);
  513. zv = zend_hash_add_new(ht, key, &tmp);
  514. if (zv) {
  515. ZEND_ASSUME(Z_PTR_P(zv));
  516. return Z_PTR_P(zv);
  517. } else {
  518. return NULL;
  519. }
  520. }
  521. static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
  522. {
  523. zval tmp, *zv;
  524. ZVAL_PTR(&tmp, pData);
  525. zv = zend_hash_str_add(ht, str, len, &tmp);
  526. if (zv) {
  527. ZEND_ASSUME(Z_PTR_P(zv));
  528. return Z_PTR_P(zv);
  529. } else {
  530. return NULL;
  531. }
  532. }
  533. static zend_always_inline void *zend_hash_str_add_new_ptr(HashTable *ht, const char *str, size_t len, void *pData)
  534. {
  535. zval tmp, *zv;
  536. ZVAL_PTR(&tmp, pData);
  537. zv = zend_hash_str_add_new(ht, str, len, &tmp);
  538. if (zv) {
  539. ZEND_ASSUME(Z_PTR_P(zv));
  540. return Z_PTR_P(zv);
  541. } else {
  542. return NULL;
  543. }
  544. }
  545. static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
  546. {
  547. zval tmp, *zv;
  548. ZVAL_PTR(&tmp, pData);
  549. zv = zend_hash_update(ht, key, &tmp);
  550. ZEND_ASSUME(Z_PTR_P(zv));
  551. return Z_PTR_P(zv);
  552. }
  553. static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
  554. {
  555. zval tmp, *zv;
  556. ZVAL_PTR(&tmp, pData);
  557. zv = zend_hash_str_update(ht, str, len, &tmp);
  558. ZEND_ASSUME(Z_PTR_P(zv));
  559. return Z_PTR_P(zv);
  560. }
  561. static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
  562. {
  563. zval tmp, *zv;
  564. ZVAL_PTR(&tmp, NULL);
  565. if ((zv = zend_hash_add(ht, key, &tmp))) {
  566. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  567. memcpy(Z_PTR_P(zv), pData, size);
  568. return Z_PTR_P(zv);
  569. }
  570. return NULL;
  571. }
  572. static zend_always_inline void *zend_hash_add_new_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
  573. {
  574. zval tmp, *zv;
  575. ZVAL_PTR(&tmp, NULL);
  576. if ((zv = zend_hash_add_new(ht, key, &tmp))) {
  577. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  578. memcpy(Z_PTR_P(zv), pData, size);
  579. return Z_PTR_P(zv);
  580. }
  581. return NULL;
  582. }
  583. static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
  584. {
  585. zval tmp, *zv;
  586. ZVAL_PTR(&tmp, NULL);
  587. if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
  588. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  589. memcpy(Z_PTR_P(zv), pData, size);
  590. return Z_PTR_P(zv);
  591. }
  592. return NULL;
  593. }
  594. static zend_always_inline void *zend_hash_str_add_new_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
  595. {
  596. zval tmp, *zv;
  597. ZVAL_PTR(&tmp, NULL);
  598. if ((zv = zend_hash_str_add_new(ht, str, len, &tmp))) {
  599. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  600. memcpy(Z_PTR_P(zv), pData, size);
  601. return Z_PTR_P(zv);
  602. }
  603. return NULL;
  604. }
  605. static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
  606. {
  607. void *p;
  608. p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  609. memcpy(p, pData, size);
  610. return zend_hash_update_ptr(ht, key, p);
  611. }
  612. static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
  613. {
  614. void *p;
  615. p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  616. memcpy(p, pData, size);
  617. return zend_hash_str_update_ptr(ht, str, len, p);
  618. }
  619. static zend_always_inline void *zend_hash_index_add_ptr(HashTable *ht, zend_ulong h, void *pData)
  620. {
  621. zval tmp, *zv;
  622. ZVAL_PTR(&tmp, pData);
  623. zv = zend_hash_index_add(ht, h, &tmp);
  624. return zv ? Z_PTR_P(zv) : NULL;
  625. }
  626. static zend_always_inline void *zend_hash_index_add_new_ptr(HashTable *ht, zend_ulong h, void *pData)
  627. {
  628. zval tmp, *zv;
  629. ZVAL_PTR(&tmp, pData);
  630. zv = zend_hash_index_add_new(ht, h, &tmp);
  631. return zv ? Z_PTR_P(zv) : NULL;
  632. }
  633. static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
  634. {
  635. zval tmp, *zv;
  636. ZVAL_PTR(&tmp, pData);
  637. zv = zend_hash_index_update(ht, h, &tmp);
  638. ZEND_ASSUME(Z_PTR_P(zv));
  639. return Z_PTR_P(zv);
  640. }
  641. static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
  642. {
  643. zval tmp, *zv;
  644. ZVAL_PTR(&tmp, NULL);
  645. if ((zv = zend_hash_index_add(ht, h, &tmp))) {
  646. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  647. memcpy(Z_PTR_P(zv), pData, size);
  648. return Z_PTR_P(zv);
  649. }
  650. return NULL;
  651. }
  652. static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
  653. {
  654. zval tmp, *zv;
  655. ZVAL_PTR(&tmp, pData);
  656. zv = zend_hash_next_index_insert(ht, &tmp);
  657. if (zv) {
  658. ZEND_ASSUME(Z_PTR_P(zv));
  659. return Z_PTR_P(zv);
  660. } else {
  661. return NULL;
  662. }
  663. }
  664. static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
  665. {
  666. void *p;
  667. p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  668. memcpy(p, pData, size);
  669. return zend_hash_index_update_ptr(ht, h, p);
  670. }
  671. static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
  672. {
  673. zval tmp, *zv;
  674. ZVAL_PTR(&tmp, NULL);
  675. if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
  676. Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
  677. memcpy(Z_PTR_P(zv), pData, size);
  678. return Z_PTR_P(zv);
  679. }
  680. return NULL;
  681. }
  682. static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
  683. {
  684. zval *zv;
  685. zv = zend_hash_find(ht, key);
  686. if (zv) {
  687. ZEND_ASSUME(Z_PTR_P(zv));
  688. return Z_PTR_P(zv);
  689. } else {
  690. return NULL;
  691. }
  692. }
  693. static zend_always_inline void *zend_hash_find_ex_ptr(const HashTable *ht, zend_string *key, bool known_hash)
  694. {
  695. zval *zv;
  696. zv = zend_hash_find_ex(ht, key, known_hash);
  697. if (zv) {
  698. ZEND_ASSUME(Z_PTR_P(zv));
  699. return Z_PTR_P(zv);
  700. } else {
  701. return NULL;
  702. }
  703. }
  704. static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
  705. {
  706. zval *zv;
  707. zv = zend_hash_str_find(ht, str, len);
  708. if (zv) {
  709. ZEND_ASSUME(Z_PTR_P(zv));
  710. return Z_PTR_P(zv);
  711. } else {
  712. return NULL;
  713. }
  714. }
  715. /* Will lowercase the str; use only if you don't need the lowercased string for
  716. * anything else. If you have a lowered string, use zend_hash_str_find_ptr. */
  717. ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len);
  718. /* Will lowercase the str; use only if you don't need the lowercased string for
  719. * anything else. If you have a lowered string, use zend_hash_find_ptr. */
  720. ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key);
  721. static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
  722. {
  723. zval *zv;
  724. zv = zend_hash_index_find(ht, h);
  725. if (zv) {
  726. ZEND_ASSUME(Z_PTR_P(zv));
  727. return Z_PTR_P(zv);
  728. } else {
  729. return NULL;
  730. }
  731. }
  732. static zend_always_inline zval *zend_hash_index_find_deref(HashTable *ht, zend_ulong h)
  733. {
  734. zval *zv = zend_hash_index_find(ht, h);
  735. if (zv) {
  736. ZVAL_DEREF(zv);
  737. }
  738. return zv;
  739. }
  740. static zend_always_inline zval *zend_hash_find_deref(HashTable *ht, zend_string *str)
  741. {
  742. zval *zv = zend_hash_find(ht, str);
  743. if (zv) {
  744. ZVAL_DEREF(zv);
  745. }
  746. return zv;
  747. }
  748. static zend_always_inline zval *zend_hash_str_find_deref(HashTable *ht, const char *str, size_t len)
  749. {
  750. zval *zv = zend_hash_str_find(ht, str, len);
  751. if (zv) {
  752. ZVAL_DEREF(zv);
  753. }
  754. return zv;
  755. }
  756. static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
  757. {
  758. zend_ulong idx;
  759. if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
  760. return zend_hash_index_find_ptr(ht, idx);
  761. } else {
  762. return zend_hash_str_find_ptr(ht, str, len);
  763. }
  764. }
  765. static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
  766. {
  767. zval *zv;
  768. zv = zend_hash_get_current_data_ex(ht, pos);
  769. if (zv) {
  770. ZEND_ASSUME(Z_PTR_P(zv));
  771. return Z_PTR_P(zv);
  772. } else {
  773. return NULL;
  774. }
  775. }
  776. #define zend_hash_get_current_data_ptr(ht) \
  777. zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
  778. #define ZEND_HASH_FOREACH_FROM(_ht, indirect, _from) do { \
  779. HashTable *__ht = (_ht); \
  780. Bucket *_p = __ht->arData + (_from); \
  781. Bucket *_end = __ht->arData + __ht->nNumUsed; \
  782. for (; _p != _end; _p++) { \
  783. zval *_z = &_p->val; \
  784. if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
  785. _z = Z_INDIRECT_P(_z); \
  786. } \
  787. if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
  788. #define ZEND_HASH_FOREACH(_ht, indirect) ZEND_HASH_FOREACH_FROM(_ht, indirect, 0)
  789. #define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
  790. HashTable *__ht = (_ht); \
  791. uint32_t _idx = __ht->nNumUsed; \
  792. Bucket *_p = __ht->arData + _idx; \
  793. zval *_z; \
  794. for (_idx = __ht->nNumUsed; _idx > 0; _idx--) { \
  795. _p--; \
  796. _z = &_p->val; \
  797. if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
  798. _z = Z_INDIRECT_P(_z); \
  799. } \
  800. if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
  801. #define ZEND_HASH_FOREACH_END() \
  802. } \
  803. } while (0)
  804. #define ZEND_HASH_FOREACH_END_DEL() \
  805. __ht->nNumOfElements--; \
  806. do { \
  807. uint32_t j = HT_IDX_TO_HASH(_idx - 1); \
  808. uint32_t nIndex = _p->h | __ht->nTableMask; \
  809. uint32_t i = HT_HASH(__ht, nIndex); \
  810. if (UNEXPECTED(j != i)) { \
  811. Bucket *prev = HT_HASH_TO_BUCKET(__ht, i); \
  812. while (Z_NEXT(prev->val) != j) { \
  813. i = Z_NEXT(prev->val); \
  814. prev = HT_HASH_TO_BUCKET(__ht, i); \
  815. } \
  816. Z_NEXT(prev->val) = Z_NEXT(_p->val); \
  817. } else { \
  818. HT_HASH(__ht, nIndex) = Z_NEXT(_p->val); \
  819. } \
  820. } while (0); \
  821. } \
  822. __ht->nNumUsed = _idx; \
  823. } while (0)
  824. #define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
  825. ZEND_HASH_FOREACH(ht, 0); \
  826. _bucket = _p;
  827. #define ZEND_HASH_FOREACH_BUCKET_FROM(ht, _bucket, _from) \
  828. ZEND_HASH_FOREACH_FROM(ht, 0, _from); \
  829. _bucket = _p;
  830. #define ZEND_HASH_REVERSE_FOREACH_BUCKET(ht, _bucket) \
  831. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  832. _bucket = _p;
  833. #define ZEND_HASH_FOREACH_VAL(ht, _val) \
  834. ZEND_HASH_FOREACH(ht, 0); \
  835. _val = _z;
  836. #define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
  837. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  838. _val = _z;
  839. #define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
  840. ZEND_HASH_FOREACH(ht, 1); \
  841. _val = _z;
  842. #define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
  843. ZEND_HASH_REVERSE_FOREACH(ht, 1); \
  844. _val = _z;
  845. #define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
  846. ZEND_HASH_FOREACH(ht, 0); \
  847. _ptr = Z_PTR_P(_z);
  848. #define ZEND_HASH_FOREACH_PTR_FROM(ht, _ptr, _from) \
  849. ZEND_HASH_FOREACH_FROM(ht, 0, _from); \
  850. _ptr = Z_PTR_P(_z);
  851. #define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
  852. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  853. _ptr = Z_PTR_P(_z);
  854. #define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
  855. ZEND_HASH_FOREACH(ht, 0); \
  856. _h = _p->h;
  857. #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY(ht, _h) \
  858. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  859. _h = _p->h;
  860. #define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
  861. ZEND_HASH_FOREACH(ht, 0); \
  862. _key = _p->key;
  863. #define ZEND_HASH_REVERSE_FOREACH_STR_KEY(ht, _key) \
  864. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  865. _key = _p->key;
  866. #define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
  867. ZEND_HASH_FOREACH(ht, 0); \
  868. _h = _p->h; \
  869. _key = _p->key;
  870. #define ZEND_HASH_REVERSE_FOREACH_KEY(ht, _h, _key) \
  871. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  872. _h = _p->h; \
  873. _key = _p->key;
  874. #define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
  875. ZEND_HASH_FOREACH(ht, 0); \
  876. _h = _p->h; \
  877. _val = _z;
  878. #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
  879. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  880. _h = _p->h; \
  881. _val = _z;
  882. #define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
  883. ZEND_HASH_FOREACH(ht, 0); \
  884. _key = _p->key; \
  885. _val = _z;
  886. #define ZEND_HASH_FOREACH_STR_KEY_VAL_FROM(ht, _key, _val, _from) \
  887. ZEND_HASH_FOREACH_FROM(ht, 0, _from); \
  888. _key = _p->key; \
  889. _val = _z;
  890. #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(ht, _key, _val) \
  891. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  892. _key = _p->key; \
  893. _val = _z;
  894. #define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
  895. ZEND_HASH_FOREACH(ht, 0); \
  896. _h = _p->h; \
  897. _key = _p->key; \
  898. _val = _z;
  899. #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
  900. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  901. _h = _p->h; \
  902. _key = _p->key; \
  903. _val = _z;
  904. #define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
  905. ZEND_HASH_FOREACH(ht, 1); \
  906. _key = _p->key; \
  907. _val = _z;
  908. #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
  909. ZEND_HASH_REVERSE_FOREACH(ht, 1); \
  910. _key = _p->key; \
  911. _val = _z;
  912. #define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
  913. ZEND_HASH_FOREACH(ht, 1); \
  914. _h = _p->h; \
  915. _key = _p->key; \
  916. _val = _z;
  917. #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
  918. ZEND_HASH_REVERSE_FOREACH(ht, 1); \
  919. _h = _p->h; \
  920. _key = _p->key; \
  921. _val = _z;
  922. #define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
  923. ZEND_HASH_FOREACH(ht, 0); \
  924. _h = _p->h; \
  925. _ptr = Z_PTR_P(_z);
  926. #define ZEND_HASH_REVERSE_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
  927. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  928. _h = _p->h; \
  929. _ptr = Z_PTR_P(_z);
  930. #define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
  931. ZEND_HASH_FOREACH(ht, 0); \
  932. _key = _p->key; \
  933. _ptr = Z_PTR_P(_z);
  934. #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
  935. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  936. _key = _p->key; \
  937. _ptr = Z_PTR_P(_z);
  938. #define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
  939. ZEND_HASH_FOREACH(ht, 0); \
  940. _h = _p->h; \
  941. _key = _p->key; \
  942. _ptr = Z_PTR_P(_z);
  943. #define ZEND_HASH_REVERSE_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
  944. ZEND_HASH_REVERSE_FOREACH(ht, 0); \
  945. _h = _p->h; \
  946. _key = _p->key; \
  947. _ptr = Z_PTR_P(_z);
  948. /* The following macros are useful to insert a sequence of new elements
  949. * of packed array. They may be used instead of series of
  950. * zend_hash_next_index_insert_new()
  951. * (HashTable must have enough free buckets).
  952. */
  953. #define ZEND_HASH_FILL_PACKED(ht) do { \
  954. HashTable *__fill_ht = (ht); \
  955. Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
  956. uint32_t __fill_idx = __fill_ht->nNumUsed; \
  957. ZEND_ASSERT(HT_FLAGS(__fill_ht) & HASH_FLAG_PACKED);
  958. #define ZEND_HASH_FILL_GROW() do { \
  959. if (UNEXPECTED(__fill_idx >= __fill_ht->nTableSize)) { \
  960. __fill_ht->nNumUsed = __fill_idx; \
  961. __fill_ht->nNumOfElements = __fill_idx; \
  962. __fill_ht->nNextFreeElement = __fill_idx; \
  963. zend_hash_packed_grow(__fill_ht); \
  964. __fill_bkt = __fill_ht->arData + __fill_idx; \
  965. } \
  966. } while (0);
  967. #define ZEND_HASH_FILL_SET(_val) \
  968. ZVAL_COPY_VALUE(&__fill_bkt->val, _val)
  969. #define ZEND_HASH_FILL_SET_NULL() \
  970. ZVAL_NULL(&__fill_bkt->val)
  971. #define ZEND_HASH_FILL_SET_LONG(_val) \
  972. ZVAL_LONG(&__fill_bkt->val, _val)
  973. #define ZEND_HASH_FILL_SET_DOUBLE(_val) \
  974. ZVAL_DOUBLE(&__fill_bkt->val, _val)
  975. #define ZEND_HASH_FILL_SET_STR(_val) \
  976. ZVAL_STR(&__fill_bkt->val, _val)
  977. #define ZEND_HASH_FILL_SET_STR_COPY(_val) \
  978. ZVAL_STR_COPY(&__fill_bkt->val, _val)
  979. #define ZEND_HASH_FILL_SET_INTERNED_STR(_val) \
  980. ZVAL_INTERNED_STR(&__fill_bkt->val, _val)
  981. #define ZEND_HASH_FILL_NEXT() do {\
  982. __fill_bkt->h = (__fill_idx); \
  983. __fill_bkt->key = NULL; \
  984. __fill_bkt++; \
  985. __fill_idx++; \
  986. } while (0)
  987. #define ZEND_HASH_FILL_ADD(_val) do { \
  988. ZEND_HASH_FILL_SET(_val); \
  989. ZEND_HASH_FILL_NEXT(); \
  990. } while (0)
  991. #define ZEND_HASH_FILL_FINISH() do { \
  992. __fill_ht->nNumUsed = __fill_idx; \
  993. __fill_ht->nNumOfElements = __fill_idx; \
  994. __fill_ht->nNextFreeElement = __fill_idx; \
  995. __fill_ht->nInternalPointer = 0; \
  996. } while (0)
  997. #define ZEND_HASH_FILL_END() \
  998. ZEND_HASH_FILL_FINISH(); \
  999. } while (0)
  1000. /* Check if an array is a list */
  1001. static zend_always_inline bool zend_array_is_list(zend_array *array)
  1002. {
  1003. zend_long expected_idx = 0;
  1004. zend_long num_idx;
  1005. zend_string* str_idx;
  1006. /* Empty arrays are lists */
  1007. if (zend_hash_num_elements(array) == 0) {
  1008. return 1;
  1009. }
  1010. /* Packed arrays are lists */
  1011. if (HT_IS_PACKED(array) && HT_IS_WITHOUT_HOLES(array)) {
  1012. return 1;
  1013. }
  1014. /* Check if the list could theoretically be repacked */
  1015. ZEND_HASH_FOREACH_KEY(array, num_idx, str_idx) {
  1016. if (str_idx != NULL || num_idx != expected_idx++) {
  1017. return 0;
  1018. }
  1019. } ZEND_HASH_FOREACH_END();
  1020. return 1;
  1021. }
  1022. static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, bool interned)
  1023. {
  1024. uint32_t idx = ht->nNumUsed++;
  1025. uint32_t nIndex;
  1026. Bucket *p = ht->arData + idx;
  1027. ZVAL_COPY_VALUE(&p->val, zv);
  1028. if (!interned && !ZSTR_IS_INTERNED(key)) {
  1029. HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
  1030. zend_string_addref(key);
  1031. zend_string_hash_val(key);
  1032. }
  1033. p->key = key;
  1034. p->h = ZSTR_H(key);
  1035. nIndex = (uint32_t)p->h | ht->nTableMask;
  1036. Z_NEXT(p->val) = HT_HASH(ht, nIndex);
  1037. HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
  1038. ht->nNumOfElements++;
  1039. return &p->val;
  1040. }
  1041. static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
  1042. {
  1043. return _zend_hash_append_ex(ht, key, zv, 0);
  1044. }
  1045. static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, bool interned)
  1046. {
  1047. uint32_t idx = ht->nNumUsed++;
  1048. uint32_t nIndex;
  1049. Bucket *p = ht->arData + idx;
  1050. ZVAL_PTR(&p->val, ptr);
  1051. if (!interned && !ZSTR_IS_INTERNED(key)) {
  1052. HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
  1053. zend_string_addref(key);
  1054. zend_string_hash_val(key);
  1055. }
  1056. p->key = key;
  1057. p->h = ZSTR_H(key);
  1058. nIndex = (uint32_t)p->h | ht->nTableMask;
  1059. Z_NEXT(p->val) = HT_HASH(ht, nIndex);
  1060. HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
  1061. ht->nNumOfElements++;
  1062. return &p->val;
  1063. }
  1064. static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
  1065. {
  1066. return _zend_hash_append_ptr_ex(ht, key, ptr, 0);
  1067. }
  1068. static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
  1069. {
  1070. uint32_t idx = ht->nNumUsed++;
  1071. uint32_t nIndex;
  1072. Bucket *p = ht->arData + idx;
  1073. ZVAL_INDIRECT(&p->val, ptr);
  1074. if (!ZSTR_IS_INTERNED(key)) {
  1075. HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
  1076. zend_string_addref(key);
  1077. zend_string_hash_val(key);
  1078. }
  1079. p->key = key;
  1080. p->h = ZSTR_H(key);
  1081. nIndex = (uint32_t)p->h | ht->nTableMask;
  1082. Z_NEXT(p->val) = HT_HASH(ht, nIndex);
  1083. HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
  1084. ht->nNumOfElements++;
  1085. }
  1086. #endif /* ZEND_HASH_H */