mirror of
https://github.com/freebsd/freebsd-src
synced 2024-10-15 04:43:53 +00:00
subr_bus: introduce device_set_descf() and modify allocation logic
device_set_descf() is a printf-like version of device_set_desc(). Allocation code has been transferred from device_set_desc_internal() to device_set_desc_copy() and device_set_descf() to avoid complicating device_set_desc_internal(). The "copy" argument in device_set_desc_internal() has been replaced with a flag which is set when the description string has been allocated with M_BUS. Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Reviewed by: imp, markj Differential Revision: https://reviews.freebsd.org/D43370
This commit is contained in:
parent
45cd29412e
commit
6b6914c1e2
|
@ -1012,6 +1012,7 @@ MLINKS+=device_get_sysctl.9 device_get_sysctl_ctx.9 \
|
|||
MLINKS+=device_quiet.9 device_is_quiet.9 \
|
||||
device_quiet.9 device_verbose.9
|
||||
MLINKS+=device_set_desc.9 device_get_desc.9 \
|
||||
device_set_desc.9 device_set_descf.9 \
|
||||
device_set_desc.9 device_set_desc_copy.9
|
||||
MLINKS+=device_set_flags.9 device_get_flags.9
|
||||
MLINKS+=devstat.9 devicestat.9 \
|
||||
|
|
|
@ -26,11 +26,12 @@
|
|||
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
.\"
|
||||
.Dd June 16, 1998
|
||||
.Dd January 9, 2024
|
||||
.Dt DEVICE_SET_DESC 9
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm device_set_desc ,
|
||||
.Nm device_set_descf ,
|
||||
.Nm device_set_desc_copy ,
|
||||
.Nm device_get_desc
|
||||
.Nd access the description of a device
|
||||
|
@ -40,6 +41,8 @@
|
|||
.Ft void
|
||||
.Fn device_set_desc "device_t dev" "const char *desc"
|
||||
.Ft void
|
||||
.Fn device_set_descf "device_t dev" "const char *fmt" "..."
|
||||
.Ft void
|
||||
.Fn device_set_desc_copy "device_t dev" "const char *desc"
|
||||
.Ft const char *
|
||||
.Fn device_get_desc "device_t dev"
|
||||
|
@ -54,6 +57,9 @@ is used to set the description if the string passed is a temporary
|
|||
buffer which will be overwritten.
|
||||
In this case, the system will copy
|
||||
the string, otherwise the pointer passed will be used directly.
|
||||
.Fn device_set_descf
|
||||
is a printf-like version of
|
||||
.Fn device_set_desc .
|
||||
.Sh SEE ALSO
|
||||
.Xr device 9
|
||||
.Sh AUTHORS
|
||||
|
|
|
@ -1997,7 +1997,7 @@ device_log(device_t dev, int pri, const char * fmt, ...)
|
|||
* @internal
|
||||
*/
|
||||
static void
|
||||
device_set_desc_internal(device_t dev, const char* desc, int copy)
|
||||
device_set_desc_internal(device_t dev, const char *desc, bool allocated)
|
||||
{
|
||||
if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
|
||||
free(dev->desc, M_BUS);
|
||||
|
@ -2005,16 +2005,9 @@ device_set_desc_internal(device_t dev, const char* desc, int copy)
|
|||
dev->desc = NULL;
|
||||
}
|
||||
|
||||
if (copy && desc) {
|
||||
dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
|
||||
if (dev->desc) {
|
||||
strcpy(dev->desc, desc);
|
||||
if (allocated && desc)
|
||||
dev->flags |= DF_DESCMALLOCED;
|
||||
}
|
||||
} else {
|
||||
/* Avoid a -Wcast-qual warning */
|
||||
dev->desc = (char *)(uintptr_t) desc;
|
||||
}
|
||||
dev->desc = __DECONST(char *, desc);
|
||||
|
||||
bus_data_generation_update();
|
||||
}
|
||||
|
@ -2029,7 +2022,24 @@ device_set_desc_internal(device_t dev, const char* desc, int copy)
|
|||
void
|
||||
device_set_desc(device_t dev, const char *desc)
|
||||
{
|
||||
device_set_desc_internal(dev, desc, FALSE);
|
||||
device_set_desc_internal(dev, desc, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the device's description
|
||||
*
|
||||
* A printf-like version of device_set_desc().
|
||||
*/
|
||||
void
|
||||
device_set_descf(device_t dev, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf = NULL;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vasprintf(&buf, M_BUS, fmt, ap);
|
||||
va_end(ap);
|
||||
device_set_desc_internal(dev, buf, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2041,7 +2051,10 @@ device_set_desc(device_t dev, const char* desc)
|
|||
void
|
||||
device_set_desc_copy(device_t dev, const char *desc)
|
||||
{
|
||||
device_set_desc_internal(dev, desc, TRUE);
|
||||
char *buf;
|
||||
|
||||
buf = strdup_flags(desc, M_BUS, M_NOWAIT);
|
||||
device_set_desc_internal(dev, buf, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -660,6 +660,7 @@ int device_quiesce(device_t dev);
|
|||
void device_quiet(device_t dev);
|
||||
void device_quiet_children(device_t dev);
|
||||
void device_set_desc(device_t dev, const char* desc);
|
||||
void device_set_descf(device_t dev, const char* fmt, ...) __printflike(2, 3);
|
||||
void device_set_desc_copy(device_t dev, const char* desc);
|
||||
int device_set_devclass(device_t dev, const char *classname);
|
||||
int device_set_devclass_fixed(device_t dev, const char *classname);
|
||||
|
|
Loading…
Reference in a new issue