Core (core)#

group core

Definitions used by every component: status codes, memory allocation, flags, function preconditions, and compiler helpers.

rcsw/core/alloc.h#

The single point of memory allocation in RCSW.

Functions

void *rcsw_alloc(void *ptr, size_t n_bytes, uint32_t flags)#

Allocate n_bytes of memory using malloc()/calloc()/etc.

This function is the one place in RCSW where memory allocation is done, making it easy and clean for RCSW to be built to:

  • Never use malloc() for anything (i.e., pass RCSW_NOALLOC_ALL for all modules). Useful in applications where malloc() is disallowed/not available, such as space and bootstraps without stdlib.

  • Zero allocated memory (i.e., behave as calloc() and set RCSW_ZALLOC for all modules).

Parameters:
  • ptr – Pointer to n_bytes of application-allocated memory. Can be NULL if malloc()/calloc() is to be used.

  • n_bytes – The number of bytes to allocate.

  • flags – Run-time flags. Valid flags are:

void rcsw_free(void *ptr, uint32_t flags)#

Free memory previously allocated with rcsw_alloc().

Nothing is freed if flags contains the RCSW_NOALLOC_* flag for the region ptr belongs to, since that memory is the caller’s; callers pass the module’s flags masked to that one flag.

Parameters:
  • ptr – The memory to free.

  • flags – Non-zero if ptr is caller-provided and must not be freed.

rcsw/core/compilers.h#

Compiler portability: attributes, warning control, symbol visibility, and atomics.

Defines

RCSW_PRAGMA(X)#

Indirection layer to pass a token sequence to _Pragma as a string.

RCSW_WARNING_DISABLE_PUSH()#

Save the compiler’s diagnostic state. Pair with RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_POP()#

Restore the diagnostic state saved by the matching RCSW_WARNING_DISABLE_PUSH().

RCSW_WARNING_DISABLE(X)#

Suppress the compiler diagnostic named X (e.g. -Wfloat-equal) until the matching RCSW_WARNING_DISABLE_POP().

Prefer the named helpers below (RCSW_WARNING_DISABLE_QUAL(), …), which use the right diagnostic name for each compiler and expand to nothing where it doesn’t have the warning.

RCSW_WARNING_DISABLE_REDUNDANT_DECLS(...)#

Suppress warnings about redundant declarations (GCC only). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_QUAL(...)#

Suppress warnings about discarding qualifiers (e.g. const). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_VLA(...)#

Suppress warnings about variable-length arrays. Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_FUNC_CAST(...)#

Suppress warnings about casting a function call result (GCC only). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_DOCUMENTATION(...)#

Suppress warnings about malformed documentation comments (Clang only). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_STRICT_PROTO(...)#

Suppress warnings about non-prototype declarations (GCC, C only). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_WARNING_DISABLE_FLOAT_EQUAL(...)#

Suppress warnings about comparing floating-point values for equality (GCC only). Use between RCSW_WARNING_DISABLE_PUSH() and RCSW_WARNING_DISABLE_POP().

RCSW_ISR#

Mark a function as an interrupt service routine.

RCSW_SECTION(SECTION)#

Place a symbol into the named linker section SECTION.

RCSW_EXPORT#

Mark a symbol as publicly visible in a shared library build.

RCSW_HIDDEN#

Mark a symbol as private (not exported) in a shared library build.

RCSW_LIB_INIT#

Mark a function to be run automatically when the library is loaded.

RCSW_LIB_FINI#

Mark a function to be run automatically when the library is unloaded.

RCSW_NORETURN#

Mark a function that never returns (e.g. calls abort() or loops forever).

RCSW_WEAK#

Mark a function as a weak symbol, allowing it to be overridden at link time by a strong definition in another translation unit.

RCSW_LIKELY(x)#

Hint to the compiler that the condition x is almost always true. Use sparingly and only where profiling confirms it matters.

RCSW_UNLIKELY(x)#

Hint to the compiler that the condition x is almost always false. Use sparingly and only where profiling confirms it matters.

RCSW_ATTR_PRINTF(...)#
RCSW_API#
RCSW_LOCAL#
__FILE_NAME__#

Only recent GCC versions provide __FILE_NAME__ (basename only). Fall back to __FILE__ on older versions.

typeof#
RCSW_ATOMIC_LOAD_ACQ(PTR)#

Atomically load *PTR with acquire ordering.

RCSW_ATOMIC_STORE_REL(PTR, VAL)#

Atomically store VAL to *PTR with release ordering.

RCSW_ATOMIC_XCHG_ACQ(PTR, VAL)#

Atomically store VAL to *PTR and return the previous value, with acquire ordering.

rcsw/core/core.h#

Definitions, etc. common to all of RCSW.

Defines

RCSW_E9#

1E9

RCSW_E6#

1E6

RCSW_E3#

1E3

RCSW_XSTR(X)#

Use when you need to stringify the result of a macro expansion.

RCSW_JOIN(x, y)#

Use when you need to join two tokens x and y together.

