Skip to content

Commit

Permalink
Linux: Guard mallinfo() use behind check for __GLIBC__ (#33626)
Browse files Browse the repository at this point in the history
* Linux: Guard mallinfo() use behind check for __GLIBC__

* Use positive checks for __GLIBC__ rather than ifndef
  • Loading branch information
ksperling-apple authored and pull[bot] committed Dec 19, 2024
1 parent 73b9b2c commit 3532584
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/platform/Linux/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ enum class WiFiStatsCountType
kWiFiOverrunCount
};

#if defined(__GLIBC__)
// Static variable to store the maximum heap size
static size_t maxHeapHighWatermark = 0;
#endif

CHIP_ERROR GetEthernetStatsCount(EthernetStatsCountType type, uint64_t & count)
{
Expand Down Expand Up @@ -223,24 +225,22 @@ DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance()

CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree)
{
#ifndef __GLIBC__
return CHIP_ERROR_NOT_IMPLEMENTED;
#else
#if defined(__GLIBC__)
struct mallinfo mallocInfo = mallinfo();

// Get the current amount of heap memory, in bytes, that are not being utilized
// by the current running program.
currentHeapFree = mallocInfo.fordblks;

return CHIP_NO_ERROR;
#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif
}

CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed)
{
#ifndef __GLIBC__
return CHIP_ERROR_NOT_IMPLEMENTED;
#else
#if defined(__GLIBC__)
struct mallinfo mallocInfo = mallinfo();

// Get the current amount of heap memory, in bytes, that are being used by
Expand All @@ -253,14 +253,14 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap
maxHeapHighWatermark = currentHeapUsed;
}
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif
}

CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark)
{
#ifndef __GLIBC__
return CHIP_ERROR_NOT_IMPLEMENTED;
#else
#if defined(__GLIBC__)
struct mallinfo mallocInfo = mallinfo();

// The usecase of this function is embedded devices,on which we would need to intercept
Expand All @@ -279,6 +279,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & cu
currentHeapHighWatermark = maxHeapHighWatermark;

return CHIP_NO_ERROR;
#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif
}

Expand All @@ -287,10 +289,12 @@ CHIP_ERROR DiagnosticDataProviderImpl::ResetWatermarks()
// If implemented, the server SHALL set the value of the CurrentHeapHighWatermark attribute to the
// value of the CurrentHeapUsed.

#if defined(__GLIBC__)
// Get the current amount of heap memory, in bytes, that are being used by
// the current running program and reset the max heap high watermark to current heap amount.
struct mallinfo mallocInfo = mallinfo();
maxHeapHighWatermark = mallocInfo.uordblks;
#endif

return CHIP_NO_ERROR;
}
Expand Down

0 comments on commit 3532584

Please sign in to comment.