Platform Support for Dynamic Memory Allocation (#767)

* Add support for platform implementation of dynamic memory allocation.
This commit is contained in:
Nick Banks
2016-10-14 09:02:38 -07:00
committed by Jonathan Hui
parent ddd0704b76
commit 86deb6e868
13 changed files with 167 additions and 13 deletions
@@ -216,6 +216,12 @@
<ClCompile Include="..\..\src\core\utils\global_address.cpp">
<Filter>Source Files\utils</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\meshcop\announce_begin_client.cpp">
<Filter>Source Files\meshcop</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\thread\announce_begin_server.cpp">
<Filter>Source Files\thread</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\core\openthreadcontext.h">
@@ -407,5 +413,11 @@
<ClInclude Include="..\..\src\core\utils\global_address.hpp">
<Filter>Header Files\utils</Filter>
</ClInclude>
<ClInclude Include="..\..\src\core\meshcop\announce_begin_client.hpp">
<Filter>Header Files\meshcop</Filter>
</ClInclude>
<ClInclude Include="..\..\src\core\meshcop\announce_begin_server.hpp">
<Filter>Header Files\thread</Filter>
</ClInclude>
</ItemGroup>
</Project>
@@ -30,7 +30,6 @@
<ClCompile Include="..\..\src\core\crypto\aes_ccm.cpp" />
<ClCompile Include="..\..\src\core\crypto\aes_ecb.cpp" />
<ClCompile Include="..\..\src\core\crypto\hmac_sha256.cpp" />
<ClCompile Include="..\..\src\core\crypto\mbedtls.cpp" />
<ClCompile Include="..\..\src\core\crypto\sha256.cpp" />
<ClCompile Include="..\..\src\core\mac\mac.cpp" />
<ClCompile Include="..\..\src\core\mac\mac_blacklist.cpp" />
@@ -207,15 +207,18 @@
<ClCompile Include="..\..\src\core\crypto\hmac_sha256.cpp">
<Filter>Source Files\crypto</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\crypto\mbedtls.cpp">
<Filter>Source Files\crypto</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\crypto\sha256.cpp">
<Filter>Source Files\crypto</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\utils\global_address.cpp">
<Filter>Source Files\utils</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\thread\announce_begin_server.cpp">
<Filter>Source Files\thread</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\meshcop\announce_begin_client.cpp">
<Filter>Source Files\meshcop</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\core\openthreadcontext.h">
@@ -407,5 +410,11 @@
<ClInclude Include="..\..\src\core\utils\global_address.hpp">
<Filter>Header Files\utils</Filter>
</ClInclude>
<ClInclude Include="..\..\src\core\meshcop\announce_begin_server.hpp">
<Filter>Header Files\thread</Filter>
</ClInclude>
<ClInclude Include="..\..\src\core\meshcop\announce_begin_client.hpp">
<Filter>Header Files\meshcop</Filter>
</ClInclude>
</ItemGroup>
</Project>
+4 -4
View File
@@ -106,7 +106,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\include;..\..\src\missing\stdint;..\..\src\missing\stdbool;..\..\src\core;..\..\third_party\mbedtls;..\..\third_party\mbedtls\repo\include;..\..\third_party\mbedtls\repo\include\mbedtls</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;OPENTHREAD_MULTIPLE_INSTANCE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DisableSpecificWarnings>4132;4242;4245;4748;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
@@ -115,7 +115,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\include;..\..\src\missing\stdint;..\..\src\missing\stdbool;..\..\src\core;..\..\third_party\mbedtls;..\..\third_party\mbedtls\repo\include;..\..\third_party\mbedtls\repo\include\mbedtls</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_WIN64;_AMD64_;AMD64;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN64;_AMD64_;AMD64;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;OPENTHREAD_MULTIPLE_INSTANCE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DisableSpecificWarnings>4132;4242;4245;4748;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
@@ -124,7 +124,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\include;..\..\src\missing\stdint;..\..\src\missing\stdbool;..\..\src\core;..\..\third_party\mbedtls;..\..\third_party\mbedtls\repo\include;..\..\third_party\mbedtls\repo\include\mbedtls</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;OPENTHREAD_MULTIPLE_INSTANCE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DisableSpecificWarnings>4132;4242;4245;4603;4627;4986;4987;4996;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
@@ -133,7 +133,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\include;..\..\src\missing\stdint;..\..\src\missing\stdbool;..\..\src\core;..\..\third_party\mbedtls;..\..\third_party\mbedtls\repo\include;..\..\third_party\mbedtls\repo\include\mbedtls</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_WIN64;_AMD64_;AMD64;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WIN64;_AMD64_;AMD64;MBEDTLS_CONFIG_FILE="mbedtls-config.h";WINDOWS_KERNEL;OPENTHREAD_MULTIPLE_INSTANCE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DisableSpecificWarnings>4132;4242;4245;4603;4627;4986;4987;4996;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<WarningLevel>Level4</WarningLevel>
<TreatWarningAsError>true</TreatWarningAsError>
+1
View File
@@ -31,6 +31,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
ot_platform_headers =\
alarm.h \
diag.h \
memory.h \
misc.h \
logging.h \
platform.h \
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2016, 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.
*/
/**
* @file
* @brief
* This file includes the platform abstraction for dynamic memory allocation.
*/
#ifndef MEMORY_H_
#define MEMORY_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup memory Memory
* @ingroup platform
*
* @brief
* This module includes the platform abstraction for dynamic memory allocation.
*
* @{
*
*/
// Currently, OpenThread only requires dynamic memory allocation when supporting multiple
// simultaneous instances, for MbedTls.
#ifdef OPENTHREAD_MULTIPLE_INSTANCE
/**
* Dynamically allocates new memory. On platforms that support it, should just redirect to calloc. For
* those that don't support calloc, should support the same functionality:
*
* "The calloc() function contiguously allocates enough space for count objects that are size bytes of
* memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes
* of value zero."
*
* @param[in] aNum The number of blocks to allocate
* @param[in] aSize The size of each block to allocate
*
* @retval void* The pointer to the front of the memory allocated
* @retval NULL Failed to allocate the memory requested.
*/
void *otPlatCAlloc(size_t aNum, size_t aSize);
/**
* Frees memory that was dynamically allocated.
*
* @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL.
*/
void otPlatFree(void *aPtr);
#endif
/**
* @}
*
*/
#ifdef __cplusplus
} // extern "C"
#endif
#endif // DEBUG_H_
+17
View File
@@ -134,6 +134,23 @@ extern "C" {
// =========== TOOLCHAIN SELECTION : END ===========
#ifdef _MSC_VER
#ifdef _WIN64
#define OT_CALL
#define OT_CDECL
#else
#define OT_CALL __stdcall
#define OT_CDECL __cdecl
#endif
#else
#define OT_CALL
#define OT_CDECL
#endif
/**
* @}
*
+3
View File
@@ -35,7 +35,10 @@
#define NEW_HPP_
#include <stddef.h>
#include <platform/toolchain.h>
inline void *operator new(size_t, void *p) throw() { return p; }
void OT_CDECL operator delete(void *, size_t) throw();
#endif // NEW_HPP_
+8
View File
@@ -59,6 +59,14 @@ Dtls::Dtls(ThreadNetif &aNetif):
mProvisioningUrl.Init();
}
Dtls::~Dtls(void)
{
if (mStarted)
{
Close();
}
}
ThreadError Dtls::Start(bool aClient, ReceiveHandler aReceiveHandler, SendHandler aSendHandler, void *aContext)
{
static const int ciphersuites[2] = {0xC0FF, 0}; // EC-JPAKE cipher suite
+6
View File
@@ -69,6 +69,12 @@ public:
*/
Dtls(ThreadNetif &aNetif);
/**
* This destructor cleans up the DTLS object.
*
*/
~Dtls(void);
/**
* This function pointer is called when data is received from the DTLS session.
*
+2
View File
@@ -71,7 +71,9 @@ typedef struct otInstance
// State
//
#ifndef OPENTHREAD_MULTIPLE_INSTANCE
Thread::Crypto::MbedTls mMbedTls;
#endif
Thread::Ip6::Ip6 mIp6;
Thread::ThreadNetif mThreadNetif;
+4 -3
View File
@@ -63,6 +63,8 @@ static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t);
otInstance *sInstance = NULL;
#endif
void OT_CDECL operator delete(void *, size_t) throw() { }
otInstance::otInstance(void) :
mReceiveIp6DatagramCallback(NULL),
mReceiveIp6DatagramCallbackContext(NULL),
@@ -70,8 +72,6 @@ otInstance::otInstance(void) :
mActiveScanCallbackContext(NULL),
mDiscoverCallback(NULL),
mDiscoverCallbackContext(NULL),
mMbedTls(),
mIp6(),
mThreadNetif(mIp6)
{
}
@@ -1051,7 +1051,8 @@ void otInstanceFinalize(otInstance *aInstance)
(void)otThreadStop(aInstance);
(void)otInterfaceDown(aInstance);
// Nothing to actually free, since the caller supplied the buffer
// Free the otInstance structure
delete aInstance;
#ifndef OPENTHREAD_MULTIPLE_INSTANCE
sInstance = NULL;
+7 -2
View File
@@ -31,6 +31,7 @@
#include <stdlib.h>
#include <platform/logging.h>
#include <platform/memory.h>
#if defined(_WIN32)
#include <stdarg.h>
@@ -1905,7 +1906,9 @@
*
* Enable this module to enable the buffer memory allocator.
*/
#ifndef OPENTHREAD_MULTIPLE_INSTANCE
#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
#endif
/**
* \def MBEDTLS_NET_C
@@ -2475,8 +2478,10 @@
/* Platform options */
//#define MBEDTLS_PLATFORM_STD_MEM_HDR <stdlib.h> /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */
//#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */
#ifdef OPENTHREAD_MULTIPLE_INSTANCE
#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 */
#endif
//#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */