Skip to content

Commit 6b6914c

Browse files
committed
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
1 parent 45cd294 commit 6b6914c

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

share/man/man9/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ MLINKS+=device_get_sysctl.9 device_get_sysctl_ctx.9 \
10121012
MLINKS+=device_quiet.9 device_is_quiet.9 \
10131013
device_quiet.9 device_verbose.9
10141014
MLINKS+=device_set_desc.9 device_get_desc.9 \
1015+
device_set_desc.9 device_set_descf.9 \
10151016
device_set_desc.9 device_set_desc_copy.9
10161017
MLINKS+=device_set_flags.9 device_get_flags.9
10171018
MLINKS+=devstat.9 devicestat.9 \

share/man/man9/device_set_desc.9

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2727
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
.\"
29-
.Dd June 16, 1998
29+
.Dd January 9, 2024
3030
.Dt DEVICE_SET_DESC 9
3131
.Os
3232
.Sh NAME
3333
.Nm device_set_desc ,
34+
.Nm device_set_descf ,
3435
.Nm device_set_desc_copy ,
3536
.Nm device_get_desc
3637
.Nd access the description of a device
@@ -40,6 +41,8 @@
4041
.Ft void
4142
.Fn device_set_desc "device_t dev" "const char *desc"
4243
.Ft void
44+
.Fn device_set_descf "device_t dev" "const char *fmt" "..."
45+
.Ft void
4346
.Fn device_set_desc_copy "device_t dev" "const char *desc"
4447
.Ft const char *
4548
.Fn device_get_desc "device_t dev"
@@ -54,6 +57,9 @@ is used to set the description if the string passed is a temporary
5457
buffer which will be overwritten.
5558
In this case, the system will copy
5659
the string, otherwise the pointer passed will be used directly.
60+
.Fn device_set_descf
61+
is a printf-like version of
62+
.Fn device_set_desc .
5763
.Sh SEE ALSO
5864
.Xr device 9
5965
.Sh AUTHORS

sys/kern/subr_bus.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,24 +1997,17 @@ device_log(device_t dev, int pri, const char * fmt, ...)
19971997
* @internal
19981998
*/
19991999
static void
2000-
device_set_desc_internal(device_t dev, const char* desc, int copy)
2000+
device_set_desc_internal(device_t dev, const char *desc, bool allocated)
20012001
{
20022002
if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
20032003
free(dev->desc, M_BUS);
20042004
dev->flags &= ~DF_DESCMALLOCED;
20052005
dev->desc = NULL;
20062006
}
20072007

2008-
if (copy && desc) {
2009-
dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2010-
if (dev->desc) {
2011-
strcpy(dev->desc, desc);
2012-
dev->flags |= DF_DESCMALLOCED;
2013-
}
2014-
} else {
2015-
/* Avoid a -Wcast-qual warning */
2016-
dev->desc = (char *)(uintptr_t) desc;
2017-
}
2008+
if (allocated && desc)
2009+
dev->flags |= DF_DESCMALLOCED;
2010+
dev->desc = __DECONST(char *, desc);
20182011

20192012
bus_data_generation_update();
20202013
}
@@ -2027,9 +2020,26 @@ device_set_desc_internal(device_t dev, const char* desc, int copy)
20272020
* call to device_set_desc() or device_set_desc_copy()).
20282021
*/
20292022
void
2030-
device_set_desc(device_t dev, const char* desc)
2023+
device_set_desc(device_t dev, const char *desc)
2024+
{
2025+
device_set_desc_internal(dev, desc, false);
2026+
}
2027+
2028+
/**
2029+
* @brief Set the device's description
2030+
*
2031+
* A printf-like version of device_set_desc().
2032+
*/
2033+
void
2034+
device_set_descf(device_t dev, const char *fmt, ...)
20312035
{
2032-
device_set_desc_internal(dev, desc, FALSE);
2036+
va_list ap;
2037+
char *buf = NULL;
2038+
2039+
va_start(ap, fmt);
2040+
vasprintf(&buf, M_BUS, fmt, ap);
2041+
va_end(ap);
2042+
device_set_desc_internal(dev, buf, true);
20332043
}
20342044

20352045
/**
@@ -2039,9 +2049,12 @@ device_set_desc(device_t dev, const char* desc)
20392049
* the device description is generated, (e.g. with sprintf()).
20402050
*/
20412051
void
2042-
device_set_desc_copy(device_t dev, const char* desc)
2052+
device_set_desc_copy(device_t dev, const char *desc)
20432053
{
2044-
device_set_desc_internal(dev, desc, TRUE);
2054+
char *buf;
2055+
2056+
buf = strdup_flags(desc, M_BUS, M_NOWAIT);
2057+
device_set_desc_internal(dev, buf, true);
20452058
}
20462059

20472060
/**

sys/sys/bus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ int device_quiesce(device_t dev);
660660
void device_quiet(device_t dev);
661661
void device_quiet_children(device_t dev);
662662
void device_set_desc(device_t dev, const char* desc);
663+
void device_set_descf(device_t dev, const char* fmt, ...) __printflike(2, 3);
663664
void device_set_desc_copy(device_t dev, const char* desc);
664665
int device_set_devclass(device_t dev, const char *classname);
665666
int device_set_devclass_fixed(device_t dev, const char *classname);

0 commit comments

Comments
 (0)