From nobody Mon Jan 5 01:25:41 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dkxSj6zrMz6N9Fd for ; Mon, 05 Jan 2026 01:25:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dkxSj1j1qz3x1M for ; Mon, 05 Jan 2026 01:25:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1767576341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0awVm5Dsv8kF1HxKuZM7BJ8hrAjqKsMPeWLzJRWYM8c=; b=JY4DUwWA+AIhEFaGgCUNje4LDIFtvPqmgBRgetAcNUBUdA7vSSPFNkoYOA3bxPx1zg0F6G XRIr/3U8fKG4XcUV/OwT9vlXmsr5wIzjmPaXCFpRDHpfINUasp9wjUyV5/W+0CDf1rLPTZ 6UXMyYN4D5VNMKt/Hs0Hs8HN58UM50zssyrQl/kTfgMwY2ezCp0Zf5zTmliDTptNGd9I9a qddjHzMai0VSa3WsDATnHabOtdGyoRghbqcMFaRVKqjtajOzEh/2TRbJ3MT8O0zjqSI4vp iOhucXO7J8czYHLepNfcGPkGFwp+0nAf7zn2D/Q2b0M0xPcqQbr+JFbJLOLy8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1767576341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0awVm5Dsv8kF1HxKuZM7BJ8hrAjqKsMPeWLzJRWYM8c=; b=Kda689ycyLY+0XiBlzRxu6zz3YjvnGx7ck6FWjTYFJh+cnINxh2SHGse37I/LmcCvjo6/0 tkIls+ufLJ8VcOr7Vkpg+2+UTjX+gjzukKOmk14nMn9MYscJySfTlzP+bvlMRACr0f4Dhg Ifrpdw6CdkEekbeSPCWInEsKiukAwxd8yUk+mv/BomqK3CF+7+gvUACw9b4GDvzMu4JDoC 7FTuQlflYTFFUH7wmKmx4o988oEOhAezQ72VxcGj9wVrGK3nqPUk/HGPK8cZy9xAbkNleS tIMF2qvxEqupJkXOQ29RaVPSjq5wEEfq6+mrbEFkoNSPKE8auXSN3JAb9SrIpQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1767576341; a=rsa-sha256; cv=none; b=BK8wjakVADyp4XnyZKLsnMkS1c+GBxtmZlMoxhK3WPLuDDZx0ak0qCq9Uo4fKLx6cGsFYq U0g7KFn+XITZkm9mFq0roiQPXXKSjQ7Zw+5elBCnfJZNTSNM7bGnKcV6bsz/O6fa9U3QKG DnCVA0mJq/at45gQvUnhAqfZ9844khJZzh0884fKT6HD6eTJ64aN/n7Oq51UcGq3zXyTcb qewT9NBCqkXRTc6ryebFBKZIaJzO9Y9h/t0rI7GCCrw2POQD4rFdaAkstjPrOialzLYcVp hmCRczo297GJv2xBXN/mC3rK9S5iPCdVIS2gPgpTKYxCHEZHKeRqa6pEy4fl9g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dkxSj0w5tz10Sm for ; Mon, 05 Jan 2026 01:25:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 39934 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 05 Jan 2026 01:25:41 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 6197518ee905 - stable/15 - libc: add glibc-compatible tdestroy(3) List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 6197518ee90541a5e04d03dffda760b888d3a3e7 Auto-Submitted: auto-generated Date: Mon, 05 Jan 2026 01:25:41 +0000 Message-Id: <695b1315.39934.e20b48e@gitrepo.freebsd.org> The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6197518ee90541a5e04d03dffda760b888d3a3e7 commit 6197518ee90541a5e04d03dffda760b888d3a3e7 Author: Konstantin Belousov AuthorDate: 2025-12-25 23:38:25 +0000 Commit: Konstantin Belousov CommitDate: 2026-01-05 00:44:21 +0000 libc: add glibc-compatible tdestroy(3) (cherry picked from commit b8c99e7d912f0dad84cec80f8c4331646b87a3ec) --- include/search.h | 1 + lib/libc/stdlib/Makefile.inc | 1 + lib/libc/stdlib/Symbol.map | 1 + lib/libc/stdlib/tdestroy.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) diff --git a/include/search.h b/include/search.h index 0615da2b42ba..5ac25a0d491f 100644 --- a/include/search.h +++ b/include/search.h @@ -77,6 +77,7 @@ void twalk(const posix_tnode *, void (*)(const posix_tnode *, VISIT, int)); int hcreate_r(size_t, struct hsearch_data *); void hdestroy_r(struct hsearch_data *); int hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *); +void tdestroy(void *, void (*)(void *)); #endif __END_DECLS diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 37a927b0597b..e46a8d7340b2 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -67,6 +67,7 @@ MISRCS+= \ strtouq.c \ system.c \ tdelete.c \ + tdestroy.c \ tfind.c \ tsearch.c \ twalk.c diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 8b7e97c3cbdc..03a6d0b543ac 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -134,6 +134,7 @@ FBSD_1.8 { FBSD_1.9 { memalignment; recallocarray; + tdestroy; }; FBSDprivate_1.0 { diff --git a/lib/libc/stdlib/tdestroy.c b/lib/libc/stdlib/tdestroy.c new file mode 100644 index 000000000000..c324e151da11 --- /dev/null +++ b/lib/libc/stdlib/tdestroy.c @@ -0,0 +1,68 @@ +/* + * Copyright 2025 The FreeBSD Foundation + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This software was developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + */ + +#define _SEARCH_PRIVATE +#include +#include + +static void +nul_node_free(void *node __unused) +{ +} + +/* Find the leftmost node. */ +static posix_tnode * +tdestroy_find_leftmost(posix_tnode *tn) +{ + while (tn->llink != NULL) + tn = tn->llink; + return (tn); +} + +/* + * This algorithm for non-recursive non-allocating destruction of the tree + * is described in + * https://codegolf.stackexchange.com/questions/478/free-a-binary-tree/489#489P + * and in https://devblogs.microsoft.com/oldnewthing/20251107-00/?p=111774. + */ +void +tdestroy(void *rootp, void (*node_free)(void *)) +{ + posix_tnode *tn, *tn_leftmost, *xtn; + + tn = rootp; + if (tn == NULL) + return; + if (node_free == NULL) + node_free = nul_node_free; + tn_leftmost = tn; + + while (tn != NULL) { + /* + * Make the right subtree the left subtree of the + * leftmost node, and recalculate the leftmost. + */ + tn_leftmost = tdestroy_find_leftmost(tn_leftmost); + if (tn->rlink != NULL) { + tn_leftmost->llink = tn->rlink; + tn_leftmost = tn_leftmost->llink; + } + + /* + * At this point, all children of tn have been + * arranged to be reachable via tn->left. We can + * safely delete the current node and advance to its + * left child as the new root. + */ + xtn = tn->llink; + node_free(tn->key); + free(tn); + tn = xtn; + } +}