Skip to content

Commit 4dac536

Browse files
Linux: Guard mallinfo() use behind check for __GLIBC__ (#33626)
* Linux: Guard mallinfo() use behind check for __GLIBC__ * Use positive checks for __GLIBC__ rather than ifndef
1 parent b9eee28 commit 4dac536

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/platform/Linux/DiagnosticDataProviderImpl.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ enum class WiFiStatsCountType
7474
kWiFiOverrunCount
7575
};
7676

77+
#if defined(__GLIBC__)
7778
// Static variable to store the maximum heap size
7879
static size_t maxHeapHighWatermark = 0;
80+
#endif
7981

8082
CHIP_ERROR GetEthernetStatsCount(EthernetStatsCountType type, uint64_t & count)
8183
{
@@ -223,24 +225,22 @@ DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance()
223225

224226
CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapFree(uint64_t & currentHeapFree)
225227
{
226-
#ifndef __GLIBC__
227-
return CHIP_ERROR_NOT_IMPLEMENTED;
228-
#else
228+
#if defined(__GLIBC__)
229229
struct mallinfo mallocInfo = mallinfo();
230230

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

235235
return CHIP_NO_ERROR;
236+
#else
237+
return CHIP_ERROR_NOT_IMPLEMENTED;
236238
#endif
237239
}
238240

239241
CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeapUsed)
240242
{
241-
#ifndef __GLIBC__
242-
return CHIP_ERROR_NOT_IMPLEMENTED;
243-
#else
243+
#if defined(__GLIBC__)
244244
struct mallinfo mallocInfo = mallinfo();
245245

246246
// Get the current amount of heap memory, in bytes, that are being used by
@@ -253,14 +253,14 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapUsed(uint64_t & currentHeap
253253
maxHeapHighWatermark = currentHeapUsed;
254254
}
255255
return CHIP_NO_ERROR;
256+
#else
257+
return CHIP_ERROR_NOT_IMPLEMENTED;
256258
#endif
257259
}
258260

259261
CHIP_ERROR DiagnosticDataProviderImpl::GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark)
260262
{
261-
#ifndef __GLIBC__
262-
return CHIP_ERROR_NOT_IMPLEMENTED;
263-
#else
263+
#if defined(__GLIBC__)
264264
struct mallinfo mallocInfo = mallinfo();
265265

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

281281
return CHIP_NO_ERROR;
282+
#else
283+
return CHIP_ERROR_NOT_IMPLEMENTED;
282284
#endif
283285
}
284286

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

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

295299
return CHIP_NO_ERROR;
296300
}

0 commit comments

Comments
 (0)