diff options
| author | John Baldwin <jhb@FreeBSD.org> | 2026-02-24 20:29:40 +0000 |
|---|---|---|
| committer | John Baldwin <jhb@FreeBSD.org> | 2026-02-24 20:29:40 +0000 |
| commit | b937f9bf750907602606691dd92fb6d70e9f88da (patch) | |
| tree | 6a725ed3cb6d2d303970349f4519560b6d6d157e | |
| parent | cb5a0eb05310d06e9853a01a542be55b9adee038 (diff) | |
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
| -rw-r--r-- | sys/sys/bus.h | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/sys/sys/bus.h b/sys/sys/bus.h index 0d1baaafb4cd..a2e315bb9dee 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -943,7 +943,7 @@ DECLARE_MODULE(_name##_##busname, _name##_##busname##_mod, \ /** * 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 \ varp ## _has_ ## var(device_t dev) \ @@ -956,29 +956,47 @@ varp ## _has_ ## var(device_t dev) \ return (e == 0); \ } \ \ +static __inline void \ +varp ## _set_ ## var(device_t dev, type t) \ +{ \ + uintptr_t v = (uintptr_t) t; \ + int e __diagused; \ + e = BUS_WRITE_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)); \ +} + +#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 void \ -varp ## _set_ ## var(device_t dev, type t) \ +static __inline type \ +varp ## _get_ ## var(device_t dev) \ { \ - uintptr_t v = (uintptr_t) t; \ - int e __diagused; \ - e = BUS_WRITE_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)); \ + 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; |
