Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Remove explicit malloc()-family calls from first-party code. #3176

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/lighting-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <mbedtls/threading.h>

#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPPlatformMemory.h>

#include <AppTask.h>

Expand Down Expand Up @@ -106,7 +107,7 @@ int main(void)
#endif
#endif

mbedtls_platform_set_calloc_free(calloc, free);
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);

// Initialize mbedtls threading support on EFR32
THREADING_setup();
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/nrf5/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ int main(void)
// Activate deep sleep mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

#if CHIP_CONFIG_MEMORY_MGMT_MALLOC && __GNU_LIBRARY__
{
struct mallinfo minfo = mallinfo();
NRF_LOG_INFO("System Heap Utilization: heap size %" PRId32 ", arena size %" PRId32 ", in use %" PRId32 ", free %" PRId32,
GetHeapTotalSize(), minfo.arena, minfo.uordblks, minfo.fordblks);
}
#endif // CHIP_CONFIG_MEMORY_MGMT_MALLOC && __GNU_LIBRARY__

NRF_LOG_INFO("Starting FreeRTOS scheduler");

Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <mbedtls/threading.h>

#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPPlatformMemory.h>

#include <AppTask.h>

Expand Down Expand Up @@ -106,7 +107,7 @@ int main(void)
#endif
#endif

mbedtls_platform_set_calloc_free(calloc, free);
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);

// Initialize mbedtls threading support on EFR32
THREADING_setup();
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/nrf5/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ int main(void)
// Activate deep sleep mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

#if CHIP_CONFIG_MEMORY_MGMT_MALLOC && __GNU_LIBRARY__
{
struct mallinfo minfo = mallinfo();
NRF_LOG_INFO("System Heap Utilization: heap size %" PRId32 ", arena size %" PRId32 ", in use %" PRId32 ", free %" PRId32,
GetHeapTotalSize(), minfo.arena, minfo.uordblks, minfo.fordblks);
}
#endif // CHIP_CONFIG_MEMORY_MGMT_MALLOC && __GNU_LIBRARY__

NRF_LOG_INFO("Starting FreeRTOS scheduler");

Expand Down
4 changes: 3 additions & 1 deletion examples/platform/efr32/init_otSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* limitations under the License.
*/

#include <support/CHIPPlatformMemory.h>

#include <mbedtls/platform.h>
#include <openthread/cli.h>
#include <openthread/dataset.h>
Expand Down Expand Up @@ -119,5 +121,5 @@ void initOtSysEFR(void)
#ifndef EFR32MG21
efr32RandomInit();
#endif /* EFR32 PLATFORM */
otHeapSetCAllocFree(calloc, free);
otHeapSetCAllocFree(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
}
1 change: 1 addition & 0 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "InetLayer.h"
#include "InetLayerEvents.h"

#include <support/CHIPMemString.h>
#include <support/CodeUtils.h>
#include <support/DLLUtil.h>

Expand Down
4 changes: 4 additions & 0 deletions src/inet/tests/TapAddrAutoconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

int CollectTapAddresses(std::vector<char *> & addresses, const char * ifName)
{
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
struct ifaddrs *addrsList, *curAddr;
const int err = getifaddrs(&addrsList);

Expand Down Expand Up @@ -71,4 +72,7 @@ int CollectTapAddresses(std::vector<char *> & addresses, const char * ifName)
}

return 0;
#else
#error "Unsupported configuration: requires CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS"
#endif
}
10 changes: 8 additions & 2 deletions src/inet/tests/TestInetCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include <inet/InetFaultInjection.h>
#include <support/CHIPFaultInjection.h>
#include <support/CHIPMem.h>
#include <system/SystemFaultInjection.h>
#include <system/SystemTimer.h>

Expand Down Expand Up @@ -154,7 +155,12 @@ static void UseStdoutLineBuffering()
{
// Set stdout to be line buffered with a buffer of 512 (will flush on new line
// or when the buffer of 512 is exceeded).
setvbuf(stdout, nullptr, _IOLBF, 512);
#if CHIP_CONFIG_MEMORY_MGMT_MALLOC
constexpr char * buf = nullptr;
#else
static char buf[512];
#endif // CHIP_CONFIG_MEMORY_MGMT_MALLOC
setvbuf(stdout, buf, _IOLBF, 512);
}

