Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Jul 2023 16:42:14 +0100
From:      Jessica Clarke <jrtc27@freebsd.org>
To:        Mateusz Guzik <mjg@FreeBSD.org>
Cc:        "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org>
Subject:   Re: git: cebb8646c4ee - main - Support byte-sized enums
Message-ID:  <0AD6BA4D-E6B1-4C56-B7B7-A85D57188A34@freebsd.org>
In-Reply-To: <202307051538.365FcoZR017983@gitrepo.freebsd.org>
References:  <202307051538.365FcoZR017983@gitrepo.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 5 Jul 2023, at 16:38, Mateusz Guzik <mjg@FreeBSD.org> wrote:
>=20
> The branch main has been updated by mjg:
>=20
> URL: =
https://cgit.FreeBSD.org/src/commit/?id=3Dcebb8646c4ee559aedcbc1b27bf30faa=
70a1f716
>=20
> commit cebb8646c4ee559aedcbc1b27bf30faa70a1f716
> Author:     Mateusz Guzik <mjg@FreeBSD.org>
> AuthorDate: 2023-03-12 19:29:20 +0000
> Commit:     Mateusz Guzik <mjg@FreeBSD.org>
> CommitDate: 2023-07-05 14:46:30 +0000
>=20
>    Support byte-sized enums
>=20
>    To that end add __enum_uint8_decl and __enum_uint8.
>=20
>    By default enums take 4 bytes, but vast majority of them have =
values
>    which would fit in a byte.
>=20
>    One can try to workaround the problem by using bitfields, like so:
>    enum some_small_enum foo:8;
>=20
>    but that's ugly and runs into trouble when using atomic_load (you =
can't
>    take an address of a bitfield, even if it is sized to a multiply of =
a
>    byte).
>=20
>    Both gcc 13 and clang support specifying the size, and for older
>    variants one can fallback to the "packed" attribute.
>=20
>    Names are mangled in order to avoid mix use with plain enum.
>=20
>    Reviewed by:
>    Differential Revision:  https://reviews.freebsd.org/D39031
> ---
> sys/sys/types.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>=20
> diff --git a/sys/sys/types.h b/sys/sys/types.h
> index 0846f2a57670..c6f7abe1f524 100644
> --- a/sys/sys/types.h
> +++ b/sys/sys/types.h
> @@ -347,6 +347,18 @@ __makedev(int _Major, int _Minor)
>    ((dev_t)(_Minor & 0xff00) << 24) | (_Minor & 0xffff00ff));
> }
>=20
> +#if (defined(__clang__) || (defined(__GNUC__) && __GNUC__ >=3D 13))
> +#define __enum_uint8_decl(name) enum enum_ ## name ## _uint8 : =
uint8_t
> +#define __enum_uint8(name) enum enum_ ## name ## _uint8

What happened to zlei@=E2=80=99s feedback about making it so =
__enum_uint8 isn=E2=80=99t
needed everywhere?

Jess

> +#else
> +/*
> + * Note: there is no real size checking here, but the code below can =
be
> + * removed once we require GCC 13.
> + */
> +#define __enum_uint8_decl(name) enum __attribute__((packed)) enum_ ## =
name ## _uint8
> +#define __enum_uint8(name) enum __attribute__((packed)) enum_ ## name =
## _uint8
> +#endif
> +
> /*
>  * These declarations belong elsewhere, but are repeated here and in
>  * <stdio.h> to give broken programs a better chance of working with




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?0AD6BA4D-E6B1-4C56-B7B7-A85D57188A34>