Support byte-sized enums

To that end add __enum_uint8_decl and __enum_uint8.

By default enums take 4 bytes, but vast majority of them have values
which would fit in a byte.

One can try to workaround the problem by using bitfields, like so:
enum some_small_enum foo:8;

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).

Both gcc 13 and clang support specifying the size, and for older
variants one can fallback to the "packed" attribute.

Names are mangled in order to avoid mix use with plain enum.

Reviewed by:
Differential Revision:	https://reviews.freebsd.org/D39031
This commit is contained in:
Mateusz Guzik 2023-03-12 19:29:20 +00:00
parent 9def8ea689
commit cebb8646c4

View File

@ -347,6 +347,18 @@ __makedev(int _Major, int _Minor)
((dev_t)(_Minor & 0xff00) << 24) | (_Minor & 0xffff00ff));
}
#if (defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 13))
#define __enum_uint8_decl(name) enum enum_ ## name ## _uint8 : uint8_t
#define __enum_uint8(name) enum enum_ ## name ## _uint8
#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