void InitTestInetCommon()
Expand Down Expand Up @@ -282,7 +288,7 @@ void InitNetwork()
for (size_t j = 0; j < gNetworkOptions.LocalIPv6Addr.size(); j++)
{
uint64_t iid = gNetworkOptions.LocalIPv6Addr[j].InterfaceId();
char * tap_name = (char *) malloc(sizeof(gDefaultTapDeviceName));
char * tap_name = (char *) chip::Platform::MemoryAlloc(sizeof(gDefaultTapDeviceName));
snprintf(tap_name, sizeof(gDefaultTapDeviceName), "chip-dev-%" PRIx64, iid & 0xFFFF);
tap_name[sizeof(gDefaultTapDeviceName) - 1] = 0;
gNetworkOptions.TapDeviceName.push_back(tap_name);
Expand Down
6 changes: 4 additions & 2 deletions src/inet/tests/TestInetCommonOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

#include <inet/InetFaultInjection.h>
#include <support/CHIPFaultInjection.h>
#include <support/CHIPMem.h>
#include <support/CHIPMemString.h>
#include <system/SystemFaultInjection.h>

using namespace chip;
Expand Down Expand Up @@ -256,9 +258,9 @@ bool FaultInjectionOptions::HandleOption(const char * progName, OptionSet * optS
{
#if CHIP_CONFIG_TEST || CHIP_SYSTEM_CONFIG_TEST || INET_CONFIG_TEST
case kToolCommonOpt_FaultInjection: {
char * mutableArg = strdup(arg);
char * mutableArg = chip::Platform::MemoryAllocString(arg);
bool parseRes = ParseFaultInjectionStr(mutableArg, faultMgrFnTable, faultMgrFnTableLen);
free(mutableArg);
chip::Platform::MemoryFree(mutableArg);
if (!parseRes)
{
PrintArgError("%s: Invalid string specified for fault injection option: %s\n", progName, arg);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ source_set("chip_config_header") {
"${chip_root}/src/ble:ble_config_header",
"${chip_root}/src/system:system_config_header",
]

allow_circular_includes_from = [ "${chip_root}/src/ble:ble_config_header" ]
}

static_library("core") {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@
#error "Please assert exactly one of CHIP_CONFIG_MEMORY_MGMT_PLATFORM, CHIP_CONFIG_MEMORY_MGMT_SIMPLE, or CHIP_CONFIG_MEMORY_MGMT_MALLOC."
#endif // ((CHIP_CONFIG_MEMORY_MGMT_PLATFORM + CHIP_CONFIG_MEMORY_MGMT_SIMPLE + CHIP_CONFIG_MEMORY_MGMT_MALLOC) != 1)

#if !CHIP_CONFIG_MEMORY_MGMT_MALLOC && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#error "!CHIP_CONFIG_MEMORY_MGMT_MALLOC but getifaddrs() uses malloc()"
#endif

/**
* @def CHIP_CONFIG_SIMPLE_ALLOCATOR_USE_SMALL_BUFFERS
*
Expand Down
27 changes: 9 additions & 18 deletions src/lib/core/CHIPTLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <core/CHIPCore.h>
#include <core/CHIPEncoding.h>
#include <core/CHIPTLV.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <support/SafeInt.h>
#include <system/SystemPacketBuffer.h>
Expand Down Expand Up @@ -661,7 +662,8 @@ CHIP_ERROR TLVReader::GetString(char * buf, uint32_t bufSize)
*
* This method creates a buffer for and returns a copy of the data associated with the byte
* or UTF-8 string element at the current position. Memory for the buffer is obtained with
* malloc() and should be freed with free() by the caller when it is no longer needed.
* Platform::MemoryAlloc() and should be freed with Platform::MemoryFree() by the caller when
* it is no longer needed.
*
* @note The data returned by this method is NOT null-terminated.
*
Expand All @@ -675,37 +677,31 @@ CHIP_ERROR TLVReader::GetString(char * buf, uint32_t bufSize)
* the reader is not positioned on an element.
* @retval #CHIP_ERROR_NO_MEMORY If memory could not be allocated for the output buffer.
* @retval #CHIP_ERROR_TLV_UNDERRUN If the underlying TLV encoding ended prematurely.
* @retval #CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE
* If the target platform does not support malloc() and free().
* @retval other Other CHIP or platform error codes returned by the configured
* GetNextBuffer() function. Only possible when GetNextBuffer
* is non-NULL.
*
*/
CHIP_ERROR TLVReader::DupBytes(uint8_t *& buf, uint32_t & dataLen)
{
#if HAVE_MALLOC && HAVE_FREE
if (!TLVTypeIsString(ElementType()))
return CHIP_ERROR_WRONG_TLV_TYPE;

buf = static_cast<uint8_t *>(malloc(mElemLenOrVal));
buf = static_cast<uint8_t *>(chip::Platform::MemoryAlloc(mElemLenOrVal));
if (buf == nullptr)
return CHIP_ERROR_NO_MEMORY;

CHIP_ERROR err = ReadData(buf, static_cast<uint32_t>(mElemLenOrVal));
if (err != CHIP_NO_ERROR)
{
free(buf);
chip::Platform::MemoryFree(buf);
return err;
}

dataLen = mElemLenOrVal;
mElemLenOrVal = 0;

return CHIP_NO_ERROR;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif // HAVE_MALLOC && HAVE_FREE
}

/**
Expand All @@ -714,7 +710,8 @@ CHIP_ERROR TLVReader::DupBytes(uint8_t *& buf, uint32_t & dataLen)
*
* This method creates a buffer for and returns a null-terminated copy of the data associated with
* the byte or UTF-8 string element at the current position. Memory for the buffer is obtained with
* malloc() and should be freed with free() by the caller when it is no longer needed.
* Platform::MemoryAlloc() and should be freed with chip::Platform::MemoryFree() by the caller when
* it is no longer needed.
*
* @param[out] buf A reference to a pointer to which a heap-allocated buffer of
* will be assigned on success.
Expand All @@ -724,37 +721,31 @@ CHIP_ERROR TLVReader::DupBytes(uint8_t *& buf, uint32_t & dataLen)
* the reader is not positioned on an element.
* @retval #CHIP_ERROR_NO_MEMORY If memory could not be allocated for the output buffer.
* @retval #CHIP_ERROR_TLV_UNDERRUN If the underlying TLV encoding ended prematurely.
* @retval #CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE
* If the target platform does not support malloc() and free().
* @retval other Other CHIP or platform error codes returned by the configured
* GetNextBuffer() function. Only possible when GetNextBuffer
* is non-NULL.
*
*/
CHIP_ERROR TLVReader::DupString(char *& buf)
{
#if HAVE_MALLOC && HAVE_FREE
if (!TLVTypeIsString(ElementType()))
return CHIP_ERROR_WRONG_TLV_TYPE;

buf = static_cast<char *>(malloc(mElemLenOrVal + 1));
buf = static_cast<char *>(chip::Platform::MemoryAlloc(mElemLenOrVal + 1));
if (buf == nullptr)
return CHIP_ERROR_NO_MEMORY;

CHIP_ERROR err = ReadData(reinterpret_cast<uint8_t *>(buf), static_cast<uint32_t>(mElemLenOrVal));
if (err != CHIP_NO_ERROR)
{
free(buf);
chip::Platform::MemoryFree(buf);
return err;
}

buf[mElemLenOrVal] = 0;
mElemLenOrVal = 0;

return err;
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif // HAVE_MALLOC && HAVE_FREE
}

/**
Expand Down
41 changes: 12 additions & 29 deletions src/lib/core/CHIPTLVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <core/CHIPCore.h>
#include <core/CHIPEncoding.h>

#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <system/SystemPacketBuffer.h>

Expand Down Expand Up @@ -745,11 +746,8 @@ CHIP_ERROR TLVWriter::PutString(uint64_t tag, const char * buf, uint32_t len)
* use the least amount of code, the `vsprintf_ex` variety of
* functions will consume less stack.
*
* If neither of the above is available, but platform provides
* `malloc` the function will allocate a temporary buffer to hold the
* output. When the platform supplies neither enhancement to the
* printf family nor malloc, the output is truncated such that it fits
* in the continuous state in the current TLV storage
* If neither of the above is available, the function will allocate a
* temporary buffer to hold the output, using Platform::MemoryAlloc().
*
* @param[in] tag The TLV tag to be encoded with the value, or @p
* AnonymousTag if the value should be encoded without
Expand Down Expand Up @@ -818,11 +816,8 @@ void TLVWriter::CHIPTLVWriterPutcharCB(uint8_t c, void * appState)
* use the least amount of code, the `vsprintf_ex` variety of
* functions will consume less stack.
*
* If neither of the above is available, but platform provides
* `malloc` the function will allocate a temporary buffer to hold the
* output. When the platform supplies neither enhancement to the
* printf family nor malloc, the output is truncated such that it fits
* in the continuous state in the current TLV storage
* If neither of the above is available, the function will allocate a
* temporary buffer to hold the output, using Platform::MemoryAlloc().
*
* @param[in] tag The TLV tag to be encoded with the value, or @p
* AnonymousTag if the value should be encoded without
Expand Down Expand Up @@ -853,10 +848,10 @@ CHIP_ERROR TLVWriter::VPutStringF(uint64_t tag, const char * fmt, va_list ap)
size_t skipLen;
size_t writtenBytes;
#elif CONFIG_HAVE_VCBPRINTF
#elif HAVE_MALLOC
char * tmpBuf;
#else
#elif CONFIG_TLV_TRUNCATE
size_t maxLen;
#else
char * tmpBuf;
#endif
va_copy(aq, ap);

Expand All @@ -871,7 +866,7 @@ CHIP_ERROR TLVWriter::VPutStringF(uint64_t tag, const char * fmt, va_list ap)
else
lenFieldSize = kTLVFieldSize_4Byte;

#if !(CONFIG_HAVE_VCBPRINTF) && !(CONFIG_HAVE_VSNPRINTF_EX) && !(HAVE_MALLOC)
#if !(CONFIG_HAVE_VCBPRINTF) && !(CONFIG_HAVE_VSNPRINTF_EX) && CONFIG_TLV_TRUNCATE
// no facilities for splitting the stream across multiple buffers,
// just write however much fits in the current buffer.
// assume conservative tag length at this time (8 bytes)
Expand Down Expand Up @@ -932,9 +927,9 @@ CHIP_ERROR TLVWriter::VPutStringF(uint64_t tag, const char * fmt, va_list ap)

va_end(aq);

#elif HAVE_MALLOC
#else // CONFIG_HAVE_VSNPRINTF_EX

tmpBuf = static_cast<char *>(malloc(dataLen + 1));
tmpBuf = static_cast<char *>(chip::Platform::MemoryAlloc(dataLen + 1));
VerifyOrExit(tmpBuf != nullptr, err = CHIP_ERROR_NO_MEMORY);

va_copy(aq, ap);
Expand All @@ -944,19 +939,7 @@ CHIP_ERROR TLVWriter::VPutStringF(uint64_t tag, const char * fmt, va_list ap)
va_end(aq);

err = WriteData(reinterpret_cast<uint8_t *>(tmpBuf), dataLen);
free(tmpBuf);

#else // CONFIG_HAVE_VSNPRINTF_EX

va_copy(aq, ap);

vsnprintf(reinterpret_cast<char *>(mWritePoint), dataLen + 1, fmt, aq);

va_end(aq);

mWritePoint += dataLen;
mRemainingLen -= dataLen;
mLenWritten += dataLen;
chip::Platform::MemoryFree(tmpBuf);

#endif // CONFIG_HAVE_VSNPRINTF_EX

Expand Down
Loading