git: f3d3c63442ff - main - newbus: Simplify reallocation of devices array

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 01 Nov 2024 17:55:48 UTC
The branch main has been updated by imp:

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

commit f3d3c63442fff813d4941e0c73b7260834642422
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-10-31 22:50:42 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-11-01 17:56:20 +0000

    newbus: Simplify reallocation of devices array
    
    Simplify the complex expression down to what it is on 64-bit systems.
    32-bit kernels would allocate 2 at a time. Replace all that with
    reallocf which will eliminate a bunch of copies. This should be faster
    and simpler on both types of kernels. In addition, transition to
    M_WAITOK since this is a sleepable context.
    
    Suggested by:           jhb
    Sponsored by:           Netflix
    Reviewed by:            jhb
    Differential Revision:  https://reviews.freebsd.org/D47362
---
 sys/kern/subr_bus.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index 8fec2b3de139..cd5f97cc72fe 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -1229,28 +1229,18 @@ devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
 	}
 
 	/*
-	 * We've selected a unit beyond the length of the table, so let's
-	 * extend the table to make room for all units up to and including
-	 * this one.
+	 * We've selected a unit beyond the length of the table, so let's extend
+	 * the table to make room for all units up to and including this one.
 	 */
 	if (unit >= dc->maxunit) {
-		device_t *newlist, *oldlist;
 		int newsize;
 
-		oldlist = dc->devices;
-		newsize = roundup((unit + 1),
-		    MAX(1, MINALLOCSIZE / sizeof(device_t)));
-		newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
-		if (!newlist)
-			return (ENOMEM);
-		if (oldlist != NULL)
-			bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
-		bzero(newlist + dc->maxunit,
+		newsize = unit + 1;
+		dc->devices = reallocf(dc->devices,
+		    newsize * sizeof(*dc->devices), M_BUS, M_WAITOK);
+		memset(dc->devices + dc->maxunit, 0,
 		    sizeof(device_t) * (newsize - dc->maxunit));
-		dc->devices = newlist;
 		dc->maxunit = newsize;
-		if (oldlist != NULL)
-			free(oldlist, M_BUS);
 	}
 	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));