aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-12-14 12:42:19 +0100
committerSven Gothel <[email protected]>2020-12-14 12:42:19 +0100
commit7a5886cc138971f67de3fb4ba95227ee87ffd437 (patch)
tree1464bc2f1f731b268039368e9d5981b1d5805968 /src/direct_bt
parent2157c1cf56140aceade31a63b450828d766a7f51 (diff)
smart_ptr-5: Use std::make_shared<T>(..) where possible, reducing two memory allocations to one
smart_ptr change series, commit #5 This series started with commit cafd8d1f3689135eae36854a9cca4def690045fd, see for details.
Diffstat (limited to 'src/direct_bt')
-rw-r--r--src/direct_bt/BTTypes.cpp12
-rw-r--r--src/direct_bt/DBTAdapter.cpp8
-rw-r--r--src/direct_bt/DBTDevice.cpp4
-rw-r--r--src/direct_bt/DBTManager.cpp3
-rw-r--r--src/direct_bt/GATTHandler.cpp14
-rw-r--r--src/direct_bt/GATTNumbers.cpp6
-rw-r--r--src/direct_bt/MgmtTypes.cpp10
7 files changed, 29 insertions, 28 deletions
diff --git a/src/direct_bt/BTTypes.cpp b/src/direct_bt/BTTypes.cpp
index ac10e8ad..1e8e7daf 100644
--- a/src/direct_bt/BTTypes.cpp
+++ b/src/direct_bt/BTTypes.cpp
@@ -490,9 +490,9 @@ void EInfoReport::setShortName(const uint8_t *buffer, int buffer_len) noexcept {
void EInfoReport::setManufactureSpecificData(uint16_t const company, uint8_t const * const data, int const data_len) noexcept {
if( nullptr == data || 0 >= data_len ) {
- msd = std::shared_ptr<ManufactureSpecificData>(new ManufactureSpecificData(company));
+ msd = std::make_shared<ManufactureSpecificData>(company);
} else {
- msd = std::shared_ptr<ManufactureSpecificData>(new ManufactureSpecificData(company, data, data_len));
+ msd = std::make_shared<ManufactureSpecificData>(company, data, data_len);
}
set(EIRDataType::MANUF_DATA);
}
@@ -619,21 +619,21 @@ int EInfoReport::read_data(uint8_t const * data, uint8_t const data_length) noex
case GAP_T::UUID16_INCOMPLETE:
case GAP_T::UUID16_COMPLETE:
for(int j=0; j<elem_len/2; j++) {
- const std::shared_ptr<uuid_t> uuid(new uuid16_t(elem_data, j*2, true));
+ const std::shared_ptr<uuid_t> uuid( std::make_shared<uuid16_t>(elem_data, j*2, true) );
addService(std::move(uuid));
}
break;
case GAP_T::UUID32_INCOMPLETE:
case GAP_T::UUID32_COMPLETE:
for(int j=0; j<elem_len/4; j++) {
- const std::shared_ptr<uuid_t> uuid(new uuid32_t(elem_data, j*4, true));
+ const std::shared_ptr<uuid_t> uuid( std::make_shared<uuid32_t>(elem_data, j*4, true) );
addService(std::move(uuid));
}
break;
case GAP_T::UUID128_INCOMPLETE:
case GAP_T::UUID128_COMPLETE:
for(int j=0; j<elem_len/16; j++) {
- const std::shared_ptr<uuid_t> uuid(new uuid128_t(elem_data, j*16, true));
+ const std::shared_ptr<uuid_t> uuid( std::make_shared<uuid128_t>(elem_data, j*16, true) );
addService(std::move(uuid));
}
break;
@@ -723,7 +723,7 @@ std::vector<std::shared_ptr<EInfoReport>> EInfoReport::read_ad_reports(uint8_t c
const uint64_t timestamp = jau::getCurrentMilliseconds();
for(i = 0; i < num_reports && i_octets < limes; i++) {
- ad_reports.push_back(std::shared_ptr<EInfoReport>(new EInfoReport()));
+ ad_reports.push_back( std::make_shared<EInfoReport>() );
ad_reports[i]->setSource(Source::AD);
ad_reports[i]->setTimestamp(timestamp);
ad_reports[i]->setEvtType(static_cast<AD_PDU_Type>(*i_octets++));
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 1ba3b1af..5f01e30a 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -204,8 +204,8 @@ bool DBTAdapter::validateDevInfo() noexcept {
return true;
errout0:
- adapterInfo = std::shared_ptr<AdapterInfo>( new AdapterInfo(dev_id, EUI48::ANY_DEVICE, 0, 0,
- AdapterSetting::NONE, AdapterSetting::NONE, 0, "invalid", "invalid"));
+ adapterInfo = std::make_shared<AdapterInfo>(dev_id, EUI48::ANY_DEVICE, 0, 0,
+ AdapterSetting::NONE, AdapterSetting::NONE, 0, "invalid", "invalid");
return false;
}
@@ -1065,6 +1065,7 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(const MgmtEvent& e) noexcept {
}
if( nullptr == device ) {
// a whitelist auto-connect w/o previous discovery
+ // private ctor: device = std::make_shared<DBTDevice>(*this, ad_report);
device = std::shared_ptr<DBTDevice>(new DBTDevice(*this, ad_report));
addDiscoveredDevice(device);
addSharedDevice(device);
@@ -1294,7 +1295,7 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
std::shared_ptr<EInfoReport> eir = deviceFoundEvent.getEIR();
if( nullptr == eir ) {
// Sourced from Linux Mgmt or otherwise ...
- eir = std::shared_ptr<EInfoReport>(new EInfoReport());
+ eir = std::make_shared<EInfoReport>();
eir->setSource(EInfoReport::Source::EIR_MGMT);
eir->setTimestamp(deviceFoundEvent.getTimestamp());
eir->setEvtType(AD_PDU_Type::ADV_IND);
@@ -1354,6 +1355,7 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
//
// new device
//
+ // private ctor: dev = std::make_shared<DBTDevice>(*this, *eir);
dev = std::shared_ptr<DBTDevice>(new DBTDevice(*this, *eir));
addDiscoveredDevice(dev);
addSharedDevice(dev);
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index 60098ec2..5a558751 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -1252,7 +1252,7 @@ bool DBTDevice::connectSMP(std::shared_ptr<DBTDevice> sthis, const BTSecurityLev
smpHandler = nullptr;
}
- smpHandler = std::shared_ptr<SMPHandler>(new SMPHandler(sthis));
+ smpHandler = std::make_shared<SMPHandler>(sthis);
if( !smpHandler->isConnected() ) {
ERR_PRINT("DBTDevice::connectSMP: Connection failed");
smpHandler = nullptr;
@@ -1297,7 +1297,7 @@ bool DBTDevice::connectGATT(std::shared_ptr<DBTDevice> sthis) noexcept {
gattHandler = nullptr;
}
- gattHandler = std::shared_ptr<GATTHandler>(new GATTHandler(sthis, l2cap_att));
+ gattHandler = std::make_shared<GATTHandler>(sthis, l2cap_att);
if( !gattHandler->isConnected() ) {
ERR_PRINT2("DBTDevice::connectGATT: Connection failed");
gattHandler = nullptr;
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index e98b55d6..43e4a5b9 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -981,8 +981,7 @@ bool DBTManager::addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndT
if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) {
const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get());
if( MgmtStatus::SUCCESS == res1.getStatus() ) {
- std::shared_ptr<WhitelistElem> wle( new WhitelistElem{dev_id, addressAndType, ctype} );
- whitelist.push_back(wle);
+ whitelist.push_back( std::make_shared<WhitelistElem>(dev_id, addressAndType, ctype) );
return true;
}
}
diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp
index 8e99fe6f..f3f356a6 100644
--- a/src/direct_bt/GATTHandler.cpp
+++ b/src/direct_bt/GATTHandler.cpp
@@ -220,7 +220,7 @@ void GATTHandler::l2capReaderThreadImpl() {
GATTCharacteristicRef decl = findCharacterisicsByValueHandle(a->getHandle());
const TOctetSlice& a_value_view = a->getValue();
const TROOctets data_view(a_value_view.get_ptr_nc(0), a_value_view.getSize()); // just a view, still owned by attPDU
- // const std::shared_ptr<TROOctets> data(new POctets(a->getValue()));
+ // const std::shared_ptr<TROOctets> data( std::make_shared<POctets>(a->getValue()) );
const uint64_t timestamp = a->ts_creation;
int i=0;
jau::for_each_cow(characteristicListenerList, [&](std::shared_ptr<GATTCharacteristicListener> &l) {
@@ -248,7 +248,7 @@ void GATTHandler::l2capReaderThreadImpl() {
GATTCharacteristicRef decl = findCharacterisicsByValueHandle(a->getHandle());
const TOctetSlice& a_value_view = a->getValue();
const TROOctets data_view(a_value_view.get_ptr_nc(0), a_value_view.getSize()); // just a view, still owned by attPDU
- // const std::shared_ptr<TROOctets> data(new POctets(a->getValue()));
+ // const std::shared_ptr<TROOctets> data( std::make_shared<POctets>(a->getValue()) );
const uint64_t timestamp = a->ts_creation;
int i=0;
jau::for_each_cow(characteristicListenerList, [&](std::shared_ptr<GATTCharacteristicListener> &l) {
@@ -708,7 +708,7 @@ bool GATTHandler::discoverDescriptors(GATTServiceRef & service) {
const uint16_t cd_handle = p->getElementHandle(e_iter);
std::unique_ptr<const uuid_t> cd_uuid = p->getElementValue(e_iter);
- std::shared_ptr<GATTDescriptor> cd( new GATTDescriptor(charDecl, std::move(cd_uuid), cd_handle) );
+ std::shared_ptr<GATTDescriptor> cd( std::make_shared<GATTDescriptor>(charDecl, std::move(cd_uuid), cd_handle) );
if( cd_handle <= charDecl->value_handle || cd_handle > cd_handle_end ) { // should never happen!
ERR_PRINT("GATT discoverDescriptors CD handle %s not in range ]%s..%s]: descr%s within char%s on %s",
jau::uint16HexString(cd_handle).c_str(),
@@ -988,7 +988,7 @@ std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(std::vector<
}
}
if( deviceName.size() > 0 ) {
- res = std::shared_ptr<GattGenericAccessSvc>(new GattGenericAccessSvc(deviceName, appearance, prefConnParam));
+ res = std::make_shared<GattGenericAccessSvc>(deviceName, appearance, prefConnParam);
}
return res;
}
@@ -1096,9 +1096,9 @@ std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(std:
}
}
if( found ) {
- res = std::shared_ptr<GattDeviceInformationSvc>(new GattDeviceInformationSvc(systemID, modelNumber, serialNumber,
- firmwareRevision, hardwareRevision, softwareRevision,
- manufacturer, regulatoryCertDataList, pnpID) );
+ res = std::make_shared<GattDeviceInformationSvc>(systemID, modelNumber, serialNumber,
+ firmwareRevision, hardwareRevision, softwareRevision,
+ manufacturer, regulatoryCertDataList, pnpID);
}
return res;
}
diff --git a/src/direct_bt/GATTNumbers.cpp b/src/direct_bt/GATTNumbers.cpp
index 80fba5da..b34e3514 100644
--- a/src/direct_bt/GATTNumbers.cpp
+++ b/src/direct_bt/GATTNumbers.cpp
@@ -369,7 +369,7 @@ std::shared_ptr<GattPeriphalPreferredConnectionParameters> GattPeriphalPreferred
ERR_PRINT("GattPeriphalPreferredConnectionParameters: Insufficient data, less than %d bytes in %s", reqSize, source.toString().c_str());
return nullptr;
}
- return std::shared_ptr<GattPeriphalPreferredConnectionParameters>(new GattPeriphalPreferredConnectionParameters(source));
+ return std::make_shared<GattPeriphalPreferredConnectionParameters>(source);
}
std::string GattPeriphalPreferredConnectionParameters::toString() const noexcept {
@@ -394,7 +394,7 @@ std::shared_ptr<GattPnP_ID> GattPnP_ID::get(const TROOctets &source) noexcept {
ERR_PRINT("GattPnP_ID: Insufficient data, less than %d bytes in %s", reqSize, source.toString().c_str());
return nullptr;
}
- return std::shared_ptr<GattPnP_ID>(new GattPnP_ID(source));
+ return std::make_shared<GattPnP_ID>(source);
}
std::string GattPnP_ID::toString() const noexcept {
@@ -447,7 +447,7 @@ std::shared_ptr<GattTemperatureMeasurement> GattTemperatureMeasurement::get(cons
if( hasTemperatureType ) {
temperature_type = source.get_uint8(1+4+7);
}
- return std::shared_ptr<GattTemperatureMeasurement>(new GattTemperatureMeasurement(flags, temperatureValue, timestamp, temperature_type));
+ return std::make_shared<GattTemperatureMeasurement>(flags, temperatureValue, timestamp, temperature_type);
}
std::string GattTemperatureMeasurement::toString() const noexcept {
diff --git a/src/direct_bt/MgmtTypes.cpp b/src/direct_bt/MgmtTypes.cpp
index d78130e5..a8f88535 100644
--- a/src/direct_bt/MgmtTypes.cpp
+++ b/src/direct_bt/MgmtTypes.cpp
@@ -453,7 +453,7 @@ std::shared_ptr<ConnectionInfo> MgmtEvtCmdComplete::toConnectionInfo() const noe
int8_t rssi = jau::get_int8(data, 7);
int8_t tx_power = jau::get_int8(data, 8);
int8_t max_tx_power = jau::get_int8(data, 9);
- return std::shared_ptr<ConnectionInfo>(new ConnectionInfo(address, addressType, rssi, tx_power, max_tx_power) );
+ return std::make_shared<ConnectionInfo>(address, addressType, rssi, tx_power, max_tx_power);
}
std::shared_ptr<NameAndShortName> MgmtEvtCmdComplete::toNameAndShortName() const noexcept {
@@ -476,19 +476,19 @@ std::shared_ptr<NameAndShortName> MgmtEvtCmdComplete::toNameAndShortName() const
std::string name = std::string( (const char*) ( data ) );
std::string short_name = std::string( (const char*) ( data + MgmtConstU16::MGMT_MAX_NAME_LENGTH ) );
- return std::shared_ptr<NameAndShortName>(new NameAndShortName(name, short_name) );
+ return std::make_shared<NameAndShortName>(name, short_name);
}
std::shared_ptr<NameAndShortName> MgmtEvtLocalNameChanged::toNameAndShortName() const noexcept {
- return std::shared_ptr<NameAndShortName>(new NameAndShortName(getName(), getShortName()) );
+ return std::make_shared<NameAndShortName>(getName(), getShortName());
}
std::shared_ptr<AdapterInfo> MgmtEvtAdapterInfo::toAdapterInfo() const noexcept {
- return std::shared_ptr<AdapterInfo>(new AdapterInfo(
+ return std::make_shared<AdapterInfo>(
getDevID(), getAddress(), getVersion(),
getManufacturer(), getSupportedSetting(),
getCurrentSetting(), getDevClass(),
- getName(), getShortName()) );
+ getName(), getShortName());
}
std::string MgmtEvtDeviceDisconnected::getDisconnectReasonString(DisconnectReason mgmtReason) noexcept {