object->type = type;
pepper_list_init(&object->event_listener_list);
-#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
+ pepper_map_pointer_init(&object->user_data_map, PEPPER_OBJECT_BUCKET_BITS, &object->buckets[0]);
if (!object_map)
{
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
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;
+ return pepper_hash32((uint32_t)key);
}
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);
+ return (int)(key0 - key1);
}
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);
+ pepper_map_init(map, bucket_bits, int32_hash, NULL, 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;
+ return pepper_hash64((uint64_t)key);
}
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);
+ return (int)(key0 - key1);
}
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_map_init(map, bucket_bits, int64_hash, NULL, int64_key_compare, buckets);
+}
+
+PEPPER_API void
+pepper_map_pointer_init(pepper_map_t *map, int bucket_bits, void *buckets)
+{
+#if INTPTR_MAX == INT32_MAX
+ pepper_map_init(map, bucket_bits, int32_hash, NULL, int32_key_compare, buckets);
+#elif INTPTR_MAX == INT64_MAX
+ pepper_map_init(map, bucket_bits, int64_hash, NULL, int64_key_compare, buckets);
+#else
+ #error "Not 32 or 64bit system"
+#endif
}
PEPPER_API void
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);
+ return pepper_map_create(bucket_bits, int32_hash, NULL, 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);
+ return pepper_map_create(bucket_bits, int64_hash, NULL, int64_key_compare);
+}
+
+PEPPER_API pepper_map_t *
+pepper_map_pointer_create(int bucket_bits)
+{
+#if INTPTR_MAX == INT32_MAX
+ return pepper_map_create(bucket_bits, int32_hash, NULL, int32_key_compare);
+#elif INTPTR_MAX == INT64_MAX
+ return pepper_map_create(bucket_bits, int64_hash, NULL, int64_key_compare);
+#else
+ #error "Not 32 or 64bit system"
+#endif
+
+ return NULL;
}
PEPPER_API void
pepper_map_entry_t **bucket = get_bucket(map, key);
pepper_map_entry_t *curr = *bucket;
pepper_map_entry_t *prev = NULL;
+ int key_length = 0;
/* Find existing entry for the key. */
while (curr)
}
/* Allocate a new entry. */
- curr = malloc(sizeof(pepper_map_entry_t));
+ if (map->key_length_func)
+ key_length = map->key_length_func(key);
+
+ curr = malloc(sizeof(pepper_map_entry_t) + key_length);
PEPPER_CHECK(curr, return, "malloc() failed.\n");
- curr->key = key;
+ if (key_length > 0)
+ {
+ memcpy(curr + 1, key, key_length);
+ curr->key = (const void *)(curr + 1);
+ }
+ else
+ {
+ curr->key = key;
+ }
+
curr->data = data;
curr->free_func = free_func;