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

Commit 70f82d18 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Replace hash_map in peer with C++ unordered_map

Change-Id: Iae48d18cc370221c425b337fefb3a8255cda759a
parent ea8751f3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ LOCAL_PATH := $(call my-dir)
# Bluetooth device static library for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc

LOCAL_C_INCLUDES := \
    $(LOCAL_PATH)/.. \
@@ -32,7 +33,7 @@ LOCAL_C_INCLUDES := \
    $(bluetooth_C_INCLUDES)

LOCAL_SRC_FILES := \
    src/classic/peer.c \
    src/classic/peer.cc \
    src/controller.c \
    src/interop.c

+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

static_library("device") {
  sources = [
    "src/classic/peer.c",
    "src/classic/peer.cc",
    "src/controller.c",
    "src/interop.c",
  ]
+8 −0
Original line number Diff line number Diff line
@@ -20,6 +20,10 @@

#include "btcore/include/bdaddr.h"

#ifdef __cplusplus
extern "C" {
#endif

static const char CLASSIC_PEER_MODULE[] = "classic_peer_module";

typedef struct classic_peer_t classic_peer_t;
@@ -32,3 +36,7 @@ classic_peer_t *classic_peer_by_address(bt_bdaddr_t *address);

// Returns the bluetooth address of the |peer|. |peer| may not be NULL.
const bt_bdaddr_t *classic_peer_get_address(classic_peer_t *peer);

#ifdef __cplusplus
}
#endif
+19 −40
Original line number Diff line number Diff line
@@ -23,35 +23,26 @@
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <unordered_map>

#include "btcore/include/module.h"
#include "osi/include/allocator.h"
#include "osi/include/future.h"
#include "osi/include/hash_map.h"
#include "osi/include/osi.h"

struct classic_peer_t {
  bt_bdaddr_t address;
};

static const size_t number_of_address_buckets = 42;

static bool initialized;
static pthread_mutex_t bag_of_peers_lock;
static hash_map_t *peers_by_address;

static bool bdaddr_equality_fn(const void *x, const void *y);
static std::unordered_map<bt_bdaddr_t*,classic_peer_t*> peers_by_addresz;

// Module lifecycle functions

static future_t *init(void) {
  peers_by_address = hash_map_new(
    number_of_address_buckets,
    hash_function_bdaddr,
    NULL,
    osi_free,
    bdaddr_equality_fn);

  pthread_mutex_init(&bag_of_peers_lock, NULL);

  initialized = true;
@@ -61,22 +52,18 @@ static future_t *init(void) {
static future_t *clean_up(void) {
  initialized = false;

  hash_map_free(peers_by_address);
  peers_by_address = NULL;
  peers_by_addresz.clear();

  pthread_mutex_destroy(&bag_of_peers_lock);
  return NULL;
}

EXPORT_SYMBOL const module_t classic_peer_module = {
extern "C" EXPORT_SYMBOL const module_t classic_peer_module = {
  .name = CLASSIC_PEER_MODULE,
  .init = init,
  .start_up = NULL,
  .shut_down = NULL,
  .clean_up = clean_up,
  .dependencies = {
    NULL
  }
  .clean_up = clean_up
};

// Interface functions
@@ -85,25 +72,24 @@ classic_peer_t *classic_peer_by_address(bt_bdaddr_t *address) {
  assert(initialized);
  assert(address != NULL);

  classic_peer_t *peer = hash_map_get(peers_by_address, address);
  auto map_ptr = peers_by_addresz.find(address);
  if (map_ptr != peers_by_addresz.end()) {
    return map_ptr->second;
  }

  if (!peer) {
  pthread_mutex_lock(&bag_of_peers_lock);

  // Make sure it didn't get added in the meantime
    peer = hash_map_get(peers_by_address, address);
    if (peer)
      goto done;
  map_ptr = peers_by_addresz.find(address);
  if (map_ptr != peers_by_addresz.end())
    return map_ptr->second;

  // Splice in a new peer struct on behalf of the caller.
    peer = osi_calloc(sizeof(classic_peer_t));
  classic_peer_t *peer = (classic_peer_t*)osi_calloc(sizeof(classic_peer_t));
  peer->address = *address;
    hash_map_set(peers_by_address, &peer->address, peer);
  peers_by_addresz[&peer->address] = peer;

  pthread_mutex_unlock(&bag_of_peers_lock);
  }

done:
  return peer;
}

@@ -111,10 +97,3 @@ const bt_bdaddr_t *classic_peer_get_address(classic_peer_t *peer) {
  assert(peer != NULL);
  return &peer->address;
}

// Internal functions

// Wrapper for bdaddr_equals used in the hash map of peers by address
static bool bdaddr_equality_fn(const void *x, const void *y) {
  return bdaddr_equals((bt_bdaddr_t *)x, (bt_bdaddr_t *)y);
}