From nobody Mon Aug 21 14:17:17 2023 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 4RTvgf0JdNz4qqd6; Mon, 21 Aug 2023 14:17:18 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RTvgd6XHkz3c2Z; Mon, 21 Aug 2023 14:17:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692627437; 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=UfoxVzPaaq5K3Cm+MPf6fu4ZDmKRnuTvkQdtzWvFjWA=; b=LXL2c3/jHlAm8RvZUG8qX2ju8rirWbqrJjtxkC/VT79tI2avoq114HLHsfiq3XnAVPD1kf 2uGDBGstoDbuYVR0mKiOi/6mDCyaXV8eDUdoHRgaG55WPUmz+//ZH1alLWecIgTtEhhhSy 6Ib+4XzlDiG8LEsz8cGmKBRO7v9mT1w3KjwwamhCxuQOQ8ZYGSeFKFYs1y2aULlLOEf1ww Dyc+5TBdaSDyV1KBAds9IBZMR8vtBa1XNU0YcJbEKqDmFNxG+pQDrkBgotf8v36hlDrd0A Xh6UBN3y6sT06RKbpwKBlEtm58R8nMt+KR95Zx7aUG/DbMsZSxsCsMqdokx0yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692627437; 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=UfoxVzPaaq5K3Cm+MPf6fu4ZDmKRnuTvkQdtzWvFjWA=; b=h0qoycY47VxwiRmT/mktXij2g3XQxmP5P3c6fgh/4dABKDlltyOYenawBR9wtnhsQu3cxx HkXWRro6mCpUhBnpjSUA+W/1u7mujagK/zuER12NH4rhSzCrxeLMZDG9MJL2qlRAXd+X3R KnmYB/UaAjzfALvgcBNMqE5OhYXgUSSe0jsGiz8b2TYtNcaazdKqTm0tctkp4M/SO+z/wr 9KHbxc7amO6h+HIjSOwjkblivB7R/Zki+dDfFAmQN9N7AvYzSPlRmJqUVCnPsJvq0WYqRK GokdP0SQ13LljCvliwYLgTYIufHHROEqATD1C0Vheb9N8RqVE0Ny1N7en/l3/g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1692627437; a=rsa-sha256; cv=none; b=E2uDERlqBGwCJ1z7dlCZIHjsj6cPvPhjPBm+upRvx+KpWj5eM9Rgvw31OpsD6/2OOY8uL9 omM2pplrnERo/SFI5RUwYhWjvrJjqs8NT7dq/tHp11KbLwAszrWvRCtTdPtZmb7y08l4Fz NH2PNxaNFv5qtYLT6pf4CGVrh7ej636QRwN1S/3SPf9481mCxyOIrfnM4zO53Rps99qBK4 kbI/EOu4HA5jKcVpKyjJwXrNcs2+t/zx3P5lHdx2l6p8/+Mp++S4CFMEwwQGjVaEW2CxPX dSupOveDDLQoYXGZoV4q9eBZo90EcPBH56tUeYKXPHTQ65iwtG9IxrzQmmTOwQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RTvgd5d6Xz80Q; Mon, 21 Aug 2023 14:17:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37LEHHRx097440; Mon, 21 Aug 2023 14:17:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37LEHHOj097437; Mon, 21 Aug 2023 14:17:17 GMT (envelope-from git) Date: Mon, 21 Aug 2023 14:17:17 GMT Message-Id: <202308211417.37LEHHOj097437@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: c29ee08204ce - main - rtld_malloc: add __crt_aligned_alloc_offset() 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: 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/main X-Git-Reftype: branch X-Git-Commit: c29ee08204ce4106d4992474005c5f2fb7d5fbf1 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=c29ee08204ce4106d4992474005c5f2fb7d5fbf1 commit c29ee08204ce4106d4992474005c5f2fb7d5fbf1 Author: Konstantin Belousov AuthorDate: 2023-07-22 04:21:45 +0000 Commit: Konstantin Belousov CommitDate: 2023-08-21 14:16:42 +0000 rtld_malloc: add __crt_aligned_alloc_offset() It is modelled after aligned_alloc(3). Most importantly, to free the allocation, __crt_free() can be used. Additionally, caller may specify offset into the aligned allocation, so that we return offset-ed from alignment pointer. Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41150 --- libexec/rtld-elf/rtld_malloc.c | 34 +++++++++++++++++++++++++++++++--- libexec/rtld-elf/rtld_malloc.h | 1 + 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/libexec/rtld-elf/rtld_malloc.c b/libexec/rtld-elf/rtld_malloc.c index ac8af4d6b6e1..6e011e88ba5a 100644 --- a/libexec/rtld-elf/rtld_malloc.c +++ b/libexec/rtld-elf/rtld_malloc.c @@ -86,13 +86,15 @@ static void morecore(int bucket); static int morepages(int n); #define MAGIC 0xef /* magic # on accounting info */ +#define AMAGIC 0xdf /* magic # for aligned alloc */ /* * nextf[i] is the pointer to the next free block of size * (FIRST_BUCKET_SIZE << i). The overhead information precedes the data * area returned to the user. */ -#define FIRST_BUCKET_SIZE 8 +#define LOW_BITS 3 +#define FIRST_BUCKET_SIZE (1U << LOW_BITS) #define NBUCKETS 30 static union overhead *nextf[NBUCKETS]; @@ -169,6 +171,28 @@ __crt_calloc(size_t num, size_t size) return (ret); } +void * +__crt_aligned_alloc_offset(size_t align, size_t size, size_t offset) +{ + void *mem, *ov; + union overhead ov1; + uintptr_t x; + + if (align < FIRST_BUCKET_SIZE) + align = FIRST_BUCKET_SIZE; + offset &= align - 1; + mem = __crt_malloc(size + align + offset + sizeof(union overhead)); + if (mem == NULL) + return (NULL); + x = roundup2((uintptr_t)mem + sizeof(union overhead), align); + x += offset; + ov = cp2op((void *)x); + ov1.ov_magic = AMAGIC; + ov1.ov_index = x - (uintptr_t)mem - sizeof(union overhead); + memcpy(ov, &ov1, sizeof(ov1)); + return ((void *)x); +} + /* * Allocate more memory to the indicated bucket. */ @@ -210,12 +234,16 @@ morecore(int bucket) void __crt_free(void *cp) { + union overhead *op, op1; + void *opx; int size; - union overhead *op; if (cp == NULL) return; - op = cp2op(cp); + opx = cp2op(cp); + memcpy(&op1, opx, sizeof(op1)); + op = op1.ov_magic == AMAGIC ? (void *)((caddr_t)cp - op1.ov_index) : + opx; if (op->ov_magic != MAGIC) return; /* sanity */ size = op->ov_index; diff --git a/libexec/rtld-elf/rtld_malloc.h b/libexec/rtld-elf/rtld_malloc.h index f9780c552821..247726b9f470 100644 --- a/libexec/rtld-elf/rtld_malloc.h +++ b/libexec/rtld-elf/rtld_malloc.h @@ -32,6 +32,7 @@ #ifndef RTLD_MALLOC_H #define RTLD_MALLOC_H +void *__crt_aligned_alloc_offset(size_t align, size_t size, size_t offset); void *__crt_calloc(size_t num, size_t size); void __crt_free(void *cp); void *__crt_malloc(size_t nbytes);