endian.h: Define uint{16,32,64}_t

The Draft Posix Issue 8 standard requires that these be defined. Define
them in the usual way that lets multiple headers define them. Opted to
not just use #include <stdint.h>, allowed by the draft, to be
conservative. Add notes about how we comply with Issue 8, and that we've
opted to define these only as macros, though the standard allows
functions, macros or both.

Sponsored by:		Netflix
This commit is contained in:
Warner Losh 2024-05-06 09:10:46 -06:00
parent e12b7446bd
commit 1314d14c32
2 changed files with 29 additions and 3 deletions

View File

@ -11,9 +11,33 @@
#ifndef _ENDIAN_H_ #ifndef _ENDIAN_H_
#define _ENDIAN_H_ #define _ENDIAN_H_
/*
* POSIX Issue 8 requires that endian.h define uint{16,32,64}_t. Although POSIX
* allows stdint.h symbols here, be conservative and only define there required
* ones. FreeBSD's sys/_endian.h doesn't need to expose those types since it
* implements all the [bl]eXtoh hto[bl]eX interfaces as macros calling builtin
* functions. POSIX allows functions, macros or both. We opt for macros only.
*/
#include <sys/_types.h>
#ifndef _UINT16_T_DECLARED
typedef __uint16_t uint16_t;
#define _UINT16_T_DECLARED
#endif
#ifndef _UINT32_T_DECLARED
typedef __uint32_t uint32_t;
#define _UINT32_T_DECLARED
#endif
#ifndef _UINT64_T_DECLARED
typedef __uint64_t uint64_t;
#define _UINT64_T_DECLARED
#endif
/* /*
* FreeBSD's sys/_endian.h is very close to the interface provided on Linux by * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
* glibc's endian.h. * glibc's endian.h as well as POSIX Issue 8's endian.h.
*/ */
#include <sys/_endian.h> #include <sys/_endian.h>

View File

@ -69,8 +69,10 @@
/* /*
* POSIX Issue 8 will require these for endian.h. Define them there and in the * POSIX Issue 8 will require these for endian.h. Define them there and in the
* traditional BSD compilation environment. Since issue 8 doesn't yet have an * traditional BSD compilation environment. PDP_ENDIAN isn't strictly in Issue
* assigned date, use strictly greater than issue 7's date. * 8, but is allowed as implementations can define any *_ENDIAN symbol. Since
* issue 8 doesn't yet have an assigned date, use strictly greater than issue
* 7's date.
*/ */
#if __BSD_VISIBLE || _POSIX_C_SOURCE > 200809 #if __BSD_VISIBLE || _POSIX_C_SOURCE > 200809
#define LITTLE_ENDIAN _LITTLE_ENDIAN #define LITTLE_ENDIAN _LITTLE_ENDIAN