FreeBSD/sys/kern/subr_bus.c
/**
 * @internal
 * @brief Allocate a unit number
 *
 * On entry, @p *unitp is the desired unit number (or @c -1 if any
 * will do). The allocated unit number is returned in @p *unitp.

 * @param dc		the devclass to allocate from
 * @param unitp		points at the location for the allocated unit
 *			number
 *
 * @retval 0		success
 * @retval EEXIST	the requested unit number is already allocated
 * @retval ENOMEM	memory allocation failure
 */
@internal
@brief Allocate a unit number

On entry, @p *unitp is the desired unit number (or @c -1 if any
will do). The allocated unit number is returned in @p *unitp.

@param dc            the devclass to allocate from
@param unitp         points at the location for the allocated unit
number

@retval 0            success
@retval EEXIST       the requested unit number is already allocated
@retval ENOMEM       memory allocation failure
@internal @brief Allocate a unit number On entry @p * unitp is the desired unit number ( or @c - 1 if any will do ) . The allocated unit number is returned in @p * unitp . @param dc the devclass to allocate from @param unitp points at the location for the allocated unit number @retval 0 success @retval EEXIST the requested unit number is already allocated @retval ENOMEM memory allocation failure
static int
devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
{
	const char *s;
	int unit = *unitp;
	PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
	if (unit == -1)
		BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
		    &unit);
	if (unit != -1) {
		if (unit >= 0 && unit < dc->maxunit &&
		    dc->devices[unit] != NULL) {
			if (bootverbose)
				printf("%s: %s%d already exists; skipping it\n",
				    dc->name, dc->name, *unitp);
			return (EEXIST);
		}
	} else {
		unit = 0;
		for (unit = 0;; unit++) {
			if (resource_string_value(dc->name, unit, "at", &s) ==
			    0)
				continue;
			if (unit < dc->maxunit && dc->devices[unit] != NULL)
				continue;
			break;
		}
	}
	if (unit >= dc->maxunit) {
		device_t *newlist, *oldlist;
		int newsize;
		oldlist = dc->devices;
		newsize = roundup((unit + 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,
		    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)));
	*unitp = unit;
	return (0);
}
static int devclass_alloc_unit ( devclass_t dc , device_t dev , int * unitp ) { const char * s ; int unit = * unitp ; PDEBUG ( ( " " , unit , DEVCLANAME ( dc ) ) ) ; if ( unit == -1 ) BUS_HINT_DEVICE_UNIT ( device_get_parent ( dev ) , dev , dc -> name , & unit ) ; if ( unit != -1 ) { if ( unit >= 0 && unit < dc -> maxunit && dc -> devices [ unit ] != NULL ) { if ( bootverbose ) printf ( " \n " , dc -> name , dc -> name , * unitp ) ; return ( EEXIST ) ; } } else { unit = 0 ; for ( unit = 0 ; ; unit ++ ) { if ( resource_string_value ( dc -> name , unit , " " , & s ) == 0 ) continue ; if ( unit < dc -> maxunit && dc -> devices [ unit ] != NULL ) continue ; break ; } } if ( unit >= dc -> maxunit ) { device_t * newlist , * oldlist ; int newsize ; oldlist = dc -> devices ; newsize = roundup ( ( unit + 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 , sizeof ( device_t ) * ( newsize - dc -> maxunit ) ) ; dc -> devices = newlist ; dc -> maxunit = newsize ; if ( oldlist != NULL ) free ( oldlist , M_BUS ) ; } PDEBUG ( ( " " , unit , DEVCLANAME ( dc ) ) ) ; * unitp = unit ; return ( 0 ) ; }
@internal
 @brief Allocate a unit number
@internal @brief Allocate a unit number
/* Ask the parent bus if it wants to wire this device. */ /* If we were given a wired unit number, check for existing device */ /* XXX imp XXX */ /* Unwired device, find the next available slot for it */ /* If there is an "at" hint for a unit then skip it. */ /* If this device slot is already in use, skip it. */ /*
	 * 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.
	 */