X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fhashtable.c;h=de25c2126a704f07834574acf7bf28421906a629;hb=42b651ef56168937d0046bde9205f1014a940941;hp=dcba68aab7592327e1b7b571cd220552b48d7ff9;hpb=56643d4311ecb26001527520da6264d92eab3d76;p=jansson.git diff --git a/src/hashtable.c b/src/hashtable.c index dcba68a..de25c21 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -1,31 +1,28 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2011 Petri Lehtinen * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ -#include - #include +#include /* for JSON_INLINE */ +#include "jansson_private.h" /* for container_of() */ #include "hashtable.h" typedef struct hashtable_list list_t; typedef struct hashtable_pair pair_t; typedef struct hashtable_bucket bucket_t; -#define container_of(ptr_, type_, member_) \ - ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_)) - #define list_to_pair(list_) container_of(list_, pair_t, list) -static inline void list_init(list_t *list) +static JSON_INLINE void list_init(list_t *list) { list->next = list; list->prev = list; } -static inline void list_insert(list_t *list, list_t *node) +static JSON_INLINE void list_insert(list_t *list, list_t *node) { node->next = list; node->prev = list->prev; @@ -33,13 +30,13 @@ static inline void list_insert(list_t *list, list_t *node) list->prev = node; } -static inline void list_remove(list_t *list) +static JSON_INLINE void list_remove(list_t *list) { list->prev->next = list->next; list->next->prev = list->prev; } -static inline int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) +static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) { return bucket->first == &hashtable->list && bucket->first == bucket->last; } @@ -67,7 +64,7 @@ static size_t primes[] = { }; static const size_t num_primes = sizeof(primes) / sizeof(size_t); -static inline size_t num_buckets(hashtable_t *hashtable) +static JSON_INLINE size_t num_buckets(hashtable_t *hashtable) { return primes[hashtable->num_buckets]; } @@ -148,7 +145,7 @@ static void hashtable_do_clear(hashtable_t *hashtable) hashtable->free_key(pair->key); if(hashtable->free_value) hashtable->free_value(pair->value); - free(pair); + jsonp_free(pair); } } @@ -158,12 +155,12 @@ static int hashtable_do_rehash(hashtable_t *hashtable) pair_t *pair; size_t i, index, new_size; - free(hashtable->buckets); + jsonp_free(hashtable->buckets); hashtable->num_buckets++; new_size = num_buckets(hashtable); - hashtable->buckets = malloc(new_size * sizeof(bucket_t)); + hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t)); if(!hashtable->buckets) return -1; @@ -190,13 +187,13 @@ static int hashtable_do_rehash(hashtable_t *hashtable) hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, free_fn free_key, free_fn free_value) { - hashtable_t *hashtable = malloc(sizeof(hashtable_t)); + hashtable_t *hashtable = jsonp_malloc(sizeof(hashtable_t)); if(!hashtable) return NULL; if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value)) { - free(hashtable); + jsonp_free(hashtable); return NULL; } @@ -206,7 +203,7 @@ hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, void hashtable_destroy(hashtable_t *hashtable) { hashtable_close(hashtable); - free(hashtable); + jsonp_free(hashtable); } int hashtable_init(hashtable_t *hashtable, @@ -217,7 +214,7 @@ int hashtable_init(hashtable_t *hashtable, hashtable->size = 0; hashtable->num_buckets = 0; /* index to primes[] */ - hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t)); + hashtable->buckets = jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t)); if(!hashtable->buckets) return -1; @@ -240,7 +237,7 @@ int hashtable_init(hashtable_t *hashtable, void hashtable_close(hashtable_t *hashtable) { hashtable_do_clear(hashtable); - free(hashtable->buckets); + jsonp_free(hashtable->buckets); } int hashtable_set(hashtable_t *hashtable, void *key, void *value) @@ -269,7 +266,7 @@ int hashtable_set(hashtable_t *hashtable, void *key, void *value) } else { - pair = malloc(sizeof(pair_t)); + pair = jsonp_malloc(sizeof(pair_t)); if(!pair) return -1;