utils: Do deep copy for the map key 78/65678/1
authorTaekyun Kim <tkq.kim@samsung.com>
Thu, 7 Apr 2016 12:19:42 +0000 (21:19 +0900)
committerTaekyun Kim <tkq.kim@samsung.com>
Tue, 12 Apr 2016 05:56:02 +0000 (14:56 +0900)
Change-Id: I2a842fe61d3ae02ef2d5a2745287b4bd97808d87

src/utils/map.c

index db1d26a..55031ba 100644 (file)
@@ -119,7 +119,6 @@ int32_key_compare(const void *key0, int len0, const void *key1, int len1)
        return (int)(k0 - k1);
 }
 
-
 void
 vk_map_int32_init(vk_map_t *map, int bucket_bits, void *buckets)
 {
@@ -319,18 +318,17 @@ vk_map_clear(vk_map_t *map)
 void *
 vk_map_get(vk_map_t *map, const void *key)
 {
-       vk_map_entry_t *curr = *get_bucket(map, key);
+       vk_map_entry_t  *curr = *get_bucket(map, key);
+       int                              len0 = 0;
+       int                              len1 = 0;
+
+       if (map->key_length_func)
+               len1 = map->key_length_func(key);
 
        while (curr)
        {
-               int len0 = 0;
-               int len1 = 0;
-
                if (map->key_length_func)
-               {
                        len0 = map->key_length_func(curr->key);
-                       len1 = map->key_length_func(key);
-               }
 
                if (map->key_compare_func(curr->key, len0, key, len1) == 0)
                        return curr->data;
@@ -347,18 +345,17 @@ vk_map_set(vk_map_t *map, const void *key, void *data, vk_free_func_t free_func)
        vk_map_entry_t  **bucket = get_bucket(map, key);
        vk_map_entry_t   *curr = *bucket;
        vk_map_entry_t   *prev = NULL;
+       int                       len0 = 0;
+       int                       len1 = 0;
+
+       if (map->key_length_func)
+               len1 = map->key_length_func(key);
 
        /* Find existing entry for the key. */
        while (curr)
        {
-               int len0 = 0;
-               int len1 = 0;
-
                if (map->key_length_func)
-               {
                        len0 = map->key_length_func(curr->key);
-                       len1 = map->key_length_func(key);
-               }
 
                if (map->key_compare_func(curr->key, len0, key, len1) == 0)
                {
@@ -397,10 +394,16 @@ vk_map_set(vk_map_t *map, const void *key, void *data, vk_free_func_t free_func)
        }
 
        /* Allocate a new entry. */
-       curr = malloc(sizeof(vk_map_entry_t));
+       curr = malloc(sizeof(vk_map_entry_t) + len1);
        VK_CHECK(curr, return, "malloc() failed.\n");
 
-       curr->key = key;
+       if (len1) {
+               curr->key = curr + 1;
+               memcpy((void *)curr->key, key, len1);
+       } else {
+               curr->key = key;
+       }
+
        curr->data = data;
        curr->free_func = free_func;