Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / atomic.h
Created June 18, 2012 10:59
Atomic read/write
#ifndef ATOMIC_H
#define ATOMIC_H
// Adapted from http://golubenco.org/2007/06/14/atomic-operations
/**
* @brief Atomic type
*/
typedef int atomic_t;
@aprell
aprell / pingpong.c
Created July 4, 2012 09:50
RCCE: measuring round-trip latency between two cores
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <RCCE.h>
#include "timer.h"
#define PRINT(...) { printf(__VA_ARGS__); fflush(stdout); }
#define CORE(i) if (RCCE_ue() == (i))
#define MSG_SIZE 32
@aprell
aprell / pingpong-all.c
Created July 4, 2012 10:08
RCCE: measuring round-trip latency between one core and all the other cores
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <RCCE.h>
#include "timer.h"
#define PRINT(...) { printf(__VA_ARGS__); fflush(stdout); }
#define CORE(i) if (RCCE_ue() == (i))
#define MSG_SIZE 32
@aprell
aprell / variadic_macro.h
Created September 6, 2012 17:33
Variadic macro
#define vm(args...) \
do { \
/* Check if argument list is empty */ \
if (#args[0] == '\0') { \
printf("Argument list is empty\n"); \
} else { \
printf("Argument list is \"%s\"\n", #args); \
} \
} while (0)
@aprell
aprell / va_nargs.c
Last active August 8, 2024 02:04
Counting arguments in variadic macros
// RUN: cc -Wall -Wextra %s && ./a.out
#include <assert.h>
#if defined(__GNUC__) || defined(__clang__)
// Supports 0-10 arguments
#define VA_NARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
// ## deletes preceding comma if _VA_ARGS__ is empty (GCC, Clang)
#define VA_NARGS(...) VA_NARGS_IMPL(_, ## __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#else
@aprell
aprell / async.c
Created September 14, 2012 16:31
Asynchronous function call / task support macros
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "async.h"
typedef struct {
void *fn; // pointer to piece of code
unsigned char data[128]; // data environment
} Task;
@aprell
aprell / overload.c
Created September 18, 2012 11:58
Simple function overloading in C
#include <stdio.h>
/* Count variadic macro arguments (1-10 arguments, extend as needed)
*/
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
/* Simple name mangling of function sum based on arity
*/
#define __sum_impl2(n, ...) __sum_impl__ ## n(__VA_ARGS__)
@aprell
aprell / overload.h.in
Created September 18, 2012 12:08
Simple function overloading in C (extended)
#ifndef OVERLOAD__FN__H
#define OVERLOAD__FN__H
#ifndef VA_NARGS
/* Count variadic macro arguments (1-10 arguments, extend as needed)
*/
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#endif
@aprell
aprell / named_params.c
Created September 30, 2012 17:56
Named parameters and default argument values in C
#include <stdio.h>
#include <stdlib.h>
struct rectangle {
double x1, x2, y1, y2;
};
double area(struct rectangle r)
{
return (r.x2 - r.x1) * (r.y2 - r.y1);
@aprell
aprell / protected.c
Created October 2, 2012 14:16
Protected blocks in C
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// Make sure that before is executed before a dependent block or statement
// and after is executed afterwards
#define PROTECTED(before, after) \
for (int __i = ((before), 0); !__i; (after), __i++)
int main(void)