Skip to content

Instantly share code, notes, and snippets.

View FedeDP's full-sized avatar

Federico Di Pierro FedeDP

  • Sysdig
  • Limbiate, Italy
  • LinkedIn in/fededp
View GitHub Profile
@FedeDP
FedeDP / go_like_interface.c
Last active December 18, 2020 07:53
Stupidly simple set of macros to mimic GO-like INTERFACE in C
#include <stdio.h>
#include <stddef.h>
#include <string.h>
/*
* PROs:
* * can have variables too as interface members
*
* CONs:
* * INTERFACE type can be directly declared and used as a normal struct
@FedeDP
FedeDP / struct_field_ptr_type.c
Last active July 17, 2018 12:16
Using these set of macros (GetPtr and GetSinglePtr) it is possible to refactor your struct and still having the correct types for pointers. Eg: it may happen that a uint32_t field is changed to uint8_t, but in some places it is still referred as uint32_t *tmp = myStruct->x, causing troubles.
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define MyPtrTypeOf(TYPE, MEMBER) typeof(&((TYPE *)0)->MEMBER)
#define GetPtr(t, i, MEMBER) \
MyPtrTypeOf(typeof(*t), MEMBER) MEMBER = \
(MyPtrTypeOf(typeof(*t), MEMBER)) ((void *)(t + i) + offsetof(typeof(*t), MEMBER))
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define X_FIELDS \
X(char *, field1) \
X(char *, field2) \
X(char *, field3) \
X(char *, field4) \