From patchwork Mon Dec 1 17:05:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 809 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=1764608990; bh=Q8/XPPlHQszDGMdC8wo2DDZOFSUvWF3mMmOomakRa40=; 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=WxDavQE8pId44yvx3BTESUAAjdyAJamTGLiKtkffBvKvEN5y1Nox6ZH2yX8lezYus BkOVBs5m4LM0Ki3JJp/pwvCo4nitv7bUiWswWd/yWcFOoxBIZDUSmlZm2zEgya6REX d/s+l3XUOwby5Noz9TopybeChToyGLmhjdoSWe396oDkTCjrrrQXqaLDz6+Z2r1ViO fJSXrhPf3COElA4Cud0AjDCJk/tBXMAG9YLNJl0iHa413GkVwwY5utnpnqmvMrG9pE +BiLfXtwc77FCARZLnxoMScz0u8uwk0fEcKIjDKyTXBRo03ZKhsMBAPWzFF2SLdWmd fydQqWqklnZ0A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 74A8F688C5 for ; Mon, 1 Dec 2025 10:09:50 -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 GaesZ3YHC5da for ; Mon, 1 Dec 2025 10:09:50 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608988; bh=Q8/XPPlHQszDGMdC8wo2DDZOFSUvWF3mMmOomakRa40=; 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=qwKutgh5hZ/KtIVHq49NTrZPy3DBbKreBs5HqEGAdI+Le16QyMehjqSfINcmuJUDZ uPdvSrmmlxfkypwkFApyrClONuj3AdtCEn8sa7UNDnUvWVbSciYrXwVk7lAOQtDlm3 IyIaImGIAHejUIMiPdyzoKs6YaGypr6ABL3Py8leHxRQurBNaMMtTbGynsqdGmE2Ij 4yJVz3mT4TMw8xIZ9qsq4gjv8ZU0AIQkBqewCh0oFp6Opf9SmegJL0gr6+keCIlfD3 pOM4z/kql/UPoWpftxNbjqO/kXv/bOx2aWzoZwIEv6WcicE1Ra8EHp7Kac7MzBy8PC 4xIfeeqExR92A== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AB28B6888B for ; Mon, 1 Dec 2025 10:09:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608986; bh=vH6Cs5byjz26VtO4X/BQHVbOrsYQ2RodQ1D5QSlF4cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BWb6TdptJuB8gnuFKcLJj/ZfKcICkUVcysUgOsURWPpTVMu4xJCDYMNkOZBOQPY5k sNHq2auDrTXx+LBmIt3SNnZKg693E2OGX/dRKiVbKz3e+nSO8HcEM1n0RUBDHeCG8s pNnwppRob9dKB0dLmi7RGXciO5v0mCLZB1zc9S3xphsI4ZjgEn4LiGQTq1dLzz4B0Y Dsnr2Z7Oqk1R1tA1PWe+RXEO7HTe/2Mr/rvzTl6q6GbY0LBKts8pE7gyAlAOdA01B6 imcsQsHX95E8WegIH3iBltn/esBnx7fdKfp3wmPyunvgazo7pAqdTFhXzmaGf4uoaa sGHmz0m7tJ+TQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D246F6881A; Mon, 1 Dec 2025 10:09:46 -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 InKDHup7YGRg; Mon, 1 Dec 2025 10:09:46 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608984; bh=GUNLWgH72l4NzyCoeAg461uibxVmKNdGa56EUy141OY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L0X1Rra4ED+yjmKyiSCPOuLxgXVLAAaOv7mPBXHKIX8GbaYggeLGfvqBCjDP1VdNV 0dfKmX8SU1e8HxBh4v8C7v1zbAMewEMOErUP2PKj0OTMpNSoZ3F0/ACSl/Q8QbBTpK JpgG8zj427qsOV+3nBKekcvvFETqPyHZS6/tT3CAbay8uqylmsdz0oIbmarmTk9xuA /DGwzTyOhkITbAGGWmXKWPt+BZgnE1wgGwpX2tJB4eaChq9arKSnnAzhdUOR+cSopl Hq4bThTfW/cAaabBfRKR1sNsmAPD0TM1Aaxgd1pzxIJBbhw5s+8uv2qVHFe5reOfi9 pDAvKUgvrYIdg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 927B4688AE; Mon, 1 Dec 2025 10:09:44 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Mon, 1 Dec 2025 10:05:13 -0700 Message-ID: <20251201170529.3237986-35-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: YPX3VZ2WIWHH2L3NRIOJZDKLX3AQTS32 X-Message-ID-Hash: YPX3VZ2WIWHH2L3NRIOJZDKLX3AQTS32 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 34/37] malloc: Add SMALLCHUNKS_AS_FUNCS to convert macros to funcs 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 The insert_small_chunk() and unlink_first_small_chunk() macros are inlined at multiple places in the code. Provide an option to convert these to functions, so the compiler can try to reduce code size. Add braces to the insert_chunk macro. This reduces code size imx8mp_venice SPL by about 208 bytes Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 65bfb97e1db..54fd2e4a08a 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -599,6 +599,7 @@ static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy( #define NO_REALLOC_IN_PLACE 1 #define SIMPLE_MEMALIGN 1 #define NO_TREE_BINS 1 +#define SMALLCHUNKS_AS_FUNCS 1 #else #define NO_TREE_BINS 0 #define SIMPLE_MEMALIGN 0 @@ -632,6 +633,10 @@ ulong mem_malloc_brk; #endif /* __UBOOT__ */ +#ifndef SMALLCHUNKS_AS_FUNCS +#define SMALLCHUNKS_AS_FUNCS 0 +#endif + #ifndef WIN32 #ifdef _WIN32 #define WIN32 1 @@ -3714,6 +3719,25 @@ static void internal_malloc_stats(mstate m) { */ /* Link a free chunk into a smallbin */ +#if defined(__UBOOT__) && SMALLCHUNKS_AS_FUNCS +static void insert_small_chunk(mstate M, mchunkptr P, size_t S) { + bindex_t I = small_index(S); + mchunkptr B = smallbin_at(M, I); + mchunkptr F = B; + assert(S >= MIN_CHUNK_SIZE); + if (!smallmap_is_marked(M, I)) + mark_smallmap(M, I); + else if (RTCHECK(ok_address(M, B->fd))) + F = B->fd; + else { + CORRUPTION_ERROR_ACTION(M); + } + B->fd = P; + F->bk = P; + P->fd = F; + P->bk = B; +} +#else #define insert_small_chunk(M, P, S) {\ bindex_t I = small_index(S);\ mchunkptr B = smallbin_at(M, I);\ @@ -3731,6 +3755,7 @@ static void internal_malloc_stats(mstate m) { P->fd = F;\ P->bk = B;\ } +#endif /* Unlink a chunk from a smallbin */ #define unlink_small_chunk(M, P, S) {\ @@ -3759,6 +3784,24 @@ static void internal_malloc_stats(mstate m) { } /* Unlink the first chunk from a smallbin */ +#if defined(__UBOOT__) && SMALLCHUNKS_AS_FUNCS +static void unlink_first_small_chunk(mstate M, mchunkptr B, mchunkptr P, bindex_t I) { + mchunkptr F = P->fd; + assert(P != B); + assert(P != F); + assert(chunksize(P) == small_index2size(I)); + if (B == F) { + clear_smallmap(M, I); + } + else if (RTCHECK(ok_address(M, F) && F->bk == P)) { + F->bk = B; + B->fd = F; + } + else { + CORRUPTION_ERROR_ACTION(M); + } +} +#else #define unlink_first_small_chunk(M, B, P, I) {\ mchunkptr F = P->fd;\ assert(P != B);\ @@ -3775,6 +3818,7 @@ static void internal_malloc_stats(mstate m) { CORRUPTION_ERROR_ACTION(M);\ }\ } +#endif /* Replace dv node, binning the old one */ /* Used only when dvsize known to be small */ @@ -3982,9 +4026,15 @@ static void unlink_large_chunk(mstate M, tchunkptr X) { /* Relays to large vs small bin operations */ +#if defined(__UBOOT__) && SMALLCHUNKS_AS_FUNCS +#define insert_chunk(M, P, S)\ + if (is_small(S)) { insert_small_chunk(M, P, S); }\ + else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } +#else #define insert_chunk(M, P, S)\ if (is_small(S)) insert_small_chunk(M, P, S)\ else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } +#endif #define unlink_chunk(M, P, S)\ if (is_small(S)) unlink_small_chunk(M, P, S)\