RCSW_JOIN3(x, y, z)#
RCSW_UNIQUE_ID(prefix)#

Generate a translation unit unique identifier using __COUNTER__.

RCSW_MIN(a, b)#

Returns a type-same minimum of its arguments (compiler warnings for unsafe comparisons of different types).

RCSW_MIN3(a, b, c)#

Returns a type-same minimum of its arguments (compiler warnings for unsafe comparisons of different types).

RCSW_MAX(a, b)#

Returns a type-same maximum of its arguments (compiler warnings for unsafe comparisons of different types).

RCSW_MAX3(a, b, c)#

Returns a type-same maximum of its arguments (compiler warnings for unsafe comparisons of different types).

RCSW_IS_ODD(n)#

Readability macro for determining if something is odd.

You can obviously do this without a macro, but this is (1) more self documenting, and (2) less error prone.

RCSW_IS_EVEN(n)#

Readability macro for determining if something is even.

You can obviously do this without a macro, but this is (1) more self documenting, and (2) less error prone.

RCSW_IS_BETWEENC(n, low, high)#

Readability macro for determining if something is between an upper and lower bound (closed=inclusive).

You can obviously do this without a macro, but this is (1) more self documenting, and (2) less error prone.

RCSW_IS_BETWEENHO(n, low, high)#

Readability macro for determining if something is between an upper and lower bound (half open=includes lb but not ub).

You can obviously do this without a macro, but this is (1) more self documenting, and (2) less error prone.

RCSW_CLAMP(val, min, max)#

Clamp a value between two setpoints.

Safe against double evaluation of any argument: each of val, min, and max is evaluated exactly once. A compiler warning is issued if the three arguments do not share the same type (same policy as RCSW_MIN and RCSW_MAX).

RCSW_ARRAY_ELTS(arr)#

Get the size of an array (NOT a pointer to an array) in units (not bytes).

Note

If you try this on arrays allocated dynamically (VLAs, heap,…) it will not work.

RCSW_CONTAINER_OF(ptr, type, member)#

Mostly stolen from the linux kernel. VERY cool macro for getting a pointer to a struct given a pointer to a member within the struct.

RCSW_FIELD_SIZEOF(t, f)#

Get the size of a field in a struct t named f.

RCSW_CHECK(cond)#

If condition is not true, go to the error/bailout section for function (you must have a label called error in your function). Use this when the function has cleanup work to do on failure (freeing memory, closing handles, etc.).

RCSW_CHECK_PTR(ptr)#

If ptr is NULL, go to the error/bailout section for function (you must have a label called error in your function).

RCSW_CHECK_FD(fd)#

If the descriptor fd is invalid (i.e. < 0), go to the error/bailout section for function (you must have a label called error in your function).

RCSW_XTABLE_STR(...)#

If you have a define like this:

#define myents A,B,C,D,E,F,G,H

Then you can use this macro to generate an array/table of strings like so:

char * foo [] = { RCSW_XTABLE_STR(myents) };

which will result in:

char* foo[] = {"A", "B", "C", "D", "E", "F", "G", "H"}

If you need more than 50 entries in a table, then you can do:

char * foo [] = {
                  RCSW_XTABLE_STR(myents1)
                  RCSW_XTABLE_STR(myents2)
                  RCSW_XTABLE_STR(myents3)
                  ...
                };

in order to get the # of entries you need.

RCSW_XTABLE_SEQ_ENUM(...)#

If you have a define like this:

#define myents A,B,C,D,E,F,G,H

Then you can use this macro to generate an enum like so:

enum foo {RCSW_XTABLE_SEQ_ENUM(myents) };

which will result in:

enum foo {A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7}

If you need more than 50 entries, then you can do:

enum foo {
           RCSW_XTABLE_SEQ_ENUM(myents1)
           RCSW_XTABLE_SEQ_ENUM(myents2)
           RCSW_XTABLE_SEQ_ENUM(myents3)
};

in order to get the # of entries you need.

RCSW_STATIC_ASSERT(...)#

Compile time asserts for size, alignment, etc.

RCSW requires C11 (or C++11), which provide static assertions natively.

Enums

enum status_t#

A C status type.

The basic unit of determining if a function has succeeded or not.

Values:

enumerator OK#

Return this on function success.

enumerator ERROR#

Return this when a function fails.

rcsw/core/flags.h#

Flags shared by every RCSW module.

Defines

RCSW_NONE#

Declare that no flags are passed.

RCSW_NOALLOC_HANDLE#

Declare that space for some structure is provided by the application.

Applicable to xx_init() functions with an xx_in parameter which is ignored if you do not pass this flag (even if it is non-NULL).

Applicable to all RCSW modules. Whether or not it applies to a specific part of a module will be documented for each module.

RCSW_NOALLOC_DATA#

Declare that the space for the data (elements) the module will directly manage is provided by the application.

If passed, you must provide a handle to the application-allocated space as a parameter.

Applicable to xx_init() functions in all RCSW modules. Whether or not it applies to a specific part of a module will be documented for each module.

