/*
- * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
*
* 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 <config.h>
-
#include <stdlib.h>
+#include <jansson_config.h> /* 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;
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;
}
};
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];
}
hashtable->free_key(pair->key);
if(hashtable->free_value)
hashtable->free_value(pair->value);
- free(pair);
+ jsonp_free(pair);
}
}
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;
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;
}
void hashtable_destroy(hashtable_t *hashtable)
{
hashtable_close(hashtable);
- free(hashtable);
+ jsonp_free(hashtable);
}
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;
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)
}
else
{
- pair = malloc(sizeof(pair_t));
+ pair = jsonp_malloc(sizeof(pair_t));
if(!pair)
return -1;