util: Deep copy a map key if the key length func is specified 47/59347/3
authorSangjin Lee <lsj119@samsung.com>
Sat, 13 Feb 2016 05:41:50 +0000 (14:41 +0900)
committerSangjin Lee <lsj119@samsung.com>
Mon, 15 Feb 2016 06:13:27 +0000 (15:13 +0900)
If key length func is specified, pepper_map_t will allocate internal space
for the key and memcpy() the key into it, otherwise, key is treated as
a normal variable. Thus, pepper_object_set/get_user_data() is modified to
use the pointer value itself as a key.

Change-Id: I141f014ea1aee552c0ce3c8a0d4e13665de71c3d

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

index d7efafb9d8a3f3249b7ca6b62a402f650fcf84b0..8c24f9f0c50d099300a7fb4e3530eab339695530 100644 (file)
@@ -48,13 +48,7 @@ pepper_object_init(pepper_object_t *object, pepper_object_type_t type)
     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)
     {
@@ -112,7 +106,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);
 }
 
 /**
@@ -128,7 +122,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 6889c65a1fb85510b4396065ec4d2798009e754b..2095b00ca9296856bee6499236c593e7f0f45d22 100644 (file)
@@ -289,6 +289,9 @@ 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_pointer_init(pepper_map_t *map, int bucket_bits, void *buckets);
+
 PEPPER_API void
 pepper_map_fini(pepper_map_t *map);
 
@@ -304,6 +307,9 @@ pepper_map_int32_create(int bucket_bits);
 PEPPER_API pepper_map_t *
 pepper_map_int64_create(int bucket_bits);
 
+PEPPER_API pepper_map_t *
+pepper_map_pointer_create(int bucket_bits);
+
 PEPPER_API void
 pepper_map_destroy(pepper_map_t *map);
 
index d3e6accb691640aae6749d65bdb859766167c15b..9e537ce09ecdc41dd91289a22d912d7ccec5f118 100644 (file)
@@ -78,56 +78,52 @@ pepper_map_init(pepper_map_t               *map,
 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
@@ -155,13 +151,27 @@ pepper_map_create(int                       bucket_bits,
 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
@@ -229,6 +239,7 @@ pepper_map_set(pepper_map_t *map, const void *key, void *data, pepper_free_func_
     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)
@@ -279,10 +290,22 @@ pepper_map_set(pepper_map_t *map, const void *key, void *data, pepper_free_func_
     }
 
     /* 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;