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

Commit 8b65439b authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Delay removal of owned memory until close callback completes" am: cb1ce14e

Change-Id: I8b429070ab6a797e4c70bdc2253a343d108021b7
parents 5183a91b cb1ce14e
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ constexpr char kModuleName[] = "shim::L2cap";
const ModuleFactory L2cap::Factory = ModuleFactory([]() { return new L2cap(); });

using ConnectionInterfaceDescriptor = uint16_t;
using DeleterFunction = std::function<void(ConnectionInterfaceDescriptor)>;

static const ConnectionInterfaceDescriptor kInvalidConnectionInterfaceDescriptor = 0;
static const ConnectionInterfaceDescriptor kStartConnectionInterfaceDescriptor = 64;
static const ConnectionInterfaceDescriptor kMaxConnections = UINT16_MAX - kStartConnectionInterfaceDescriptor - 1;
@@ -71,9 +73,9 @@ std::unique_ptr<packet::RawBuilder> MakeUniquePacket(const uint8_t* data, size_t
class ConnectionInterface {
 public:
  ConnectionInterface(ConnectionInterfaceDescriptor cid, std::unique_ptr<l2cap::classic::DynamicChannel> channel,
                      os::Handler* handler)
                      os::Handler* handler, DeleterFunction deleter)
      : cid_(cid), channel_(std::move(channel)), handler_(handler), on_data_ready_callback_(nullptr),
        on_connection_closed_callback_(nullptr), address_(channel_->GetDevice()) {
        on_connection_closed_callback_(nullptr), address_(channel_->GetDevice()), deleter_(deleter) {
    channel_->RegisterOnCloseCallback(
        handler_, common::BindOnce(&ConnectionInterface::OnConnectionClosed, common::Unretained(this)));
    channel_->GetQueueUpEnd()->RegisterDequeue(
@@ -136,6 +138,7 @@ class ConnectionInterface {
              address_.ToString().c_str());
    ASSERT(on_connection_closed_callback_ != nullptr);
    on_connection_closed_callback_(cid_, static_cast<int>(error_code));
    deleter_(cid_);
  }

  void SetConnectionClosedCallback(::bluetooth::shim::ConnectionClosedCallback on_connection_closed) {
@@ -158,6 +161,8 @@ class ConnectionInterface {

  const hci::Address address_;

  DeleterFunction deleter_{};

  std::queue<std::unique_ptr<packet::PacketBuilder<hci::kLittleEndian>>> write_queue_;

  bool enqueue_registered_{false};
@@ -245,14 +250,17 @@ void ConnectionInterfaceManager::FreeConnectionInterfaceDescriptor(ConnectionInt
void ConnectionInterfaceManager::AddConnection(ConnectionInterfaceDescriptor cid,
                                               std::unique_ptr<l2cap::classic::DynamicChannel> channel) {
  ASSERT(cid_to_interface_map_.count(cid) == 0);
  cid_to_interface_map_.emplace(cid, std::make_unique<ConnectionInterface>(cid, std::move(channel), handler_));
  cid_to_interface_map_.emplace(cid, std::make_unique<ConnectionInterface>(
                                         cid, std::move(channel), handler_, [this](ConnectionInterfaceDescriptor cid) {
                                           LOG_DEBUG("Deleting connection interface cid:%hd", cid);
                                           cid_to_interface_map_.erase(cid);
                                           FreeConnectionInterfaceDescriptor(cid);
                                         }));
}

void ConnectionInterfaceManager::RemoveConnection(ConnectionInterfaceDescriptor cid) {
  ASSERT(cid_to_interface_map_.count(cid) == 1);
  cid_to_interface_map_.find(cid)->second->Close();
  cid_to_interface_map_.erase(cid);
  FreeConnectionInterfaceDescriptor(cid);
}

bool ConnectionInterfaceManager::HasResources() const {