2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH(type) struct { type **data; uint count, order; }
|
2013-09-10 18:09:36 +08:00
|
|
|
#define HASH_TYPE(v) typeof(** (v).data)
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_SIZE(v) (1 << (v).order)
|
2013-11-27 05:37:24 +08:00
|
|
|
|
|
|
|
#define HASH_EQ(v,id,k1,k2...) (id##_EQ(k1, k2))
|
|
|
|
#define HASH_FN(v,id,key...) ((u32) (id##_FN(key)) >> (32 - (v).order))
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
|
|
|
|
#define HASH_INIT(v,pool,init_order) \
|
2013-09-10 18:09:36 +08:00
|
|
|
({ \
|
2013-09-17 05:57:40 +08:00
|
|
|
(v).count = 0; \
|
|
|
|
(v).order = (init_order); \
|
|
|
|
(v).data = mb_allocz(pool, HASH_SIZE(v) * sizeof(* (v).data)); \
|
2013-09-10 18:09:36 +08:00
|
|
|
})
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_FIND(v,id,key...) \
|
2013-09-10 18:09:36 +08:00
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
u32 _h = HASH_FN(v, id, key); \
|
2013-09-17 05:57:40 +08:00
|
|
|
HASH_TYPE(v) *_n = (v).data[_h]; \
|
2013-11-27 05:37:24 +08:00
|
|
|
while (_n && !HASH_EQ(v, id, id##_KEY(_n), key)) \
|
2013-09-17 05:57:40 +08:00
|
|
|
_n = id##_NEXT(_n); \
|
2013-09-10 18:09:36 +08:00
|
|
|
_n; \
|
|
|
|
})
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_INSERT(v,id,node) \
|
2013-09-10 18:09:36 +08:00
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
u32 _h = HASH_FN(v, id, id##_KEY((node))); \
|
2013-09-17 05:57:40 +08:00
|
|
|
HASH_TYPE(v) **_nn = (v).data + _h; \
|
|
|
|
id##_NEXT(node) = *_nn; \
|
2013-09-10 18:09:36 +08:00
|
|
|
*_nn = node; \
|
2013-09-17 05:57:40 +08:00
|
|
|
(v).count++; \
|
2013-09-10 18:09:36 +08:00
|
|
|
})
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_DO_REMOVE(v,id,_nn) \
|
2013-09-10 18:09:36 +08:00
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
*_nn = id##_NEXT((*_nn)); \
|
|
|
|
(v).count--; \
|
2013-09-10 18:09:36 +08:00
|
|
|
})
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_DELETE(v,id,key...) \
|
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
u32 _h = HASH_FN(v, id, key); \
|
|
|
|
HASH_TYPE(v) *_n, **_nn = (v).data + _h; \
|
2013-09-17 05:57:40 +08:00
|
|
|
\
|
2013-11-27 05:37:24 +08:00
|
|
|
while ((*_nn) && !HASH_EQ(v, id, id##_KEY((*_nn)), key)) \
|
2013-09-17 05:57:40 +08:00
|
|
|
_nn = &(id##_NEXT((*_nn))); \
|
|
|
|
\
|
2013-11-27 05:37:24 +08:00
|
|
|
if (_n = *_nn) \
|
|
|
|
HASH_DO_REMOVE(v,id,_nn); \
|
|
|
|
_n; \
|
2013-09-17 05:57:40 +08:00
|
|
|
})
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
#define HASH_REMOVE(v,id,node) \
|
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
u32 _h = HASH_FN(v, id, id##_KEY((node))); \
|
|
|
|
HASH_TYPE(v) *_n, **_nn = (v).data + _h; \
|
2013-09-17 05:57:40 +08:00
|
|
|
\
|
2013-09-10 18:09:36 +08:00
|
|
|
while ((*_nn) && (*_nn != (node))) \
|
2013-09-17 05:57:40 +08:00
|
|
|
_nn = &(id##_NEXT((*_nn))); \
|
2013-09-10 18:09:36 +08:00
|
|
|
\
|
2013-11-27 05:37:24 +08:00
|
|
|
if (_n = *_nn) \
|
|
|
|
HASH_DO_REMOVE(v,id,_nn); \
|
|
|
|
_n; \
|
2013-09-10 18:09:36 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
#define HASH_REHASH(v,id,pool,step) \
|
|
|
|
({ \
|
|
|
|
HASH_TYPE(v) *_n, *_n2, **_od; \
|
2013-11-27 05:37:24 +08:00
|
|
|
uint _i, _os; \
|
2013-09-17 05:57:40 +08:00
|
|
|
\
|
2013-11-27 05:37:24 +08:00
|
|
|
_os = HASH_SIZE(v); \
|
2013-09-17 05:57:40 +08:00
|
|
|
_od = (v).data; \
|
|
|
|
(v).count = 0; \
|
|
|
|
(v).order += (step); \
|
|
|
|
(v).data = mb_allocz(pool, HASH_SIZE(v) * sizeof(* (v).data)); \
|
|
|
|
\
|
2013-11-27 05:37:24 +08:00
|
|
|
for (_i = 0; _i < _os; _i++) \
|
2013-09-17 05:57:40 +08:00
|
|
|
for (_n = _od[_i]; _n && (_n2 = id##_NEXT(_n), 1); _n = _n2) \
|
|
|
|
HASH_INSERT(v, id, _n); \
|
|
|
|
\
|
|
|
|
mb_free(_od); \
|
|
|
|
})
|
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
#define REHASH_LO_MARK(a,b,c,d,e,f) a
|
|
|
|
#define REHASH_HI_MARK(a,b,c,d,e,f) b
|
|
|
|
#define REHASH_LO_STEP(a,b,c,d,e,f) c
|
|
|
|
#define REHASH_HI_STEP(a,b,c,d,e,f) d
|
|
|
|
#define REHASH_LO_BOUND(a,b,c,d,e,f) e
|
|
|
|
#define REHASH_HI_BOUND(a,b,c,d,e,f) f
|
|
|
|
|
|
|
|
#define HASH_DEFINE_REHASH_FN(id,type) \
|
|
|
|
static void id##_REHASH(void *v, pool *p, int step) \
|
2013-09-17 05:57:40 +08:00
|
|
|
{ HASH_REHASH(* (HASH(type) *) v, id, p, step); }
|
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
|
|
|
|
#define HASH_MAY_STEP_UP(v,id,pool) HASH_MAY_STEP_UP_(v,pool, id##_REHASH, id##_PARAMS)
|
|
|
|
#define HASH_MAY_STEP_DOWN(v,id,pool) HASH_MAY_STEP_DOWN_(v,pool, id##_REHASH, id##_PARAMS)
|
|
|
|
#define HASH_MAY_RESIZE_DOWN(v,id,pool) HASH_MAY_RESIZE_DOWN_(v,pool, id##_REHASH, id##_PARAMS)
|
|
|
|
|
|
|
|
#define HASH_MAY_STEP_UP_(v,pool,rehash_fn,args) \
|
|
|
|
({ \
|
|
|
|
if (((v).count > (HASH_SIZE(v) REHASH_HI_MARK(args))) && \
|
|
|
|
((v).order < (REHASH_HI_BOUND(args)))) \
|
|
|
|
rehash_fn(&(v), pool, REHASH_HI_STEP(args)); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define HASH_MAY_STEP_DOWN_(v,pool,rehash_fn,args) \
|
|
|
|
({ \
|
|
|
|
if (((v).count < (HASH_SIZE(v) REHASH_LO_MARK(args))) && \
|
|
|
|
((v).order > (REHASH_LO_BOUND(args)))) \
|
|
|
|
rehash_fn(&(v), pool, -(REHASH_LO_STEP(args))); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define HASH_MAY_RESIZE_DOWN_(v,pool,rehash_fn,args) \
|
|
|
|
({ \
|
|
|
|
int _o = (v).order; \
|
|
|
|
while (((v).count < ((1 << _o) REHASH_LO_MARK(args))) && \
|
|
|
|
(_o > (REHASH_LO_BOUND(args)))) \
|
|
|
|
_o -= (REHASH_LO_STEP(args)); \
|
|
|
|
if (_o < (v).order) \
|
|
|
|
rehash_fn(&(v), pool, _o - (int) (v).order); \
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
#define HASH_INSERT2(v,id,pool,node) \
|
2013-09-17 05:57:40 +08:00
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_INSERT(v, id, node); \
|
|
|
|
HASH_MAY_STEP_UP(v, id, pool); \
|
2013-09-17 05:57:40 +08:00
|
|
|
})
|
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
#define HASH_DELETE2(v,id,pool,key...) \
|
2013-09-17 05:57:40 +08:00
|
|
|
({ \
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_TYPE(v) *_n = HASH_DELETE(v, id, key); \
|
|
|
|
if (_n) HASH_MAY_STEP_DOWN(v, id, pool); \
|
|
|
|
_n; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define HASH_REMOVE2(v,id,pool,node) \
|
|
|
|
({ \
|
|
|
|
HASH_TYPE(v) *_n = HASH_REMOVE(v, id, node); \
|
|
|
|
if (_n) HASH_MAY_STEP_DOWN(v, id, pool); \
|
|
|
|
_n; \
|
2013-09-17 05:57:40 +08:00
|
|
|
})
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
#define HASH_WALK(v,next,n) \
|
|
|
|
do { \
|
|
|
|
HASH_TYPE(v) *n; \
|
|
|
|
uint _i; \
|
2013-09-17 05:57:40 +08:00
|
|
|
uint _s = HASH_SIZE(v); \
|
|
|
|
for (_i = 0; _i < _s; _i++) \
|
2013-09-10 18:09:36 +08:00
|
|
|
for (n = (v).data[_i]; n; n = n->next)
|
|
|
|
|
|
|
|
#define HASH_WALK_END } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define HASH_WALK_DELSAFE(v,next,n) \
|
|
|
|
do { \
|
|
|
|
HASH_TYPE(v) *n, *_next; \
|
|
|
|
uint _i; \
|
2013-09-17 05:57:40 +08:00
|
|
|
uint _s = HASH_SIZE(v); \
|
|
|
|
for (_i = 0; _i < _s; _i++) \
|
2013-09-10 18:09:36 +08:00
|
|
|
for (n = (v).data[_i]; n && (_next = n->next, 1); n = _next)
|
|
|
|
|
|
|
|
#define HASH_WALK_DELSAFE_END } while (0)
|
|
|
|
|
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
#define HASH_WALK_FILTER(v,next,n,nn) \
|
|
|
|
do { \
|
|
|
|
HASH_TYPE(v) *n, **nn; \
|
|
|
|
uint _i; \
|
|
|
|
uint _s = HASH_SIZE(v); \
|
|
|
|
for (_i = 0; _i < _s; _i++) \
|
|
|
|
for (nn = (v).data + _i; n = *nn; (*nn == n) ? (nn = &n->next) : NULL)
|
|
|
|
|
|
|
|
#define HASH_WALK_FILTER_END } while (0)
|
|
|
|
|
2016-05-12 22:16:25 +08:00
|
|
|
static inline void
|
2016-05-13 19:46:46 +08:00
|
|
|
mem_hash_init(u64 *h)
|
2016-05-12 22:16:25 +08:00
|
|
|
{
|
|
|
|
*h = 0x001047d54778bcafULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2016-05-13 19:46:46 +08:00
|
|
|
mem_hash_mix(u64 *h, void *p, int s)
|
2016-02-10 20:26:07 +08:00
|
|
|
{
|
|
|
|
const u64 multiplier = 0xb38bc09a61202731ULL;
|
2016-05-12 22:16:25 +08:00
|
|
|
const char *pp = p;
|
|
|
|
uint i;
|
2016-05-13 19:46:46 +08:00
|
|
|
for (i=0; i<s/4; i++)
|
|
|
|
*h = *h * multiplier + ((const u32 *)pp)[i];
|
|
|
|
|
|
|
|
for (i=s & ~0x3; i<s; i++)
|
2016-05-12 22:16:25 +08:00
|
|
|
*h = *h * multiplier + pp[i];
|
|
|
|
}
|
2016-02-10 20:26:07 +08:00
|
|
|
|
2016-05-12 22:16:25 +08:00
|
|
|
static inline uint
|
2016-05-13 19:46:46 +08:00
|
|
|
mem_hash_value(u64 *h)
|
2016-05-12 22:16:25 +08:00
|
|
|
{
|
2016-05-13 19:46:46 +08:00
|
|
|
return ((*h >> 32) ^ (*h & 0xffffffff));
|
2016-02-10 20:26:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 22:16:25 +08:00
|
|
|
static inline uint
|
|
|
|
mem_hash(void *p, int s)
|
|
|
|
{
|
2016-05-13 19:46:46 +08:00
|
|
|
static u64 h;
|
2016-05-12 22:16:25 +08:00
|
|
|
mem_hash_init(&h);
|
|
|
|
mem_hash_mix(&h, p, s);
|
|
|
|
return mem_hash_value(&h);
|
|
|
|
}
|
|
|
|
|