[PATCH v2 00/20] Add memberof(), split headers, and simplify code

From: Alejandro Colomar
Date: Sat Nov 20 2021 - 08:01:20 EST



Hi all,

I splitted some macros into separate headers,
to be able to use them
without pulling too many deps.

I also simplified some of themr
to be implemented in terms of the others
and to remove some unnecessary explicit casts.

And I added memberof(),
which gives name to a typical construction
to get the member of a struct
without needing a variable of that type.


The next step after this patch set
is another one removing all redefinitions
(at least all that are possible,
since these headers can't be included everywhere)
of these macros,
by including these new tiny headers.
Since these headers are so tiny and bring no dependencies,
they should break anything.

It was hard for me to get this working
because the order of includes _matters a lot_,
and which headers you include _matters_ even outside of uapi.
So I think this should help fix that,
by allowing headers to pull exactly what they want,
without all of the stuff that came with
<linux/compiler.h>
<linux/compiler_types.h>
<linux/stddef.h>.

I already have much of the next patch set ready,
and it removes hundreds of redefinitions of these macros,
which should be a good thing.


Then,
when there are (almost) no redefinitions of these macros,
I'll prepare a 3rd patch set that
explicitly includes these tiny headers
wherever these macros were already in use,
to allow for removal of other bigger headers
(although I won't remove anything,
to avoid silently breaking anything).


And then,
a 4th patch set will
attempt to find all uses of these macros
that were not even named
(i.e., hard-coded sizeof divisions).


Hope this is clear and
that you like these changes.


Cheers,
Alex


Alejandro Colomar (20):
linux/stddef.h, linux/offsetof.h: Split offsetof() into a separate
header
linux/stddef.h, linux/sizeof_field.h: Split sizeof_field() into a
separate header
linux/stddef.h, linux/offsetofend.h: Split offsetofend() into a
separate header
linux/stddef.h, linux/NULL.h: Split NULL into a separate header
linux/container_of.h, linux/typeof_member.h: Split typeof_member()
into a separate header
linux/kernel.h, linux/array_size.h: Split ARRAY_SIZE() into a separate
header
linux/memberof.h: Add memberof(T, m) macro
Implement container_of_safe() in terms of container_of()
Implement offsetof(), sizeof_member(), typeof_member(), and
container_of() in terms of memberof()
linux/container_of.h: Remove unnecessary cast
linux/container_of.h: Cosmetic
linux/must_be.h: Add must_be() to improve readability of
BUILD_BUG_ON_ZERO()
linux/build_bug.h, linux/must_be.h: Move BUILD_BUG_ON_ZERO to
<linux/must_be.h>
linux/compiler_types.h, linux/same_type.h: Split __same_type() to a
separate header
linux/compiler.h: Implement __must_be_array() in terms of __must_be()
linux/compiler.h, linux/array_size.h: Move __must_be_array() into
<linux/array_size.h>
linux/array_size.h: Add __is_array(a) to help readability
linux/power_of_2.h: Add __IS_POWER_OF_2(n) and __IS_POWER_OF_2_OR_0(n)
macros
linux/build_bug.h, linux/power_of_2.h: Move
[__]BUILD_BUG_ON_NOT_POWER_OF_2() to <linux/power_of_2.h>
linux/power_of_2.h: Implement [__]BUILD_BUG_ON_NOT_POWER_OF_2() in
terms of __IS_POWER_OF_2[_OR_0]()

drivers/gpu/drm/i915/i915_utils.h | 3 ++
drivers/net/can/usb/etas_es58x/es58x_core.h | 3 ++
include/linux/NULL.h | 10 +++++++
include/linux/array_size.h | 21 ++++++++++++++
include/linux/build_bug.h | 20 ++-----------
include/linux/compiler.h | 3 --
include/linux/compiler_types.h | 7 +++--
include/linux/container_of.h | 25 ++++++++--------
include/linux/kernel.h | 9 ++----
include/linux/memberof.h | 12 ++++++++
include/linux/must_be.h | 21 ++++++++++++++
include/linux/offsetof.h | 19 ++++++++++++
include/linux/offsetofend.h | 19 ++++++++++++
include/linux/overflow.h | 3 ++
include/linux/power_of_2.h | 17 +++++++++++
include/linux/same_type.h | 10 +++++++
include/linux/sizeof_field.h | 18 ++++++++++++
include/linux/stddef.h | 32 ++++-----------------
include/linux/typeof_member.h | 12 ++++++++
19 files changed, 197 insertions(+), 67 deletions(-)
create mode 100644 include/linux/NULL.h
create mode 100644 include/linux/array_size.h
create mode 100644 include/linux/memberof.h
create mode 100644 include/linux/must_be.h
create mode 100644 include/linux/offsetof.h
create mode 100644 include/linux/offsetofend.h
create mode 100644 include/linux/power_of_2.h
create mode 100644 include/linux/same_type.h
create mode 100644 include/linux/sizeof_field.h
create mode 100644 include/linux/typeof_member.h

--
2.33.1