KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
Utility Macros

General useful language macros More...

Macros

#define __build_assert(cond)    do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
 Assert a build-time dependency.
 
#define __build_assert_or_zero(cond)    (sizeof(char [1 - 2*!(cond)]) - 1)
 Assert a build-time dependency.
 
#define __array_size(arr)   (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
 Get the number of elements in a visible array.
 
#define _array_size_chk(arr)   0
 

Detailed Description

General useful language macros

This group contains definitions to help give robust solutions to common code patterns.

Macro Definition Documentation

◆ __array_size

#define __array_size ( arr)    (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))

Get the number of elements in a visible array.

This does not work on pointers, or arrays declared as [], or function parameters. With correct compiler support, such usage will cause a build error (

See also
__build_assert).
Parameters
arrThe array whose size you want.

◆ __build_assert

#define __build_assert ( cond)     do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)

Assert a build-time dependency.

Your compiler will fail if the condition isn't true, or can't be evaluated by the compiler. This can only be used within a function.

Example:

#include <stddef.h>
...
static char *foo_to_char(struct foo *foo)
{
// This code needs string to be at start of foo.
__build_assert(offsetof(struct foo, string) == 0);
return (char *)foo;
}
#define __build_assert(cond)
Assert a build-time dependency.
Definition cdefs.h:240
Parameters
condThe compile-time condition which must be true.
See also
__build_assert_or_zero

◆ __build_assert_or_zero

#define __build_assert_or_zero ( cond)     (sizeof(char [1 - 2*!(cond)]) - 1)

Assert a build-time dependency.

Your compiler will fail if the condition isn't true, or can't be evaluated by the compiler. This can be used in an expression: its value is "0".

Example:

#define foo_to_char(foo) \
((char *)(foo) \
+ __build_assert_or_zero(offsetof(struct foo, string) == 0))
Parameters
condThe compile-time condition which must be true.
See also
__build_assert

◆ _array_size_chk

#define _array_size_chk ( arr)    0