1*4882a593Smuzhiyun /* x-hash.h -- basic hash table class
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person
6*4882a593Smuzhiyun * obtaining a copy of this software and associated documentation files
7*4882a593Smuzhiyun * (the "Software"), to deal in the Software without restriction,
8*4882a593Smuzhiyun * including without limitation the rights to use, copy, modify, merge,
9*4882a593Smuzhiyun * publish, distribute, sublicense, and/or sell copies of the Software,
10*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so,
11*4882a593Smuzhiyun * subject to the following conditions:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be
14*4882a593Smuzhiyun * included in all copies or substantial portions of the Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
20*4882a593Smuzhiyun * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*4882a593Smuzhiyun * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*4882a593Smuzhiyun * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Except as contained in this notice, the name(s) of the above
26*4882a593Smuzhiyun * copyright holders shall not be used in advertising or otherwise to
27*4882a593Smuzhiyun * promote the sale, use or other dealings in this Software without
28*4882a593Smuzhiyun * prior written authorization.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifndef X_HASH_H
32*4882a593Smuzhiyun #define X_HASH_H 1
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include <stdlib.h>
35*4882a593Smuzhiyun #include <assert.h>
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun typedef struct x_hash_table_struct x_hash_table;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun typedef int (x_compare_fun)(const void *a, const void *b);
40*4882a593Smuzhiyun typedef unsigned int (x_hash_fun)(const void *k);
41*4882a593Smuzhiyun typedef void (x_destroy_fun)(void *x);
42*4882a593Smuzhiyun typedef void (x_hash_foreach_fun)(void *k, void *v, void *data);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* for X_PFX and X_EXTERN */
45*4882a593Smuzhiyun #include "x-list.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun X_EXTERN x_hash_table *X_PFX(hash_table_new) (x_hash_fun * hash,
48*4882a593Smuzhiyun x_compare_fun * compare,
49*4882a593Smuzhiyun x_destroy_fun * key_destroy,
50*4882a593Smuzhiyun x_destroy_fun * value_destroy);
51*4882a593Smuzhiyun X_EXTERN void X_PFX(hash_table_free) (x_hash_table * h);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun X_EXTERN unsigned int X_PFX(hash_table_size) (x_hash_table * h);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun X_EXTERN void X_PFX(hash_table_insert) (x_hash_table * h, void *k, void *v);
56*4882a593Smuzhiyun X_EXTERN void X_PFX(hash_table_replace) (x_hash_table * h, void *k, void *v);
57*4882a593Smuzhiyun X_EXTERN void X_PFX(hash_table_remove) (x_hash_table * h, void *k);
58*4882a593Smuzhiyun X_EXTERN void *X_PFX(hash_table_lookup) (x_hash_table * h,
59*4882a593Smuzhiyun void *k, void **k_ret);
60*4882a593Smuzhiyun X_EXTERN void X_PFX(hash_table_foreach) (x_hash_table * h,
61*4882a593Smuzhiyun x_hash_foreach_fun * fun,
62*4882a593Smuzhiyun void *data);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* Conversion between unsigned int (e.g. xp_resource_id) and void pointer */
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Forward declarations */
67*4882a593Smuzhiyun static __inline__ void *
68*4882a593Smuzhiyun X_PFX(cvt_uint_to_vptr) (unsigned int val) __attribute__((always_inline));
69*4882a593Smuzhiyun static __inline__ unsigned int
70*4882a593Smuzhiyun X_PFX(cvt_vptr_to_uint) (void * val) __attribute__((always_inline));
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* Implementations */
73*4882a593Smuzhiyun static __inline__ void *
X_PFX(cvt_uint_to_vptr)74*4882a593Smuzhiyun X_PFX(cvt_uint_to_vptr) (unsigned int val) {
75*4882a593Smuzhiyun return (void *)((unsigned long)(val));
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun static __inline__ unsigned int
X_PFX(cvt_vptr_to_uint)79*4882a593Smuzhiyun X_PFX(cvt_vptr_to_uint) (void * val) {
80*4882a593Smuzhiyun size_t sv = (size_t)val;
81*4882a593Smuzhiyun unsigned int uv = (unsigned int)sv;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* If this assert fails, chances are val actually is a pointer,
84*4882a593Smuzhiyun or there's been memory corruption */
85*4882a593Smuzhiyun assert(sv == uv);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun return uv;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #endif /* X_HASH_H */
91