Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8fd78613 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Replace hash_map in data_dispatcher with C++ unordered_map

Change-Id: Ic8d99108fd557a1d994dcea5a2bf92aef98a0cac
parent 7e7553b5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ btosiCommonSrc := \
    ./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 \
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ static_library("osi") {
    "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",
+8 −0
Original line number Diff line number Diff line
@@ -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 @@ void data_dispatcher_register_default(data_dispatcher_t *dispatcher, fixed_queue
// 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
+16 −14
Original line number Diff line number Diff line
@@ -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 @@ void data_dispatcher_free(data_dispatcher_t *dispatcher) {
  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_free(data_dispatcher_t *dispatcher) {
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 @@ bool data_dispatcher_dispatch(data_dispatcher_t *dispatcher, data_dispatcher_typ
  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);
+2 −1
Original line number Diff line number Diff line
@@ -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"
}