[heap] fix external heap configuration (#6123)

The Instance::HeapCAlloc() method can not be used in constructors when
EXTERNAL_HEAP is enabled but works well when internal heap is
used. This is because the external heap methods are set by public
otHeapSetCAllocFree API after the OT instance is constructed.

This commit fixes this issue by having Instance::HeapCAlloc() using
otPlatCAlloc when EXTERNAL_HEAP is enabled. It also fixes build issues
when MULTIPLE_INSTANCE and EXTERNAL_HEAP are used together.
This commit is contained in:
kangping
2021-02-05 10:15:13 -08:00
committed by GitHub
parent 099dfa2731
commit 31746000e8
22 changed files with 137 additions and 177 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ void __gcov_flush();
#define OPENTHREAD_ENABLE_COVERAGE 0
#endif
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
void *otPlatCAlloc(size_t aNum, size_t aSize)
{
return calloc(aNum, aSize);
+1 -1
View File
@@ -49,7 +49,7 @@ void __gcov_flush();
#define OPENTHREAD_ENABLE_COVERAGE 0
#endif
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
void *otPlatCAlloc(size_t aNum, size_t aSize)
{
return calloc(aNum, aSize);
-3
View File
@@ -170,9 +170,6 @@ void otSysInit(int argc, char *argv[])
__disable_irq();
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otHeapSetCAllocFree(sl_calloc, sl_free);
#endif
#undef FIXED_EXCEPTION
#define FIXED_EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler)
#define EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler, priorityLevel, subpriority) \
-33
View File
@@ -51,27 +51,6 @@ extern "C" {
*
*/
/**
* Function pointer used to set external CAlloc function for OpenThread.
*
* @param[in] aCount Number of allocate units.
* @param[in] aSize Unit size in bytes.
*
* @returns A pointer to the allocated memory.
*
* @retval NULL Indicates not enough memory.
*
*/
typedef void *(*otHeapCAllocFn)(size_t aCount, size_t aSize);
/**
* Function pointer used to set external Free function for OpenThread.
*
* @param[in] aPointer A pointer to the memory to free.
*
*/
typedef void (*otHeapFreeFn)(void *aPointer);
// This is a temporary API and would be removed after moving heap to platform.
// TODO: Remove after moving heap to platform.
/**
@@ -88,18 +67,6 @@ void *otHeapCAlloc(size_t aCount, size_t aSize);
*/
void otHeapFree(void *aPointer);
/**
* This function sets the external heap CAlloc and Free
* functions to be used by the OpenThread stack.
*
* This function must be used before invoking instance initialization.
*
* @param[in] aCAlloc A pointer to external CAlloc function.
* @param[in] aFree A pointer to external Free function.
*
*/
void otHeapSetCAllocFree(otHeapCAllocFn aCAlloc, otHeapFreeFn aFree);
/**
* @}
*
+2 -2
View File
@@ -61,7 +61,7 @@ extern "C" {
* memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes
* of value zero."
*
* This function is available and can ONLY be used only when support for multiple OpenThread instances is enabled.
* This function is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
*
* @param[in] aNum The number of blocks to allocate
* @param[in] aSize The size of each block to allocate
@@ -74,7 +74,7 @@ void *otPlatCAlloc(size_t aNum, size_t aSize);
/**
* Frees memory that was dynamically allocated.
*
* This function is available and can ONLY be used only when support for multiple OpenThread instances is enabled.
* This function is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
*
* @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL.
*/
+3 -16
View File
@@ -37,8 +37,6 @@
#include "common/instance.hpp"
#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE || OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#if OPENTHREAD_RADIO
void *otHeapCAlloc(size_t aCount, size_t aSize)
@@ -61,25 +59,14 @@ void otHeapFree(void *aPointer)
OT_ASSERT(false);
}
#else // OPENTHREAD_RADIO
#else // OPENTHREAD_RADIO
void *otHeapCAlloc(size_t aCount, size_t aSize)
{
return ot::Instance::Get().HeapCAlloc(aCount, aSize);
return ot::Instance::HeapCAlloc(aCount, aSize);
}
void otHeapFree(void *aPointer)
{
ot::Instance::Get().HeapFree(aPointer);
ot::Instance::HeapFree(aPointer);
}
#endif // OPENTHREAD_RADIO
#endif // !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE || OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE && !OPENTHREAD_RADIO
void otHeapSetCAllocFree(otHeapCAllocFn aCAlloc, otHeapFreeFn aFree)
{
ot::Instance::HeapSetCAllocFree(aCAlloc, aFree);
}
#endif // OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
+5 -9
View File
@@ -37,6 +37,7 @@
#include "common/logging.hpp"
#include "common/new.hpp"
#include "utils/heap.hpp"
namespace ot {
@@ -48,15 +49,10 @@ OT_DEFINE_ALIGNED_VAR(gInstanceRaw, sizeof(Instance), uint64_t);
#endif
#if OPENTHREAD_MTD || OPENTHREAD_FTD
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otHeapFreeFn ot::Instance::mFree = nullptr;
otHeapCAllocFn ot::Instance::mCAlloc = nullptr;
#endif // OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#endif // OPENTHREAD_MTD || OPENTHREAD_FTD
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
Utils::Heap Instance::sHeap;
#endif
#endif
Instance::Instance(void)
: mTimerMilliScheduler(*this)
+12 -37
View File
@@ -42,7 +42,7 @@
#include <openthread/error.h>
#include <openthread/heap.h>
#include <openthread/platform/logging.h>
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#include <openthread/platform/memory.h>
#endif
@@ -60,18 +60,16 @@
#endif
#if OPENTHREAD_FTD || OPENTHREAD_MTD
#include "common/code_utils.hpp"
#include "crypto/mbedtls.hpp"
#include "thread/tmf.hpp"
#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#include "utils/heap.hpp"
#endif
#include "common/notifier.hpp"
#include "common/settings.hpp"
#include "crypto/mbedtls.hpp"
#include "meshcop/border_agent.hpp"
#include "net/ip6.hpp"
#include "thread/announce_sender.hpp"
#include "thread/link_quality.hpp"
#include "thread/thread_netif.hpp"
#include "thread/tmf.hpp"
#include "utils/heap.hpp"
#if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD
#include "utils/channel_manager.hpp"
#endif
@@ -247,28 +245,11 @@ public:
otError ErasePersistentInfo(void);
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
void HeapFree(void *aPointer)
{
OT_ASSERT(mFree != nullptr);
mFree(aPointer);
}
void *HeapCAlloc(size_t aCount, size_t aSize)
{
OT_ASSERT(mCAlloc != nullptr);
return mCAlloc(aCount, aSize);
}
static void HeapSetCAllocFree(otHeapCAllocFn aCAlloc, otHeapFreeFn aFree)
{
mFree = aFree;
mCAlloc = aCAlloc;
}
#elif !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
void HeapFree(void *aPointer) { mHeap.Free(aPointer); }
void *HeapCAlloc(size_t aCount, size_t aSize) { return mHeap.CAlloc(aCount, aSize); }
static void HeapFree(void *aPointer) { otPlatFree(aPointer); }
static void *HeapCAlloc(size_t aCount, size_t aSize) { return otPlatCAlloc(aCount, aSize); }
#else
static void HeapFree(void *aPointer) { sHeap.Free(aPointer); }
static void *HeapCAlloc(size_t aCount, size_t aSize) { return sHeap.CAlloc(aCount, aSize); }
/**
* This method returns a reference to the Heap object.
@@ -276,10 +257,7 @@ public:
* @returns A reference to the Heap object.
*
*/
Utils::Heap &GetHeap(void) { return mHeap; }
#else
void HeapFree(void *aPointer) { otPlatFree(aPointer); }
void *HeapCAlloc(size_t aCount, size_t aSize) { return otPlatCAlloc(aCount, aSize); }
Utils::Heap &GetHeap(void) { return sHeap; }
#endif // OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#if OPENTHREAD_CONFIG_COAP_API_ENABLE
@@ -338,11 +316,8 @@ private:
#if OPENTHREAD_MTD || OPENTHREAD_FTD
// RandomManager is initialized before other objects. Note that it
// requires MbedTls which itself may use Heap.
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
static otHeapFreeFn mFree;
static otHeapCAllocFn mCAlloc;
#elif !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
Utils::Heap mHeap;
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
static Utils::Heap sHeap;
#endif
Crypto::MbedTls mMbedTls;
#endif // OPENTHREAD_MTD || OPENTHREAD_FTD
+2 -2
View File
@@ -111,7 +111,7 @@ Buffer *MessagePool::NewBuffer(Message::Priority aPriority)
while ((
#if OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE
buffer = static_cast<Buffer *>(GetInstance().HeapCAlloc(sizeof(Buffer), 1))
buffer = static_cast<Buffer *>(Instance::HeapCAlloc(1, sizeof(Buffer)))
#elif OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT
buffer = static_cast<Buffer *>(otPlatMessagePoolNew(&GetInstance()))
#else
@@ -143,7 +143,7 @@ void MessagePool::FreeBuffers(Buffer *aBuffer)
{
Buffer *next = aBuffer->GetNextBuffer();
#if OPENTHREAD_CONFIG_MESSAGE_USE_HEAP_ENABLE
GetInstance().HeapFree(aBuffer);
Instance::HeapFree(aBuffer);
#elif OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT
otPlatMessagePoolFree(&GetInstance(), aBuffer);
#else
+3 -17
View File
@@ -49,29 +49,15 @@
namespace ot {
namespace Crypto {
#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
static void *CAlloc(size_t aCount, size_t aSize)
{
return Instance::Get().HeapCAlloc(aCount, aSize);
}
static void Free(void *aPointer)
{
Instance::Get().HeapFree(aPointer);
}
#endif // !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
MbedTls::MbedTls(void)
{
#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
#if OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
#ifdef MBEDTLS_DEBUG_C
// mbedTLS's debug level is almost the same as OpenThread's
mbedtls_debug_set_threshold(OPENTHREAD_CONFIG_LOG_LEVEL);
#endif
mbedtls_platform_set_calloc_free(CAlloc, Free);
#endif // !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
mbedtls_platform_set_calloc_free(Instance::HeapCAlloc, Instance::HeapFree);
#endif // OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
}
otError MbedTls::MapError(int aMbedTlsError)
+3 -3
View File
@@ -209,7 +209,7 @@ exit:
SendErrorMessage(aForwardContext, error);
}
GetInstance().HeapFree(&aForwardContext);
Instance::HeapFree(&aForwardContext);
}
template <Coap::Resource BorderAgent::*aResource>
@@ -494,7 +494,7 @@ otError BorderAgent::ForwardToLeader(const Coap::Message & aMessage,
SuccessOrExit(error = Get<Coap::CoapSecure>().SendAck(aMessage, aMessageInfo));
}
forwardContext = static_cast<ForwardContext *>(GetInstance().HeapCAlloc(1, sizeof(ForwardContext)));
forwardContext = static_cast<ForwardContext *>(Instance::HeapCAlloc(1, sizeof(ForwardContext)));
VerifyOrExit(forwardContext != nullptr, error = OT_ERROR_NO_BUFS);
forwardContext->Init(GetInstance(), aMessage, aPetition, aSeparate);
@@ -530,7 +530,7 @@ exit:
{
if (forwardContext != nullptr)
{
GetInstance().HeapFree(forwardContext);
Instance::HeapFree(forwardContext);
}
FreeMessage(message);
+32 -35
View File
@@ -42,7 +42,6 @@
#include "net/dns_headers.hpp"
#include "thread/network_data_service.hpp"
#include "thread/thread_netif.hpp"
#include "utils/heap.hpp"
namespace ot {
namespace Srp {
@@ -94,7 +93,7 @@ Server::Server(Instance &aInstance)
Server::~Server(void)
{
GetInstance().HeapFree(mDomain);
Instance::HeapFree(mDomain);
}
void Server::SetServiceHandler(otSrpServerServiceUpdateHandler aServiceHandler, void *aServiceHandlerContext)
@@ -182,7 +181,7 @@ otError Server::SetDomain(const char *aDomain)
appendTrailingDot = 1;
}
buf = static_cast<char *>(GetInstance().HeapCAlloc(1, length + appendTrailingDot + 1));
buf = static_cast<char *>(Instance::HeapCAlloc(1, length + appendTrailingDot + 1));
VerifyOrExit(buf != nullptr, error = OT_ERROR_NO_BUFS);
strcpy(buf, aDomain);
@@ -191,13 +190,13 @@ otError Server::SetDomain(const char *aDomain)
buf[length] = '.';
buf[length + 1] = '\0';
}
GetInstance().HeapFree(mDomain);
Instance::HeapFree(mDomain);
mDomain = buf;
exit:
if (error != OT_ERROR_NONE)
{
GetInstance().HeapFree(buf);
Instance::HeapFree(buf);
}
return error;
}
@@ -531,7 +530,7 @@ void Server::HandleDnsUpdate(Message & aMessage,
// Per 2.3.2 of SRP draft 6, no prerequisites should be included in a SRP update.
VerifyOrExit(aDnsHeader.GetPrerequisiteRecordCount() == 0, error = OT_ERROR_FAILED);
host = Host::New(GetInstance());
host = Host::New();
VerifyOrExit(host != nullptr, error = OT_ERROR_NO_BUFS);
SuccessOrExit(error = ProcessUpdateSection(*host, aMessage, aDnsHeader, zone, aOffset));
@@ -1261,15 +1260,15 @@ void Server::HandleOutstandingUpdatesTimer(void)
}
}
Server::Service *Server::Service::New(Instance &aInstance, const char *aFullName)
Server::Service *Server::Service::New(const char *aFullName)
{
void * buf;
Service *service = nullptr;
buf = aInstance.HeapCAlloc(1, sizeof(Service));
buf = Instance::HeapCAlloc(1, sizeof(Service));
VerifyOrExit(buf != nullptr);
service = new (buf) Service(aInstance);
service = new (buf) Service();
if (service->SetFullName(aFullName) != OT_ERROR_NONE)
{
service->Free();
@@ -1282,14 +1281,13 @@ exit:
void Server::Service::Free(void)
{
GetInstance().HeapFree(mFullName);
GetInstance().HeapFree(mTxtData);
GetInstance().HeapFree(this);
Instance::HeapFree(mFullName);
Instance::HeapFree(mTxtData);
Instance::HeapFree(this);
}
Server::Service::Service(Instance &aInstance)
: InstanceLocator(aInstance)
, mFullName(nullptr)
Server::Service::Service(void)
: mFullName(nullptr)
, mPriority(0)
, mWeight(0)
, mPort(0)
@@ -1306,12 +1304,12 @@ otError Server::Service::SetFullName(const char *aFullName)
OT_ASSERT(aFullName != nullptr);
otError error = OT_ERROR_NONE;
char * nameCopy = static_cast<char *>(GetInstance().HeapCAlloc(1, strlen(aFullName) + 1));
char * nameCopy = static_cast<char *>(Instance::HeapCAlloc(1, strlen(aFullName) + 1));
VerifyOrExit(nameCopy != nullptr, error = OT_ERROR_NO_BUFS);
strcpy(nameCopy, aFullName);
GetInstance().HeapFree(mFullName);
Instance::HeapFree(mFullName);
mFullName = nameCopy;
exit:
@@ -1341,12 +1339,12 @@ otError Server::Service::SetTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLe
otError error = OT_ERROR_NONE;
uint8_t *txtData;
txtData = static_cast<uint8_t *>(GetInstance().HeapCAlloc(1, aTxtDataLength));
txtData = static_cast<uint8_t *>(Instance::HeapCAlloc(1, aTxtDataLength));
VerifyOrExit(txtData != nullptr, error = OT_ERROR_NO_BUFS);
memcpy(txtData, aTxtData, aTxtDataLength);
GetInstance().HeapFree(mTxtData);
Instance::HeapFree(mTxtData);
mTxtData = txtData;
mTxtLength = aTxtDataLength;
@@ -1362,12 +1360,12 @@ otError Server::Service::SetTxtDataFromMessage(const Message &aMessage, uint16_t
otError error = OT_ERROR_NONE;
uint8_t *txtData;
txtData = static_cast<uint8_t *>(GetInstance().HeapCAlloc(1, aLength));
txtData = static_cast<uint8_t *>(Instance::HeapCAlloc(1, aLength));
VerifyOrExit(txtData != nullptr, error = OT_ERROR_NO_BUFS);
VerifyOrExit(aMessage.ReadBytes(aOffset, txtData, aLength) == aLength, error = OT_ERROR_PARSE);
VerifyOrExit(Dns::TxtRecord::VerifyTxtData(txtData, aLength), error = OT_ERROR_PARSE);
GetInstance().HeapFree(mTxtData);
Instance::HeapFree(mTxtData);
mTxtData = txtData;
mTxtLength = aLength;
@@ -1376,7 +1374,7 @@ otError Server::Service::SetTxtDataFromMessage(const Message &aMessage, uint16_t
exit:
if (error != OT_ERROR_NONE)
{
GetInstance().HeapFree(txtData);
Instance::HeapFree(txtData);
}
return error;
@@ -1385,7 +1383,7 @@ exit:
void Server::Service::ClearResources(void)
{
mPort = 0;
GetInstance().HeapFree(mTxtData);
Instance::HeapFree(mTxtData);
mTxtData = nullptr;
mTxtLength = 0;
}
@@ -1418,15 +1416,15 @@ bool Server::Service::Matches(const char *aFullName) const
return (mFullName != nullptr) && (strcmp(mFullName, aFullName) == 0);
}
Server::Host *Server::Host::New(Instance &aInstance)
Server::Host *Server::Host::New(void)
{
void *buf;
Host *host = nullptr;
buf = aInstance.HeapCAlloc(1, sizeof(Host));
buf = Instance::HeapCAlloc(1, sizeof(Host));
VerifyOrExit(buf != nullptr);
host = new (buf) Host(aInstance);
host = new (buf) Host();
exit:
return host;
@@ -1435,13 +1433,12 @@ exit:
void Server::Host::Free(void)
{
RemoveAndFreeAllServices();
GetInstance().HeapFree(mFullName);
GetInstance().HeapFree(this);
Instance::HeapFree(mFullName);
Instance::HeapFree(this);
}
Server::Host::Host(Instance &aInstance)
: InstanceLocator(aInstance)
, mFullName(nullptr)
Server::Host::Host(void)
: mFullName(nullptr)
, mAddressesNum(0)
, mNext(nullptr)
, mLease(0)
@@ -1456,14 +1453,14 @@ otError Server::Host::SetFullName(const char *aFullName)
OT_ASSERT(aFullName != nullptr);
otError error = OT_ERROR_NONE;
char * nameCopy = static_cast<char *>(GetInstance().HeapCAlloc(1, strlen(aFullName) + 1));
char * nameCopy = static_cast<char *>(Instance::HeapCAlloc(1, strlen(aFullName) + 1));
VerifyOrExit(nameCopy != nullptr, error = OT_ERROR_NO_BUFS);
strcpy(nameCopy, aFullName);
if (mFullName != nullptr)
{
GetInstance().HeapFree(mFullName);
Instance::HeapFree(mFullName);
}
mFullName = nameCopy;
@@ -1508,7 +1505,7 @@ Server::Service *Server::Host::AddService(const char *aFullName)
VerifyOrExit(service == nullptr);
service = Service::New(GetInstance(), aFullName);
service = Service::New(aFullName);
if (service != nullptr)
{
IgnoreError(mServices.Add(*service));
@@ -1626,7 +1623,7 @@ exit:
void Server::UpdateMetadata::Free(void)
{
GetInstance().HeapFree(this);
Instance::HeapFree(this);
}
Server::UpdateMetadata::UpdateMetadata(Instance & aInstance,
+9 -12
View File
@@ -80,7 +80,7 @@ public:
* This class implements a server-side SRP service.
*
*/
class Service : public InstanceLocator, public LinkedListEntry<Service>, private NonCopyable
class Service : public LinkedListEntry<Service>, private NonCopyable
{
friend class LinkedListEntry<Service>;
friend class Server;
@@ -89,14 +89,13 @@ public:
/**
* This method creates a new Service object with given full name.
*
* @param[in] aInstance A reference to the OpenThread instance.
* @param[in] aFullName The full name of the service instance.
*
* @returns A pointer to the newly created Service object. nullptr if
* @returns A pointer to the newly created Service object, nullptr if
* cannot allocate memory for the object.
*
*/
static Service *New(Instance &aInstance, const char *aFullName);
static Service *New(const char *aFullName);
/**
* This method frees the Service object.
@@ -198,7 +197,7 @@ public:
bool Matches(const char *aFullName) const;
private:
explicit Service(Instance &aInstance);
explicit Service(void);
otError SetFullName(const char *aFullName);
otError SetTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength);
otError SetTxtDataFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
@@ -222,22 +221,20 @@ public:
* This class implements the Host which registers services on the SRP server.
*
*/
class Host : public InstanceLocator, public LinkedListEntry<Host>, private NonCopyable
class Host : public LinkedListEntry<Host>, private NonCopyable
{
friend class LinkedListEntry<Host>;
friend class Server;
public:
/**
* This method creates a new Host object with given full name.
* This method creates a new Host object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
* @returns A pointer to the newly created Host object. nullptr if
* @returns A pointer to the newly created Host object, nullptr if
* cannot allocate memory for the object.
*
*/
static Host *New(Instance &aInstance);
static Host *New(void);
/**
* This method Frees the Host object.
@@ -348,7 +345,7 @@ public:
kMaxAddressesNum = OPENTHREAD_CONFIG_SRP_SERVER_MAX_ADDRESSES_NUM,
};
explicit Host(Instance &aInstance);
explicit Host(void);
otError SetFullName(const char *aFullName);
void SetKey(Dns::Ecdsa256KeyRecord &aKey);
void SetLease(uint32_t aLease);
+4
View File
@@ -34,6 +34,8 @@
#include "heap.hpp"
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#include <string.h>
#include "common/code_utils.hpp"
@@ -217,3 +219,5 @@ void Heap::Free(void *aPointer)
} // namespace Utils
} // namespace ot
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
+4
View File
@@ -37,6 +37,8 @@
#include "openthread-core-config.h"
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#include <stddef.h>
#include <stdint.h>
@@ -353,4 +355,6 @@ private:
} // namespace Utils
} // namespace ot
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#endif // OT_HEAP_HPP_
+1
View File
@@ -66,6 +66,7 @@ add_library(openthread-posix
hdlc_interface.cpp
infra_if.cpp
logging.cpp
memory.cpp
misc.cpp
multicast_routing.cpp
netif.cpp
+1
View File
@@ -50,6 +50,7 @@ libopenthread_posix_a_SOURCES = \
hdlc_interface.cpp \
infra_if.cpp \
logging.cpp \
memory.cpp \
misc.cpp \
multicast_routing.cpp \
netif.cpp \
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "openthread-posix-config.h"
#include "platform-posix.h"
#include <stdlib.h>
#include <openthread/platform/memory.h>
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
void *otPlatCAlloc(size_t aNum, size_t aSize)
{
return calloc(aNum, aSize);
}
void otPlatFree(void *aPtr)
{
free(aPtr);
}
#endif
-4
View File
@@ -92,10 +92,6 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig)
#endif
platformRandomInit();
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otHeapSetCAllocFree(calloc, free);
#endif
instance = otInstanceInitSingle();
assert(instance != nullptr);
+6
View File
@@ -38,6 +38,8 @@
#include "test_platform.h"
#include "test_util.h"
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
/**
* Verifies single variable allocating and freeing.
*
@@ -171,9 +173,13 @@ void RunTimerTests(void)
TestAllocateMultiple();
}
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
int main(void)
{
#if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
RunTimerTests();
printf("All tests passed\n");
#endif // !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
return 0;
}
+1 -1
View File
@@ -94,7 +94,7 @@ bool sDiagMode = false;
extern "C" {
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
void *otPlatCAlloc(size_t aNum, size_t aSize)
{
return calloc(aNum, aSize);
+1 -1
View File
@@ -117,7 +117,7 @@
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */
#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCAlloc /**< Default allocator to use, can be undefined */
#define MBEDTLS_PLATFORM_STD_FREE otPlatFree /**< Default free to use, can be undefined */
#else