Use key as a pointer to the key value instead of key itself.
Added several convinience functions for int32/64 maps.
Change-Id: Idfba75a31fccde6b6aec4042094831cad83624ba
#include "pepper-internal.h"
-static int
-user_data_hash(const void *key, int key_length)
-{
-#if INTPTR_MAX == INT32_MAX
- return ((uint32_t)key) >> 2;
-#elif INTPTR_MAX == INT64_MAX
- return ((uint64_t)key) >> 3;
-#else
- #error "Not 32 or 64bit system"
-#endif
-}
-
-static int
-user_data_key_length(const void *key)
-{
- return sizeof(key);
-}
-
-static int
-user_data_key_compare(const void *key0, int key0_length, const void *key1, int key1_length)
-{
- return (int)(key0 - key1);
-}
-
pepper_object_t *
pepper_object_alloc(pepper_object_type_t type, size_t size)
{
{
object->type = type;
pepper_list_init(&object->event_listener_list);
- pepper_map_init(&object->user_data_map, PEPPER_OBJECT_BUCKET_BITS,
- user_data_hash, user_data_key_length, user_data_key_compare,
- &object->buckets[0]);
+
+#if INTPTR_MAX == INT32_MAX
+ pepper_map_int32_init(&object->user_data_map, PEPPER_OBJECT_BUCKET_BITS, &object->buckets[0]);
+#elif INTPTR_MAX == INT64_MAX
+ pepper_map_int64_init(&object->user_data_map, PEPPER_OBJECT_BUCKET_BITS, &object->buckets[0]);
+#else
+ #error "Not 32 or 64bit system"
+#endif
}
void
pepper_object_set_user_data(pepper_object_t *object, const void *key, void *data,
pepper_free_func_t free_func)
{
- pepper_map_set(&object->user_data_map, key, data, free_func);
+ pepper_map_set(&object->user_data_map, &key, data, free_func);
}
/**
PEPPER_API void *
pepper_object_get_user_data(pepper_object_t *object, const void *key)
{
- return pepper_map_get(&object->user_data_map, key);
+ return pepper_map_get(&object->user_data_map, &key);
}
static void
list->next = other->next;
}
+/* Hash functions from Thomas Wang https://gist.github.com/badboy/6267743 */
+static inline int
+pepper_hash32(uint32_t key)
+{
+ key = ~key + (key << 15);
+ key ^= key >> 12;
+ key += key << 2;
+ key ^= key >> 4;
+ key *= 2057;
+ key ^= key >> 16;
+
+ return key;
+}
+
+static inline int
+pepper_hash64(uint64_t key)
+{
+ key = ~key + (key << 18);
+ key ^= key >> 31;
+ key *= 21;
+ key ^= key >> 11;
+ key += key << 6;
+ key ^= key >> 22;
+
+ return (int)key;
+}
+
typedef struct pepper_map_entry pepper_map_entry_t;
typedef struct pepper_map pepper_map_t;
pepper_key_compare_func_t key_compare_func,
void *buckets);
+PEPPER_API void
+pepper_map_int32_init(pepper_map_t *map, int bucket_bits, void *buckets);
+
+PEPPER_API void
+pepper_map_int64_init(pepper_map_t *map, int bucket_bits, void *buckets);
+
PEPPER_API void
pepper_map_fini(pepper_map_t *map);
pepper_key_length_func_t key_length_func,
pepper_key_compare_func_t key_compare_func);
+PEPPER_API pepper_map_t *
+pepper_map_int32_create(int bucket_bits);
+
+PEPPER_API pepper_map_t *
+pepper_map_int64_create(int bucket_bits);
+
PEPPER_API void
pepper_map_destroy(pepper_map_t *map);
map->buckets = buckets;
}
+static int
+int32_hash(const void *key, int key_length)
+{
+ return pepper_hash32(*(const uint32_t *)key);
+}
+
+static int
+int32_key_length(const void *key)
+{
+ return 4;
+}
+
+static int
+int32_key_compare(const void *key0, int key0_length,
+ const void *key1, int key1_length)
+{
+ uint64_t k0 = *(const uint32_t *)key0;
+ uint64_t k1 = *(const uint32_t *)key1;
+ return (int)(k0 - k1);
+}
+
+
+PEPPER_API void
+pepper_map_int32_init(pepper_map_t *map, int bucket_bits, void *buckets)
+{
+ pepper_map_init(map, bucket_bits, int32_hash, int32_key_length, int32_key_compare, buckets);
+}
+
+static int
+int64_hash(const void *key, int key_length)
+{
+ return pepper_hash64(*(const uint64_t *)key);
+}
+
+static int
+int64_key_length(const void *key)
+{
+ return 8;
+}
+
+static int
+int64_key_compare(const void *key0, int key0_length,
+ const void *key1, int key1_length)
+{
+ uint64_t k0 = *(const uint64_t *)key0;
+ uint64_t k1 = *(const uint64_t *)key1;
+ return (int)(k0 - k1);
+}
+
+PEPPER_API void
+pepper_map_int64_init(pepper_map_t *map, int bucket_bits, void *buckets)
+{
+ pepper_map_init(map, bucket_bits, int64_hash, int64_key_length, int64_key_compare, buckets);
+}
+
PEPPER_API void
pepper_map_fini(pepper_map_t *map)
{
return map;
}
+PEPPER_API pepper_map_t *
+pepper_map_int32_create(int bucket_bits)
+{
+ return pepper_map_create(bucket_bits, int32_hash, int32_key_length, int32_key_compare);
+}
+
+PEPPER_API pepper_map_t *
+pepper_map_int64_create(int bucket_bits)
+{
+ return pepper_map_create(bucket_bits, int64_hash, int64_key_length, int64_key_compare);
+}
+
PEPPER_API void
pepper_map_destroy(pepper_map_t *map)
{