Skip to content

Commit

Permalink
Revert "Make MemoryInit and MemoryShutdown Platform APIs threadsafe (#…
Browse files Browse the repository at this point in the history
…18854)" (#19143)

This reverts commit 19d826a.

The PR causes esp32 to boot-loop.

Fixes #19142
  • Loading branch information
bzbarsky-apple authored Jun 3, 2022
1 parent fd5e78d commit 59e4898
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
43 changes: 11 additions & 32 deletions src/lib/support/CHIPMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <lib/core/CHIPConfig.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPPlatformMemory.h>
#include <system/SystemMutex.h>

#include <atomic>
#include <stdlib.h>
Expand All @@ -37,51 +36,31 @@ namespace Platform {
extern CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize);
extern void MemoryAllocatorShutdown();

#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
static chip::System::Mutex gMemoryLock;
#endif
static int memoryInitializationCount{ 0 };
static std::atomic_int memoryInitializationCount{ 0 };

CHIP_ERROR MemoryInit(void * buf, size_t bufSize)
{
CHIP_ERROR err = CHIP_NO_ERROR;
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
gMemoryLock.Lock();
#endif
do
if (memoryInitializationCount++ > 0)
{
if (memoryInitializationCount++ > 0)
{
// Already initialized
break;
}
// Initialize the allocator.
err = MemoryAllocatorInit(buf, bufSize);
if (err != CHIP_NO_ERROR)
{
break;
}
// Here we do things like mbedtls_platform_set_calloc_free(), depending on configuration.
} while (0);
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
gMemoryLock.Unlock();
#endif
return CHIP_NO_ERROR;
}
// Initialize the allocator.
CHIP_ERROR err = MemoryAllocatorInit(buf, bufSize);
if (err != CHIP_NO_ERROR)
{
return err;
}
// Here we do things like mbedtls_platform_set_calloc_free(), depending on configuration.
return err;
}

void MemoryShutdown()
{
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
gMemoryLock.Lock();
#endif
if ((memoryInitializationCount > 0) && (--memoryInitializationCount == 0))
{
// Here we undo things like mbedtls_platform_set_calloc_free()
MemoryAllocatorShutdown();
}
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
gMemoryLock.Unlock();
#endif
}

} // namespace Platform
Expand Down
2 changes: 0 additions & 2 deletions src/lib/support/CHIPMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace Platform {
* This function is platform specific and might be empty in certain cases.
* For example, this function is doing nothing when the C Standard Library malloc()
* and free() functions are used for memory allocation.
* Note- The MemoryInit and MemoryShutdown APIs are thread-safe, unless CHIP_SYSTEM_CONFIG_NO_LOCKING is enabled
*
* @param[in] buf A pointer to a dedicated memory buffer, which should be used as
* a memory pool for CHIP memory allocation.
Expand Down Expand Up @@ -74,7 +73,6 @@ extern CHIP_ERROR MemoryInit(void * buf = nullptr, size_t bufSize = 0);
* This function can be an empty call if there is no need to release resources. For example,
* this is the case when the C Standard Library malloc() and free() functions are used
* for memory allocation.
* Note- The MemoryInit and MemoryShutdown APIs are thread-safe, unless CHIP_SYSTEM_CONFIG_NO_LOCKING is enabled
*
*/
extern void MemoryShutdown();
Expand Down

0 comments on commit 59e4898

Please sign in to comment.