git: 71d31f1cf601 - main - malloc_aligned(9): allow zero size and alignment

Konstantin Belousov kib at FreeBSD.org
Sat Sep 25 13:11:30 UTC 2021


The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=71d31f1cf6012b143fd676f099430818ae949c3f

commit 71d31f1cf6012b143fd676f099430818ae949c3f
Author:     Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-09-24 19:38:53 +0000
Commit:     Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-09-25 12:58:12 +0000

    malloc_aligned(9): allow zero size and alignment
    
    For alignment we do not need to do anything to make it operational.
    For size, upgrade zero sized request to one byte so that we do not
    request insane amount of memory for placeholder.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D32127
---
 sys/kern/kern_malloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 0bdce47b37b4..b1c5909db2a4 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -797,7 +797,7 @@ malloc_domainset_aligned(size_t size, size_t align,
 	void *res;
 	size_t asize;
 
-	KASSERT(align != 0 && powerof2(align),
+	KASSERT(powerof2(align),
 	    ("malloc_domainset_aligned: wrong align %#zx size %#zx",
 	    align, size));
 	KASSERT(align <= PAGE_SIZE,
@@ -812,6 +812,8 @@ malloc_domainset_aligned(size_t size, size_t align,
 	 * align, since malloc zones provide alignment equal to their
 	 * size.
 	 */
+	if (size == 0)
+		size = 1;
 	asize = size <= align ? align : 1UL << flsl(size - 1);
 
 	res = malloc_domainset(asize, mtp, ds, flags);


More information about the dev-commits-src-all mailing list