Skip to content

Commit 72190c0

Browse files
committed
libc: common: Add support for malloc stats
If malloc is enabled in common libc, add support for malloc stats using the mallinfo API. Signed-off-by: Chaitanya Tata <[email protected]>
1 parent c709414 commit 72190c0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/libc/common/source/stdlib/malloc.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <stdlib.h>
8+
#include <malloc.h>
89
#include <zephyr/kernel.h>
910
#include <zephyr/init.h>
1011
#include <errno.h>
@@ -229,6 +230,38 @@ void free(void *ptr)
229230
(void) sys_mutex_unlock(&z_malloc_heap_mutex);
230231
}
231232

233+
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 33)
234+
struct mallinfo mallinfo(void)
235+
#else
236+
struct mallinfo2 mallinfo2(void)
237+
#endif /* __GLIBC__ < 2 && (__GLIBC__ == 2 && __GLIBC_MINOR__ < 33) */
238+
{
239+
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 33)
240+
struct mallinfo info = { 0 };
241+
#else
242+
struct mallinfo2 info = { 0 };
243+
#endif /* __GLIBC__ < 2 && (__GLIBC__ == 2 && __GLIBC_MINOR__ < 33) */
244+
#ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
245+
struct sys_memory_stats stats;
246+
int ret = sys_heap_runtime_stats_get(&z_malloc_heap, &stats);
247+
248+
if (ret != 0) {
249+
LOG_ERR("Failed to get heap stats");
250+
return info;
251+
}
252+
253+
info.arena = stats.free_bytes + stats.allocated_bytes;
254+
info.fordblks = stats.free_bytes;
255+
info.uordblks = stats.allocated_bytes;
256+
info.usmblks = stats.max_allocated_bytes;
257+
#else
258+
ARG_UNUSED(info);
259+
LOG_ERR("CONFIG_SYS_HEAP_RUNTIME_STATS is not enabled");
260+
#endif
261+
262+
return info;
263+
}
264+
232265
SYS_INIT(malloc_prepare, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
233266
#else /* No malloc arena */
234267
void *malloc(size_t size)
@@ -251,6 +284,15 @@ void *realloc(void *ptr, size_t size)
251284
ARG_UNUSED(ptr);
252285
return malloc(size);
253286
}
287+
288+
struct mallinfo2 mallinfo2(void)
289+
{
290+
struct mallinfo2 info = { 0 };
291+
292+
LOG_ERR("CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE is 0");
293+
294+
return info;
295+
}
254296
#endif /* else no malloc arena */
255297

256298
#endif /* CONFIG_COMMON_LIBC_MALLOC */

lib/libc/minimal/include/malloc.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* malloc.h */
2+
3+
/*
4+
* Copyright (c) 2023 - Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
#ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_MALLOC_H_
9+
#define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_MALLOC_H_
10+
11+
#include <stddef.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
struct mallinfo2 {
18+
size_t arena; /* total space allocated from system */
19+
size_t ordblks; /* number of non-inuse chunks */
20+
size_t smblks; /* unused -- always zero */
21+
size_t hblks; /* number of mmapped regions */
22+
size_t hblkhd; /* total space in mmapped regions */
23+
size_t usmblks; /* unused -- always zero */
24+
size_t fsmblks; /* unused -- always zero */
25+
size_t uordblks; /* total allocated space */
26+
size_t fordblks; /* total non-inuse space */
27+
size_t keepcost; /* top-most, releasable (via malloc_trim) space */
28+
};
29+
30+
struct mallinfo2 mallinfo2(void);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
#endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_MALLOC_H_ */

0 commit comments

Comments
 (0)