2016-09-26 09:37:39 -04:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2020-02-05 03:08:29 -05:00
|
|
|
#include "bt_common.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/list.h"
|
|
|
|
#include "osi/hash_map.h"
|
|
|
|
#include "osi/allocator.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
struct hash_map_t;
|
|
|
|
|
|
|
|
typedef struct hash_map_bucket_t {
|
2016-11-24 13:10:15 -05:00
|
|
|
list_t *list;
|
2016-09-26 09:37:39 -04:00
|
|
|
} hash_map_bucket_t;
|
|
|
|
|
|
|
|
typedef struct hash_map_t {
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map_bucket_t *bucket;
|
|
|
|
size_t num_bucket;
|
|
|
|
size_t hash_size;
|
|
|
|
hash_index_fn hash_fn;
|
|
|
|
key_free_fn key_fn;
|
|
|
|
data_free_fn data_fn;
|
|
|
|
key_equality_fn keys_are_equal;
|
2016-09-26 09:37:39 -04:00
|
|
|
} hash_map_t;
|
|
|
|
|
|
|
|
// Hidden constructor for list, only to be used by us.
|
2018-08-14 22:40:51 -04:00
|
|
|
list_t *list_new_internal(list_free_cb callback);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
static void bucket_free_(void *data);
|
|
|
|
static bool default_key_equality(const void *x, const void *y);
|
|
|
|
static hash_map_entry_t *find_bucket_entry_(list_t *hash_bucket_list,
|
2016-11-24 13:10:15 -05:00
|
|
|
const void *key);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
// Hidden constructor, only to be used by the allocation tracker. Behaves the same as
|
|
|
|
// |hash_map_new|, except you get to specify the allocator.
|
|
|
|
hash_map_t *hash_map_new_internal(
|
|
|
|
size_t num_bucket,
|
|
|
|
hash_index_fn hash_fn,
|
|
|
|
key_free_fn key_fn,
|
|
|
|
data_free_fn data_fn,
|
2018-08-14 22:40:51 -04:00
|
|
|
key_equality_fn equality_fn)
|
2016-11-24 13:10:15 -05:00
|
|
|
{
|
|
|
|
assert(hash_fn != NULL);
|
|
|
|
assert(num_bucket > 0);
|
2018-08-14 22:40:51 -04:00
|
|
|
hash_map_t *hash_map = osi_calloc(sizeof(hash_map_t));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map->hash_fn = hash_fn;
|
|
|
|
hash_map->key_fn = key_fn;
|
|
|
|
hash_map->data_fn = data_fn;
|
|
|
|
hash_map->keys_are_equal = equality_fn ? equality_fn : default_key_equality;
|
|
|
|
|
|
|
|
hash_map->num_bucket = num_bucket;
|
2018-08-14 22:40:51 -04:00
|
|
|
hash_map->bucket = osi_calloc(sizeof(hash_map_bucket_t) * num_bucket);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map->bucket == NULL) {
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(hash_map);
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return hash_map;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hash_map_t *hash_map_new(
|
|
|
|
size_t num_bucket,
|
|
|
|
hash_index_fn hash_fn,
|
|
|
|
key_free_fn key_fn,
|
|
|
|
data_free_fn data_fn,
|
2016-11-24 13:10:15 -05:00
|
|
|
key_equality_fn equality_fn)
|
|
|
|
{
|
2018-08-14 22:40:51 -04:00
|
|
|
return hash_map_new_internal(num_bucket, hash_fn, key_fn, data_fn, equality_fn);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void hash_map_free(hash_map_t *hash_map)
|
|
|
|
{
|
|
|
|
if (hash_map == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hash_map_clear(hash_map);
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(hash_map->bucket);
|
|
|
|
osi_free(hash_map);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
bool hash_map_is_empty(const hash_map_t *hash_map) {
|
|
|
|
assert(hash_map != NULL);
|
|
|
|
return (hash_map->hash_size == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t hash_map_size(const hash_map_t *hash_map) {
|
|
|
|
assert(hash_map != NULL);
|
|
|
|
return hash_map->hash_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t hash_map_num_buckets(const hash_map_t *hash_map) {
|
|
|
|
assert(hash_map != NULL);
|
|
|
|
return hash_map->num_bucket;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool hash_map_has_key(const hash_map_t *hash_map, const void *key)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
|
|
|
|
list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
|
|
|
|
return (hash_map_entry != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool hash_map_set(hash_map_t *hash_map, const void *key, void *data)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
|
|
|
|
|
|
|
|
if (hash_map->bucket[hash_key].list == NULL) {
|
2018-08-14 22:40:51 -04:00
|
|
|
hash_map->bucket[hash_key].list = list_new_internal(bucket_free_);
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map->bucket[hash_key].list == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map_entry) {
|
|
|
|
// Calls hash_map callback to delete the hash_map_entry.
|
|
|
|
bool rc = list_remove(hash_bucket_list, hash_map_entry);
|
|
|
|
assert(rc == true);
|
|
|
|
} else {
|
|
|
|
hash_map->hash_size++;
|
|
|
|
}
|
2018-08-14 22:40:51 -04:00
|
|
|
hash_map_entry = osi_calloc(sizeof(hash_map_entry_t));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map_entry == NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return false;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
hash_map_entry->key = key;
|
|
|
|
hash_map_entry->data = data;
|
|
|
|
hash_map_entry->hash_map = hash_map;
|
|
|
|
|
|
|
|
return list_append(hash_bucket_list, hash_map_entry);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
bool hash_map_erase(hash_map_t *hash_map, const void *key)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
|
|
|
|
list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
|
|
|
|
if (hash_map_entry == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map->hash_size--;
|
2018-08-14 22:40:51 -04:00
|
|
|
bool remove = list_remove(hash_bucket_list, hash_map_entry);
|
|
|
|
if(list_is_empty(hash_map->bucket[hash_key].list)) {
|
|
|
|
list_free(hash_map->bucket[hash_key].list);
|
|
|
|
hash_map->bucket[hash_key].list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return remove;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void *hash_map_get(const hash_map_t *hash_map, const void *key)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_index_t hash_key = hash_map->hash_fn(key) % hash_map->num_bucket;
|
|
|
|
list_t *hash_bucket_list = hash_map->bucket[hash_key].list;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
hash_map_entry_t *hash_map_entry = find_bucket_entry_(hash_bucket_list, key);
|
|
|
|
if (hash_map_entry != NULL) {
|
|
|
|
return hash_map_entry->data;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void hash_map_clear(hash_map_t *hash_map)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
for (hash_index_t i = 0; i < hash_map->num_bucket; i++) {
|
|
|
|
if (hash_map->bucket[i].list == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
list_free(hash_map->bucket[i].list);
|
|
|
|
hash_map->bucket[i].list = NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
void hash_map_foreach(hash_map_t *hash_map, hash_map_iter_cb callback, void *context)
|
|
|
|
{
|
|
|
|
assert(hash_map != NULL);
|
|
|
|
assert(callback != NULL);
|
|
|
|
|
|
|
|
for (hash_index_t i = 0; i < hash_map->num_bucket; ++i) {
|
|
|
|
if (hash_map->bucket[i].list == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const list_node_t *iter = list_begin(hash_map->bucket[i].list);
|
|
|
|
iter != list_end(hash_map->bucket[i].list);
|
|
|
|
iter = list_next(iter)) {
|
|
|
|
hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)list_node(iter);
|
|
|
|
if (!callback(hash_map_entry, context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static void bucket_free_(void *data)
|
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)data;
|
|
|
|
const hash_map_t *hash_map = hash_map_entry->hash_map;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_map->key_fn) {
|
|
|
|
hash_map->key_fn((void *)hash_map_entry->key);
|
|
|
|
}
|
|
|
|
if (hash_map->data_fn) {
|
|
|
|
hash_map->data_fn(hash_map_entry->data);
|
|
|
|
}
|
2018-08-14 22:40:51 -04:00
|
|
|
osi_free(hash_map_entry);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static hash_map_entry_t *find_bucket_entry_(list_t *hash_bucket_list,
|
|
|
|
const void *key)
|
|
|
|
{
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (hash_bucket_list == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
for (const list_node_t *iter = list_begin(hash_bucket_list);
|
|
|
|
iter != list_end(hash_bucket_list);
|
|
|
|
iter = list_next(iter)) {
|
|
|
|
hash_map_entry_t *hash_map_entry = (hash_map_entry_t *)list_node(iter);
|
|
|
|
if (hash_map_entry->hash_map->keys_are_equal(hash_map_entry->key, key)) {
|
|
|
|
return hash_map_entry;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
return NULL;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
static bool default_key_equality(const void *x, const void *y)
|
|
|
|
{
|
|
|
|
return x == y;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|