CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches

Cross-platform macros for controlling code generation and memory layout. These macros provide a consistent interface across compilers for common optimizations and attributes.

stackAlloc(size)

Allocate memory on the stack (equivalent to alloca).

Allocates the specified number of bytes on the current stack frame. The memory is automatically freed when the function returns. This is faster than heap allocation but should be used carefully as it can cause stack overflow if used with large or unbounded sizes.

Warning: The allocated memory is only valid until the function returns. Do not return pointers to stack-allocated memory.

Parameters
szNumber of bytes to allocate
Returns
Pointer to allocated stack memory

Example:

void processData(size_t count) {
int *temp = stackAlloc(count * sizeof(int));
// Use temp array...
// Automatically freed on return
}

_meta_inline

Force a function to be inlined.

Apply to functions that are critical to performance or used for metaprogramming where inlining is essential for proper code generation. The compiler will aggressively inline these functions even at low optimization levels.

Note: Despite the leading underscore, this macro is part of the public API. It's prefixed with underscore to match C11 conventions and avoid conflicts with user code.

Example:

_meta_inline int add(int a, int b) {
return a + b;
}

_no_inline

Prevent a function from being inlined.

Apply to functions where inlining would be detrimental (e.g., rarely called error handlers, functions with large bodies, or when you need a stable function address for debugging).

Example:

_no_inline void reportError(const char *msg) {
// Error handling code...
}

_no_return

Indicate that a function never returns to its caller.

Apply to functions that always exit the program, throw exceptions, or perform long jumps. This helps the compiler optimize code paths and eliminates warnings about missing return statements in calling code.

Example:

_no_return void fatal(const char *msg) {
fprintf(stderr, "Fatal: %s\n", msg);
exit(1);
}

alignMem(bytes)

Align a variable or struct to a specific byte boundary.

Ensures that the declared variable or type is aligned to the specified power-of-2 byte boundary. This is useful for SIMD operations, cache line alignment, or interfacing with hardware that requires specific alignment.

Parameters
bytesAlignment in bytes (must be power of 2)

Example:

// Align to 16-byte boundary for SIMD
alignMem(16) float vec[4];
// Cache line aligned structure
typedef struct alignMem(64) {
atomic(int) counter;
char padding[60];
} CacheLinePadded;