<cahute/cdefs.h>
– Basic definitions for Cahute#
This header declares basic definitions for Cahute.
Macro definitions#
-
CAHUTE_PREREQ(MAJOR, MINOR)#
Macro that returns whether the current version of Cahute is compatible with the provided version of Cahute.
For example,
CAHUTE_PREREQ(2, 4)
checks if the current version of Cahute is compatible with version 2.4.
-
OF(...)#
Macro for defining parameter definitions in a function declaration. For example:
int my_function OF((int arg1, char const *arg2));
This is present because in K&R C (pre-C89), function declarations did not include parameters, i.e. the function declaration above would need to render as the following:
int my_function();
-
CAHUTE_EXTERN(TYPE)#
Macro to use in Cahute function declarations, surrounding the return type, for compatibility. For example:
CAHUTE_EXTERN(int) my_function(int arg1, char const *arg2);
This is used to add calling conventions or other platform-specific options in some cases, such as, on 16-bit x86 where the calling convention was best described explicitely.
Example outputs of the above are the following:
int my_function(int arg1, char const *arg2); /* Default. */ _stdcall int my_function(int arg1, char const *arg2); /* WINAPI x86 */ int __cdecl my_function(int arg1, char const *arg2); /* GCC x86 */ extern __declspec(dllexport) int my_function(int arg1, char const *arg2); /* Borland C */
-
CAHUTE_LOCAL(TYPE)#
Macro to use in Cahute function definitions, surrounding the return type, as opposed to
CAHUTE_EXTERN
. For example:CAHUTE_LOCAL(int) my_local_utility(int arg1, char const *arg2);
For now, this only produces the following output:
static int my_local_utility(int arg1, char const *arg2);
-
CAHUTE_INLINE(TYPE)#
Macro to use in inlinable local Cahute function definitions, surrounding the return type. This macro extends on
CAHUTE_LOCAL
’s meaning, by making the function inlinable if the compiler is so inclined.For example:
CAHUTE_INLINE(int) my_tiny_utility(int arg1, char const *arg2);
This can then use compiler-specific functions, such as GCC’s
always_inline
attribute; see GCC function attributes for more information.
-
CAHUTE_LOCAL_DATA(TYPE)#
Macro to use in local data in Cahute source files, surrounding the variable type, for example:
CAHUTE_LOCAL_DATA(char const *) my_string = "hello, world";
For now, this only produces the following output:
static char const * my_string = "hello, world";
-
CAHUTE_DEPRECATED#
Function attribute, placed before the return type and
CAHUTE_EXTERN
, that indicates that the compiler should warn that the function is deprecated when compiling a user program or library.For example:
CAHUTE_DEPRECATED int my_deprecated_function(int a, int b);
-
CAHUTE_WUR#
Function attribute, placed before the return type and
CAHUTE_EXTERN
, that indicates that the compiler should warn in case of unused result, i.e. if the caller does not store nor process the resulting value from the function.For example:
CAHUTE_WUR resource *create_resource(int a, int b);
-
CAHUTE_NNPTR(NAME)#
Function parameter attribute that indicate that the passed value should not be
NULL
. It must be used in both the function declaration and definition. For example:int my_function(char const CAHUTE_NNPTR(my_string))
Calling this with an explicitely NULL pointer will raise a compiler warning or error.
This may resolve as:
int my_function(char const *my_string); /* Default. */ int my_function(char const my_string[static 1]); /* C99. */
For maximum compatibility, this macro must be used with
CAHUTE_NONNULL
.
-
CAHUTE_NONNULL(INDEXES)#
Indicate, as an attribute, that one or more of the function arguments should not be passed as NULL. For example:
int my_function(int *a, int *b, int *c) CAHUTE_NONNULL((1, 3));
Calling
my_function
with a NULL pointer fora
orc
will raise a compiler warning or error.This may resolve as:
int my_function(int *a, int *b, int *c); /* Default. */ int my_function(int *a, int *b, int *c) __attribute__((nonnull (1, 3))); /* Pre-C99 GCC. */
For maximum compatibility, this macro must be used with
CAHUTE_NNPTR
.
-
CAHUTE_PRIu16#
printf specifier for displaying
cahute_u16
in decimal form, e.g.hu
.
-
CAHUTE_PRIu32#
printf specifier for displaying
cahute_u32
in decimal form, e.g.u
.
-
CAHUTE_PRIuSIZE#
printf specifier for displaying
size_t
in decimal form, e.g.zu
.
-
CAHUTE_PRIx16#
printf specifier for displaying
cahute_u16
in lowercase hexadecimal form, e.g.hx
.
-
CAHUTE_PRIx32#
printf specifier for displaying
cahute_u32
in lowercase hexadecimal form, e.g.x
.
-
CAHUTE_PRIxSIZE#
printf specifier for displaying
size_t
in lowercase hexadecimal form, e.g.zx
.
-
CAHUTE_PRIX16#
printf specifier for displaying
cahute_u16
in uppercase hexadecimal form, e.g.hX
.
-
CAHUTE_PRIX32#
printf specifier for displaying
cahute_u32
in uppercase hexadecimal form, e.g.X
.
-
CAHUTE_PRIXSIZE#
printf specifier for displaying
size_t
in uppercase hexadecimal form, e.g.zX
.
Type definitions#
-
type cahute_u8#
Unsigned 8-bit integer type.
Available printf specifiers for this type are
CAHUTE_PRIu8
,CAHUTE_PRIx8
andCAHUTE_PRIX8
.
-
type cahute_u16#
Unsigned 16-bit integer type.
Available printf specifiers for this type are
CAHUTE_PRIu16
,CAHUTE_PRIx16
andCAHUTE_PRIX16
.
-
type cahute_u32#
Unsigned 32-bit integer type.
Available printf specifiers for this type are
CAHUTE_PRIu32
,CAHUTE_PRIx32
andCAHUTE_PRIX32
.
Function declarations#
-
cahute_u16 cahute_be16toh(cahute_u16 value)#
Convert a 16-bit unsigned integer from big endian to host endianness.
- Parameters:
value – 16-bit unsigned integer in big endian.
- Returns:
16-bit unsigned integer in host endianness.
-
cahute_u16 cahute_le16toh(cahute_u16 value)#
Convert a 16-bit unsigned integer from little endian to host endianness.
- Parameters:
value – 16-bit unsigned integer in little endian.
- Returns:
16-bit unsigned integer in host endianness.
-
cahute_u32 cahute_be32toh(cahute_u32 value)#
Convert a 32-bit unsigned integer from big endian to host endianness.
- Parameters:
value – 32-bit unsigned integer in big endian.
- Returns:
32-bit unsigned integer in host endianness.
-
cahute_u32 cahute_le32toh(cahute_u32 value)#
Convert a 32-bit unsigned integer from little endian to host endianness.
- Parameters:
value – 32-bit unsigned integer in little endian.
- Returns:
32-bit unsigned integer in host endianness.
-
cahute_u16 cahute_htobe16(cahute_u16 value)#
Convert a 16-bit unsigned integer from host endianness to big endian.
- Parameters:
value – 16-bit unsigned integer in host endianness.
- Returns:
16-bit unsigned integer in big endian.
-
cahute_u16 cahute_htole16(cahute_u16 value)#
Convert a 16-bit unsigned integer from host endianness to little endian.
- Parameters:
value – 16-bit unsigned integer in host endianness.
- Returns:
16-bit unsigned integer in little endian.
-
cahute_u32 cahute_htobe32(cahute_u32 value)#
Convert a 32-bit unsigned integer from host endianness to big endian.
- Parameters:
value – 32-bit unsigned integer in host endianness.
- Returns:
32-bit unsigned integer in big endian.
-
cahute_u32 cahute_htole32(cahute_u32 value)#
Convert a 32-bit unsigned integer from host endianness to little endian.
- Parameters:
value – 32-bit unsigned integer in host endianness.
- Returns:
32-bit unsigned integer in little endian.