1 /**
2  * Copyright 2015-2017 DataStax, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PHP_DRIVER_HASH_H
18 #define PHP_DRIVER_HASH_H
19 
20 #define uthash_malloc(sz) emalloc(sz)
21 #define uthash_free(ptr,sz) efree(ptr)
22 
23 #define HASH_FUNCTION(key,keylen,num_bkts,hashv,bkt)                \
24   hashv = php_driver_value_hash((zval*)key TSRMLS_CC); \
25   bkt = (hashv) & (num_bkts - 1U)
26 #define HASH_KEYCOMPARE(a, b, len) \
27   php_driver_value_compare((zval*)a, (zval*)b TSRMLS_CC)
28 
29 #undef HASH_ADD /* Previously defined in Zend/zend_hash.h */
30 
31 #include "util/uthash.h"
32 
33 #define HASH_FIND_ZVAL(head, zvptr, out) \
34     HASH_FIND(hh, head, zvptr, 0, out)
35 
36 #define HASH_ADD_ZVAL(head, fieldname, add) \
37    HASH_ADD_KEYPTR(hh, head, PHP5TO7_ZVAL_MAYBE_P(((add)->fieldname)), 0, add)
38 
39 struct php_driver_map_entry_ {
40   php5to7_zval key;
41   php5to7_zval value;
42   UT_hash_handle hh;
43 };
44 
45 struct php_driver_set_entry_ {
46   php5to7_zval value;
47   UT_hash_handle hh;
48 };
49 
50 #define PHP_DRIVER_COMPARE(a, b) ((a) < (b) ? -1 : (a) > (b))
51 
52 unsigned php_driver_value_hash(zval* zvalue TSRMLS_DC);
53 int php_driver_value_compare(zval* zvalue1, zval* zvalue2 TSRMLS_DC);
54 int php_driver_data_compare(const void* a, const void* b TSRMLS_DC);
55 
56 unsigned php_driver_mpz_hash(unsigned seed, mpz_t n);
57 
php_driver_bigint_hash(cass_int64_t value)58 static inline unsigned php_driver_bigint_hash(cass_int64_t value) {
59   return (unsigned)(value ^ (value >> 32));
60 }
61 
php_driver_combine_hash(unsigned seed,unsigned hashv)62 static inline unsigned php_driver_combine_hash(unsigned seed, unsigned  hashv) {
63   return seed ^ (hashv + 0x9e3779b9 + (seed << 6) + (seed >> 2));
64 }
65 
66 #endif /* PHP_DRIVER_HASH_H */
67