pepper: pepper_map_t revised 12/56912/1
authorTaekyun Kim <tkq.kim@samsung.com>
Wed, 13 Jan 2016 12:16:07 +0000 (21:16 +0900)
committerTaekyun Kim <tkq.kim@samsung.com>
Wed, 13 Jan 2016 12:21:01 +0000 (21:21 +0900)
Use key as a pointer to the key value instead of key itself.
Added several convinience functions for int32/64 maps.

Change-Id: Idfba75a31fccde6b6aec4042094831cad83624ba

src/lib/pepper/object.c
src/lib/pepper/pepper-utils.h
src/lib/pepper/utils-map.c

index cb22fce7d80e1dbd1407a3c6a489da0e70384e7c..cbe64592478d01ebad86fb1a6b4fe3e733303ae1 100644 (file)
 
 #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)
 {
@@ -66,9 +42,14 @@ pepper_object_init(pepper_object_t *object, pepper_object_type_t type)
 {
     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
@@ -113,7 +94,7 @@ PEPPER_API 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);
 }
 
 /**
@@ -129,7 +110,7 @@ pepper_object_set_user_data(pepper_object_t *object, const void *key, void *data
 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
index 23e7e38b698e34127de3db9a5be106e625ad5c55..88bc5b6d4191bba93b501ee3f1df6b0a7c38c40c 100644 (file)
@@ -228,6 +228,33 @@ pepper_list_insert_list(pepper_list_t *list, pepper_list_t *other)
     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;
 
@@ -256,6 +283,12 @@ pepper_map_init(pepper_map_t               *map,
                 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);
 
@@ -265,6 +298,12 @@ pepper_map_create(int                       bucket_bits,
                   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);
 
index 374cf79797dfb9f90bbbebc6b9afa03c8c480ec2..01aeea3ce4703f4a836e085eed1911d4ab306932 100644 (file)
@@ -75,6 +75,61 @@ pepper_map_init(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)
 {
@@ -97,6 +152,18 @@ pepper_map_create(int                       bucket_bits,
     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)
 {