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