From patchwork Mon Dec 1 17:04:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 783 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=1764608897; bh=TBwWFk7QxMvYwC/efFG4WeuSyHRpHZwBEekjjOiF8IA=; 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=Zek9Xebeelt/Lp9c0uGBbRZl6JCvGqIV/awVQ6db72tYwWnfwtjenfxNc1takW9io fS2jUIW6GW/mmIQzhdXuJIT6SM9tgq/vWAnJxtLuInxkFFE9JXMCbno3iZhY2wLR3s XN7nlYm1bo7EcEmol83RI9AQ6Fy5izM/2grLAsUaftDQet3UAnbHteGXvBOupwwQKh +m68FCxiGYh5FTqEjb6GRPVDF+yMxCBCIr5g7D8GlPyk2EXpau4LUPad0Vt83dSjqD tJ0TR7+D1stYU4X/r8OkPKLaorBtHgQ2wO8j1z9y+uG+6br6EQpFzCs678BghOqNBq eMn5pFbOFB0bw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6CE135E439 for ; Mon, 1 Dec 2025 10:08:17 -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 3WopAtCt79nH for ; Mon, 1 Dec 2025 10:08:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608897; bh=TBwWFk7QxMvYwC/efFG4WeuSyHRpHZwBEekjjOiF8IA=; 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=Zek9Xebeelt/Lp9c0uGBbRZl6JCvGqIV/awVQ6db72tYwWnfwtjenfxNc1takW9io fS2jUIW6GW/mmIQzhdXuJIT6SM9tgq/vWAnJxtLuInxkFFE9JXMCbno3iZhY2wLR3s XN7nlYm1bo7EcEmol83RI9AQ6Fy5izM/2grLAsUaftDQet3UAnbHteGXvBOupwwQKh +m68FCxiGYh5FTqEjb6GRPVDF+yMxCBCIr5g7D8GlPyk2EXpau4LUPad0Vt83dSjqD tJ0TR7+D1stYU4X/r8OkPKLaorBtHgQ2wO8j1z9y+uG+6br6EQpFzCs678BghOqNBq eMn5pFbOFB0bw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4D403688B0 for ; Mon, 1 Dec 2025 10:08:17 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608894; bh=2+UhZjqPoEEYa6TXujhqd4JmxPPMWQPQvTmSzPnw87U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EO+PhtALqNG1s86l+ghDz3gIhcjN8yze9b+wswKs9vpy4at/xRFh5fQ55AkWJznJy a2M99l5YBmdG0XEGAWiJXA5NphYm+Dz9LKrxMb9N5jIZ+n5rgrqpvzpBmjYB59ESzT EbzYBlzkiZNfSFn69qnSh4JISZMWmyVBC/JySlQZFY6dNK48IOmolzCKpNi7QmQtv4 cZnpDl140hfBilZu4rgwjssYVAfKs92zduPr/u1wJ23DPia9016pvb+vNESwzmvSxP oSaR/UxC5/9CYI+By7CPbYBlFLbyXZZ0LGGOT2hQJAVSHyLfLIDmPtlwolxksQECIY IQuXroTdx5SIQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C88AD5E439; Mon, 1 Dec 2025 10:08:14 -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 lnigxQ1fCzNI; Mon, 1 Dec 2025 10:08:14 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608890; bh=G7Q61BAAuuZFo4ZR5uis45AC8r5hag112KmONGY8qi8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lahvr4DK0oJMmfqZ0wUGJJydb1LiIDVZ96/brxQYz/EAW7j33jkat+Xn7uD/hD8nw tD/ZcIRSJGdVy8Gvfdy7P6DOyw31XlUssbDGXg3IZ+zPy91JBJKvtsBO+ZV4yG3zfN 08hA0xZLBXScbMhyI3aZoCur4i8kloHb0gQNAS0ZTbZ+uN9XIgnAER0IieZIyM5gaI rqJci0ROQ9mVCJpyqc0ubXht35yq5ocJVaN0np5JnNuBukG2nWgBBE0p7JExYP5rh4 a0djQe4Wr1GG4VI+mxyo3q+vZsF3zLNW6HLIeYKx+ZvQof0VlVow8cwuWP3mFgQNQf eX5irJhgdh3+Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 5F0846881A; Mon, 1 Dec 2025 10:08:10 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Mon, 1 Dec 2025 10:04:47 -0700 Message-ID: <20251201170529.3237986-9-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251201170529.3237986-1-sjg@u-boot.org> References: <20251201170529.3237986-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: BGIG6IEII7RQZZRKKNB5KAWNNY2SWNVV X-Message-ID-Hash: BGIG6IEII7RQZZRKKNB5KAWNNY2SWNVV 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 08/37] malloc: Add mem_malloc_init() and sbrk() 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 mem_malloc_init() to initialise the malloc heap and sbrk() for heap extension. These are U-Boot-specific functions that manage the memory pool used by dlmalloc. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ include/malloc.h | 27 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index ba8ca5bd0ae..2be2262ebeb 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -542,6 +542,18 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #define DLMALLOC_EXPORT extern #endif +#ifdef __UBOOT__ +#include +#include + +DECLARE_GLOBAL_DATA_PTR; + +ulong mem_malloc_start; +ulong mem_malloc_end; +ulong mem_malloc_brk; + +#endif /* __UBOOT__ */ + #ifndef WIN32 #ifdef _WIN32 #define WIN32 1 @@ -6290,3 +6302,42 @@ History: structure of old version, but most details differ.) */ + +/* --------------------- U-Boot additions --------------------- */ + +#ifdef __UBOOT__ + +void *sbrk(ptrdiff_t increment) +{ + ulong old = mem_malloc_brk; + ulong new = old + increment; + + /* mem_malloc_end points one byte past the end, so >= is correct */ + if ((new < mem_malloc_start) || (new >= mem_malloc_end)) + return (void *)MORECORE_FAILURE; + + /* + * if we are giving memory back make sure we clear it out since + * we set MORECORE_CLEARS to 1 + */ + if (increment < 0) + memset((void *)new, '\0', -increment); + + mem_malloc_brk = new; + + return (void *)old; +} + +void mem_malloc_init(ulong start, ulong size) +{ + mem_malloc_start = (ulong)map_sysmem(start, size); + mem_malloc_end = mem_malloc_start + size; + mem_malloc_brk = mem_malloc_end; + + debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start, + mem_malloc_end); +#if CONFIG_IS_ENABLED(SYS_MALLOC_CLEAR_ON_INIT) + memset((void *)mem_malloc_start, '\0', size); +#endif +} +#endif /* __UBOOT__ */ diff --git a/include/malloc.h b/include/malloc.h index 4608082d2e0..e0a5b732203 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -625,6 +625,33 @@ void mspace_inspect_all(mspace msp, void* arg); #endif /* MSPACES */ +/* --------------------- U-Boot additions --------------------- */ + +#ifdef __UBOOT__ +#include + +/* Memory pool boundaries */ +extern ulong mem_malloc_start; +extern ulong mem_malloc_end; +extern ulong mem_malloc_brk; + +/** + * mem_malloc_init() - Initialize the malloc() heap + * + * @start: Start address of heap memory region + * @size: Size of heap memory region in bytes + */ +void mem_malloc_init(ulong start, ulong size); + +/** + * sbrk() - Extend the heap + * + * @increment: Number of bytes to add (or remove if negative) + * Return: Previous break value on success, MORECORE_FAILURE on error + */ +void *sbrk(ptrdiff_t increment); +#endif /* __UBOOT__ */ + #ifdef __cplusplus }; /* end of extern "C" */ #endif