bus: Add __BUS_ACCESSOR_DEFAULT

This macro is similar to __BUS_ACCESSOR in that it creates three
helper routines for an ivar, but the "get" wrapper returns a default
value if BUS_READ_IVAR does not return a value.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D55353
This commit is contained in:
John Baldwin
2026-02-24 15:29:40 -05:00
parent cb5a0eb053
commit b937f9bf75
+32 -14
View File
@@ -943,7 +943,7 @@ DECLARE_MODULE(_name##_##busname, _name##_##busname##_mod, \
/** /**
* Generic ivar accessor generation macros for bus drivers * Generic ivar accessor generation macros for bus drivers
*/ */
#define __BUS_ACCESSOR(varp, var, ivarp, ivar, type) \ #define __BUS_ACCESSOR_COMMON(varp, var, ivarp, ivar, type) \
\ \
static __inline bool \ static __inline bool \
varp ## _has_ ## var(device_t dev) \ varp ## _has_ ## var(device_t dev) \
@@ -956,19 +956,6 @@ varp ## _has_ ## var(device_t dev) \
return (e == 0); \ return (e == 0); \
} \ } \
\ \
static __inline type \
varp ## _get_ ## var(device_t dev) \
{ \
uintptr_t v = 0; \
int e __diagused; \
e = BUS_READ_IVAR(device_get_parent(dev), dev, \
ivarp ## _IVAR_ ## ivar, &v); \
KASSERT(e == 0, ("%s failed for %s on bus %s, error = %d", \
__func__, device_get_nameunit(dev), \
device_get_nameunit(device_get_parent(dev)), e)); \
return ((type) v); \
} \
\
static __inline void \ static __inline void \
varp ## _set_ ## var(device_t dev, type t) \ varp ## _set_ ## var(device_t dev, type t) \
{ \ { \
@@ -981,6 +968,37 @@ varp ## _set_ ## var(device_t dev, type t) \
device_get_nameunit(device_get_parent(dev)), e)); \ device_get_nameunit(device_get_parent(dev)), e)); \
} }
#define __BUS_ACCESSOR(varp, var, ivarp, ivar, type) \
__BUS_ACCESSOR_COMMON(varp, var, ivarp, ivar, type) \
\
static __inline type \
varp ## _get_ ## var(device_t dev) \
{ \
uintptr_t v = 0; \
int e __diagused; \
\
e = BUS_READ_IVAR(device_get_parent(dev), dev, \
ivarp ## _IVAR_ ## ivar, &v); \
KASSERT(e == 0, ("%s failed for %s on bus %s, error = %d", \
__func__, device_get_nameunit(dev), \
device_get_nameunit(device_get_parent(dev)), e)); \
return ((type) v); \
}
#define __BUS_ACCESSOR_DEFAULT(varp, var, ivarp, ivar, type, default) \
__BUS_ACCESSOR_COMMON(varp, var, ivarp, ivar, type) \
\
static __inline type \
varp ## _get_ ## var(device_t dev) \
{ \
uintptr_t v = 0; \
int e; \
\
e = BUS_READ_IVAR(device_get_parent(dev), dev, \
ivarp ## _IVAR_ ## ivar, &v); \
return (e == 0 ? (type) v : (default)); \
}
struct device_location_cache; struct device_location_cache;
typedef struct device_location_cache device_location_cache_t; typedef struct device_location_cache device_location_cache_t;
device_location_cache_t *dev_wired_cache_init(void); device_location_cache_t *dev_wired_cache_init(void);