RCSW_NOALLOC_META#

Declare that space for the metadata the module needs to manage the actual data is provided by the application.

If passed, you must provide a handle to the application-allocated space as a parameter.

Applicable to xx_init() functions in all RCSW modules. Whether or not it applies to a specific part of a module will be documented for each module.

RCSW_NOALLOC_ALL#

Convenience constant for callers that provide all necessary memory.

RCSW_ZALLOC#

Declare that any allocated space, regardless of where it comes from, should be zeroed before use.

Applicable to all modules which do memory allocation via rcsw_alloc().

If dynamic memory allocation is allowed, this causes it to use calloc() instead of malloc(). It memory allocation is disallowed, then provided application-allocated memory will be zeroed via memset().

RCSW_MODFLAGS_SHIFT#

Necessary bit shift to get to unused bits for RCSW configuration flags.

RCSW_MODFLAGS_BASE#

Used by modules in RCSW to start their list of bitmask flags.

Prevents conflicts with common flags broadly applicable everywhere.

rcsw/core/fpc.h#

Function precondition/post-condition definitions (very useful!).

Allows you to define a set of conditions that must be met for a function to proceed (preconditions) or that must be true when it returns (post conditions). If a condition is not met:

Defines

RCSW_FPC#

The configured FPC definition.

RCSW_FPC_RETURN#

Indicate that failure of FPC should cause a return of an error value.

RCSW_FPC_ABORT#

Indicate that failure of FPC should fail an assert(), which aborts the program unless NDEBUG is defined.

RCSW_FPC_NONE#

Indicate that failure of FPC should be ignored.

RCSW_FPC_RET_NV(X, v)#

Check a single function pre/post condition X, returning a value v if the condition is not met. Requires that the function does not return void. The value to return must be convertible to the return type of the function.

This macro can be used to unconditionally return if a precondition fails, rather than the behavior of RCSW_FPC_NV, which is dependent on the value of RCSW_FPC.

RCSW_FPC_RET_V(X)#

Check a single function pre/post condition X, returning if the condition is not met. Requires that the function returns void.

This macro can be used to unconditionally return if a precondition fails, rather than the behavior of RCSW_FPC_NV, which is dependent on the value of RCSW_FPC.

RCSW_FPC_ASSERT(X)#

Check a single function pre/post condition with assert(), halting the program if the condition X fails. Nothing is checked if NDEBUG is defined.

RCSW_FPC_ABORT_NV(X)#

Check a single function pre/post condition X with RCSW_FPC_ASSERT, regardless of RCSW_FPC (whereas RCSW_FPC_NV depends on it). Nothing is checked if NDEBUG is defined.

RCSW_FPC_ABORT_V(X)#

Check a single function pre/post condition X with RCSW_FPC_ASSERT, regardless of RCSW_FPC (whereas RCSW_FPC_V depends on it). Nothing is checked if NDEBUG is defined.

RCSW_FPC_NV(v, ...)#

Function pre/post condition macro.

It performs short circuit evaluation, in that if the check for precondition i-1 fails, conditions [i,n] are not checked, and the function returns/program exits (depending on configuration).

v is returned if parameter validation fails. Do not use this macro in functions that return void.

… is a list of conditions that need to be met before the function can execute/must be true when the function returns.

RCSW_FPC_V(...)#

Function pre/post condition macro for functions that return void.

It performs short circuit evaluation, in that if the check for precondition i-1 fails, conditions [i,n] are not checked, and the function returns/program exits (depending on configuration).

… is a list of conditions that need to be met before the function can execute/must be true when the function returns.

rcsw/core/variadics.h#

Macros for iterating over and counting variadic macro arguments.

Defines

RCSW_XFOR_EACH1(action, ...)#

Call action on each item in the varargs list.

action needs to have the following signature (as a macro or function):

mymacro(X)

where X is the current item from the varargs list.

RCSW_XFOR_EACH1_NOTAIL(action, ...)#

Call action on each item in the varargs list.

action needs to have the following signature (as a macro or function):

mymacro(X)

where X is the current item from the varargs list. Identical to RCSW_XFOR_EACH1 EXCEPT the last item in the varargs list will NOT have the action called on it. This results in:

"foo" "_" "bar" "_" "baz"

generated from

foo,bar,baz
. Using RCSW_XFOR_EACH1 you would get:

"foo" "_" "bar" "_" "baz" "_"
RCSW_XFOR_EACH2(action, v, ...)#

Call action on each item in the varargs list.

An additional argument v is passed to each invocation, so action needs to have the following signature (as a macro or function):

mymacro(X, v)

where X is the current item from the varargs list, and v is the additional argument.

RCSW_VAR_NARG(...)#

Tricky preprocessor awesomeness to get the # of arguments passed to a variadac macro.

It works by using the fact that VA_ARGS is a comma-separated list of arguments to determine an offset within the list of #’s that corresponds to the # of arguments passed to the macro. Works with up to 100 arguments.