From patchwork Wed Dec 10 00:07:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 872 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325352; bh=niD8XzvXaZR4SbPB9UN/KsQMWpBu13+ZCu6jblrXQhU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=ZDPTnYeHgoAkqANmA38eFYpSzkPaSm9mDA18wf8+d2H94v52TyctpvISMTaEpuJLL vOPuJ53KLLi8EivjDQLLuHQFq3gI0sHGN3hBiALiQPzgx3FOoW/SO0LrCxLO+ZqLdG W6N8tVZ69h+mC/QMd/GL05ouzZ+sMxuzCEkm4MzhrmoB2+EYJGacnOrO+zUXO+sDQ3 76cQOCvx0sTh1kCl5UqtxdYJ/FrrLx0sMT9oLKcZonqagahvuH5KcoYlIyjeyrbtq3 TJXLJ7/dl6NrhvAA9qecDi8jXwui1O7F8L5WQZ7ESRcVCSjwpxPob991JhHqsmQHl2 0BJhRk3LFHjDw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E8A1768A22 for ; Tue, 9 Dec 2025 17:09:12 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id nXRS7owEKSWu for ; Tue, 9 Dec 2025 17:09:12 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325352; bh=niD8XzvXaZR4SbPB9UN/KsQMWpBu13+ZCu6jblrXQhU=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=ZDPTnYeHgoAkqANmA38eFYpSzkPaSm9mDA18wf8+d2H94v52TyctpvISMTaEpuJLL vOPuJ53KLLi8EivjDQLLuHQFq3gI0sHGN3hBiALiQPzgx3FOoW/SO0LrCxLO+ZqLdG W6N8tVZ69h+mC/QMd/GL05ouzZ+sMxuzCEkm4MzhrmoB2+EYJGacnOrO+zUXO+sDQ3 76cQOCvx0sTh1kCl5UqtxdYJ/FrrLx0sMT9oLKcZonqagahvuH5KcoYlIyjeyrbtq3 TJXLJ7/dl6NrhvAA9qecDi8jXwui1O7F8L5WQZ7ESRcVCSjwpxPob991JhHqsmQHl2 0BJhRk3LFHjDw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D644F689E6 for ; Tue, 9 Dec 2025 17:09:12 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325350; bh=jFBZlHvGLhaYiV66bpJYtGcE/xdxYBQhCDocw52/nyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b+N58qXDotf2aTn7T75Qcc35U2P8REyHvjyc1l6/ivBRzAQ0yvdieL/ZiIpCMzmDK EDF3zeCx9XITKlKzF6MpioaogLeAQxVhmags5bt2GfKHBp0fDZQJUK4wGORXWV3463 FSHvlKwdfOe0ht5ii+OW5EYVLeOVhQW8+DTW16BiRQxyrV0OGiBZdFhsWoJ/cZlvRZ XLaOLfTkaNNgkJmbgHWGIjuegCSLyTdJwJvu/MtZZaPgQzHSTMde4vKEmq0IgVdRD8 L1Ib3kFUIuCvtcs3IiXoEHg7xduOKnjpG1PxvO5tX+fTObwL3qkvSqbIkEdgtYwZ4i XtAxY/Yg0fDMA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id ED40A68957; Tue, 9 Dec 2025 17:09:10 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id rGg0JOnsGc0P; Tue, 9 Dec 2025 17:09:10 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765325346; bh=HuxHmHRq8v22xBkwgRGb+HL0t7pjXuNFDKCfP8KqeQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E1sJxIt5qo9tlw4t4VFKh5m+gFG8Te2CAdlaILX4Jo4+ZgY4hnsFDFcMiojA4nP4P il8ZparQJKWYjDp2pVj66zvD/LYKdRHe6AjiRHKY7Kt/klcz9aT4OHWbjjRNIVKR91 TvHj3wvnZwuzOsuo/i3cP3EnY2Q+YjZ1d/oh/OrDRHzo3VMPfd5v8s8qcyfjiOX3JS q2P3eFh01HrIpfzpeNtgRmk8tuF9IuL/TDAzzKSiadS3t+4R4idisya6GcdylPFmBj QTlSD0o5wKqLWFf/Wqd5VNuz3+wuIyV3zlHcJHfRKTB9MD0Ew2ylB2RGVRO7byCCra wDgpsBb896ePw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 7262A6884F; Tue, 9 Dec 2025 17:09:06 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Dec 2025 17:07:06 -0700 Message-ID: <20251210000737.180797-16-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251210000737.180797-1-sjg@u-boot.org> References: <20251210000737.180797-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: HKFJMCH23CB7MVXHC476Z4MH6CI6TCKC X-Message-ID-Hash: HKFJMCH23CB7MVXHC476Z4MH6CI6TCKC X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 15/35] malloc: Add malloc_get_info() to retrieve memory statistics List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add struct malloc_info and malloc_get_info() function to programmatically access the memory-allocation stats that malloc_stats() prints. The struct contains the size of the malloc() poll and the number of bytes in use. Add a static inline stub to return an error when DEBUG is not defined. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 10 ++++++++++ include/malloc.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index efeff18653f..004dafe4b3b 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -3689,6 +3689,16 @@ static struct mallinfo internal_mallinfo(mstate m) { } return nm; } + +int malloc_get_info(struct malloc_info *info) +{ + struct mallinfo mi = internal_mallinfo(gm); + + info->total_bytes = mem_malloc_end - mem_malloc_start; + info->in_use_bytes = mi.uordblks; + + return 0; +} #endif /* !NO_MALLINFO */ #if !NO_MALLOC_STATS diff --git a/include/malloc.h b/include/malloc.h index 73b2da0c383..fd8a8ddcfaf 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -665,8 +665,22 @@ void mspace_inspect_all(mspace msp, /* --------------------- U-Boot additions --------------------- */ #ifdef __UBOOT__ +#include #include +/** + * struct malloc_info - Memory allocation statistics + * + * This is filled in by malloc_get_info(). + * + * @total_bytes: Total bytes available in the heap + * @in_use_bytes: Current bytes allocated (in use by application) + */ +struct malloc_info { + ulong total_bytes; + ulong in_use_bytes; +}; + /* Memory pool boundaries */ extern ulong mem_malloc_start; extern ulong mem_malloc_end; @@ -735,6 +749,21 @@ void *memalign_simple(size_t alignment, size_t bytes); */ int initf_malloc(void); +/** + * malloc_get_info() - Get memory allocation statistics + * + * @info: Place to put the statistics + * Return: 0 on success, -ENOSYS if not available (DEBUG not defined) + */ +#ifdef DEBUG +int malloc_get_info(struct malloc_info *info); +#else +static inline int malloc_get_info(struct malloc_info *info) +{ + return -ENOSYS; +} +#endif + #endif /* __UBOOT__ */ #ifdef __cplusplus