14642e01fSmrg/* x-hash.h -- basic hash table class
235c4bbdfSmrg *
335c4bbdfSmrg * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
435c4bbdfSmrg *
535c4bbdfSmrg * Permission is hereby granted, free of charge, to any person
635c4bbdfSmrg * obtaining a copy of this software and associated documentation files
735c4bbdfSmrg * (the "Software"), to deal in the Software without restriction,
835c4bbdfSmrg * including without limitation the rights to use, copy, modify, merge,
935c4bbdfSmrg * publish, distribute, sublicense, and/or sell copies of the Software,
1035c4bbdfSmrg * and to permit persons to whom the Software is furnished to do so,
1135c4bbdfSmrg * subject to the following conditions:
1235c4bbdfSmrg *
1335c4bbdfSmrg * The above copyright notice and this permission notice shall be
1435c4bbdfSmrg * included in all copies or substantial portions of the Software.
1535c4bbdfSmrg *
1635c4bbdfSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1735c4bbdfSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1835c4bbdfSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1935c4bbdfSmrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
2035c4bbdfSmrg * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2135c4bbdfSmrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2235c4bbdfSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2335c4bbdfSmrg * DEALINGS IN THE SOFTWARE.
2435c4bbdfSmrg *
2535c4bbdfSmrg * Except as contained in this notice, the name(s) of the above
2635c4bbdfSmrg * copyright holders shall not be used in advertising or otherwise to
2735c4bbdfSmrg * promote the sale, use or other dealings in this Software without
2835c4bbdfSmrg * prior written authorization.
2935c4bbdfSmrg */
304642e01fSmrg
314642e01fSmrg#ifndef X_HASH_H
324642e01fSmrg#define X_HASH_H 1
334642e01fSmrg
344642e01fSmrg#include <stdlib.h>
354642e01fSmrg#include <assert.h>
364642e01fSmrg
374642e01fSmrgtypedef struct x_hash_table_struct x_hash_table;
384642e01fSmrg
3935c4bbdfSmrgtypedef int (x_compare_fun)(const void *a, const void *b);
4035c4bbdfSmrgtypedef unsigned int (x_hash_fun)(const void *k);
4135c4bbdfSmrgtypedef void (x_destroy_fun)(void *x);
4235c4bbdfSmrgtypedef void (x_hash_foreach_fun)(void *k, void *v, void *data);
434642e01fSmrg
444642e01fSmrg/* for X_PFX and X_EXTERN */
454642e01fSmrg#include "x-list.h"
464642e01fSmrg
4735c4bbdfSmrgX_EXTERN x_hash_table *X_PFX(hash_table_new) (x_hash_fun * hash,
4835c4bbdfSmrg                                              x_compare_fun * compare,
4935c4bbdfSmrg                                              x_destroy_fun * key_destroy,
5035c4bbdfSmrg                                              x_destroy_fun * value_destroy);
5135c4bbdfSmrgX_EXTERN void X_PFX(hash_table_free) (x_hash_table * h);
524642e01fSmrg
5335c4bbdfSmrgX_EXTERN unsigned int X_PFX(hash_table_size) (x_hash_table * h);
544642e01fSmrg
5535c4bbdfSmrgX_EXTERN void X_PFX(hash_table_insert) (x_hash_table * h, void *k, void *v);
5635c4bbdfSmrgX_EXTERN void X_PFX(hash_table_replace) (x_hash_table * h, void *k, void *v);
5735c4bbdfSmrgX_EXTERN void X_PFX(hash_table_remove) (x_hash_table * h, void *k);
5835c4bbdfSmrgX_EXTERN void *X_PFX(hash_table_lookup) (x_hash_table * h,
5935c4bbdfSmrg                                         void *k, void **k_ret);
6035c4bbdfSmrgX_EXTERN void X_PFX(hash_table_foreach) (x_hash_table * h,
6135c4bbdfSmrg                                         x_hash_foreach_fun * fun,
6235c4bbdfSmrg                                         void *data);
634642e01fSmrg
644642e01fSmrg/* Conversion between unsigned int (e.g. xp_resource_id) and void pointer */
654642e01fSmrg
664642e01fSmrg/* Forward declarations */
674642e01fSmrgstatic __inline__ void *
6835c4bbdfSmrgX_PFX(cvt_uint_to_vptr) (unsigned int val) __attribute__((always_inline));
694642e01fSmrgstatic __inline__ unsigned int
7035c4bbdfSmrgX_PFX(cvt_vptr_to_uint) (void * val) __attribute__((always_inline));
714642e01fSmrg
724642e01fSmrg/* Implementations */
734642e01fSmrgstatic __inline__ void *
7435c4bbdfSmrgX_PFX(cvt_uint_to_vptr) (unsigned int val) {
7535c4bbdfSmrg    return (void *)((unsigned long)(val));
764642e01fSmrg}
774642e01fSmrg
784642e01fSmrgstatic __inline__ unsigned int
7935c4bbdfSmrgX_PFX(cvt_vptr_to_uint) (void * val) {
8035c4bbdfSmrg    size_t sv = (size_t)val;
8135c4bbdfSmrg    unsigned int uv = (unsigned int)sv;
8235c4bbdfSmrg
8335c4bbdfSmrg    /* If this assert fails, chances are val actually is a pointer,
8435c4bbdfSmrg       or there's been memory corruption */
8535c4bbdfSmrg    assert(sv == uv);
8635c4bbdfSmrg
8735c4bbdfSmrg    return uv;
884642e01fSmrg}
894642e01fSmrg
904642e01fSmrg#endif /* X_HASH_H */
91