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

Linux: Guard mallinfo() use behind check for __GLIBC__ #33626

Merged
Merged
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
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
Loading