Replace hash_map in data_dispatcher with C++ unordered_map
Change-Id: Ic8d99108fd557a1d994dcea5a2bf92aef98a0cac
diff --git a/system/osi/Android.mk b/system/osi/Android.mk
index a796157..2c5fef1 100644
--- a/system/osi/Android.mk
+++ b/system/osi/Android.mk
@@ -31,7 +31,7 @@
./src/buffer.c \
./src/compat.c \
./src/config.c \
- ./src/data_dispatcher.c \
+ ./src/data_dispatcher.cc \
./src/eager_reader.c \
./src/fixed_queue.c \
./src/future.c \
diff --git a/system/osi/BUILD.gn b/system/osi/BUILD.gn
index ecaa437..c82531a 100644
--- a/system/osi/BUILD.gn
+++ b/system/osi/BUILD.gn
@@ -23,7 +23,7 @@
"src/buffer.c",
"src/compat.c",
"src/config.c",
- "src/data_dispatcher.c",
+ "src/data_dispatcher.cc",
"src/eager_reader.c",
"src/fixed_queue.c",
"src/future.c",
diff --git a/system/osi/include/data_dispatcher.h b/system/osi/include/data_dispatcher.h
index 3cdb07d..838ee31 100644
--- a/system/osi/include/data_dispatcher.h
+++ b/system/osi/include/data_dispatcher.h
@@ -23,6 +23,10 @@
#include "osi/include/fixed_queue.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define DISPATCHER_NAME_MAX 16
typedef struct data_dispatcher_t data_dispatcher_t;
@@ -54,3 +58,7 @@
// Neither |dispatcher| nor |data| may be NULL.
// Returns true if data dispatch was successful.
bool data_dispatcher_dispatch(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, void *data);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/system/osi/src/data_dispatcher.c b/system/osi/src/data_dispatcher.cc
similarity index 77%
rename from system/osi/src/data_dispatcher.c
rename to system/osi/src/data_dispatcher.cc
index 1537f3a..c8705f9 100644
--- a/system/osi/src/data_dispatcher.c
+++ b/system/osi/src/data_dispatcher.cc
@@ -21,31 +21,28 @@
#include "osi/include/data_dispatcher.h"
#include <assert.h>
+#include <unordered_map>
#include "osi/include/allocator.h"
-#include "osi/include/hash_functions.h"
-#include "osi/include/hash_map.h"
#include "osi/include/osi.h"
#include "osi/include/log.h"
#define DEFAULT_TABLE_BUCKETS 10
+typedef std::unordered_map<data_dispatcher_type_t, fixed_queue_t*> DispatchTableMap;
+
struct data_dispatcher_t {
char *name;
- hash_map_t *dispatch_table;
+ DispatchTableMap *dispatch_table;
fixed_queue_t *default_queue; // We don't own this queue
};
data_dispatcher_t *data_dispatcher_new(const char *name) {
assert(name != NULL);
- data_dispatcher_t *ret = osi_calloc(sizeof(data_dispatcher_t));
+ data_dispatcher_t *ret = (data_dispatcher_t*)osi_calloc(sizeof(data_dispatcher_t));
- ret->dispatch_table = hash_map_new(DEFAULT_TABLE_BUCKETS, hash_function_naive, NULL, NULL, NULL);
- if (!ret->dispatch_table) {
- LOG_ERROR(LOG_TAG, "%s unable to create dispatch table.", __func__);
- goto error;
- }
+ ret->dispatch_table = new DispatchTableMap();
ret->name = osi_strdup(name);
if (!ret->name) {
@@ -64,7 +61,7 @@
if (!dispatcher)
return;
- hash_map_free(dispatcher->dispatch_table);
+ delete dispatcher->dispatch_table;
osi_free(dispatcher->name);
osi_free(dispatcher);
}
@@ -72,9 +69,11 @@
void data_dispatcher_register(data_dispatcher_t *dispatcher, data_dispatcher_type_t type, fixed_queue_t *queue) {
assert(dispatcher != NULL);
- hash_map_erase(dispatcher->dispatch_table, (void *)type);
if (queue)
- hash_map_set(dispatcher->dispatch_table, (void *)type, queue);
+ (*dispatcher->dispatch_table)[type] = queue;
+ else
+ dispatcher->dispatch_table->erase(type);
+
}
void data_dispatcher_register_default(data_dispatcher_t *dispatcher, fixed_queue_t *queue) {
@@ -87,9 +86,12 @@
assert(dispatcher != NULL);
assert(data != NULL);
- fixed_queue_t *queue = hash_map_get(dispatcher->dispatch_table, (void *)type);
- if (!queue)
+ fixed_queue_t *queue;
+ auto iter = dispatcher->dispatch_table->find(type);
+ if (iter == dispatcher->dispatch_table->end())
queue = dispatcher->default_queue;
+ else
+ queue = iter->second;
if (queue)
fixed_queue_enqueue(queue, data);
diff --git a/system/osi/test/data_dispatcher_test.cc b/system/osi/test/data_dispatcher_test.cc
index 98354bc..5afa7b32 100644
--- a/system/osi/test/data_dispatcher_test.cc
+++ b/system/osi/test/data_dispatcher_test.cc
@@ -4,8 +4,9 @@
#include "AllocationTestHarness.h"
-extern "C" {
#include "osi/include/data_dispatcher.h"
+
+extern "C" {
#include "osi/include/fixed_queue.h"
#include "osi/include/osi.h"
}