Skip to content

Instantly share code, notes, and snippets.

@m1lkweed
Last active May 14, 2022 04:27
Show Gist options
  • Save m1lkweed/7682f2c2e9d37096f96d98080371c4d3 to your computer and use it in GitHub Desktop.
Save m1lkweed/7682f2c2e9d37096f96d98080371c4d3 to your computer and use it in GitHub Desktop.
Pointers that store array size data, x86_64 only; not recommended for use.
#include <inttypes.h>
#define IS_ARRAY(x) _Generic(&x, default: 1, typeof(*x)**: 0)
#define COUNT_OF(x) _Generic(&x, default: (sizeof(x) / sizeof(0[x])), typeof(*x)**: 0)
#define array_to_fatptr(x) ((fatptr_t){.addr = x, .n_elems = (int16_t)COUNT_OF(x)})
#define fatptr_to_ptr(x, type) ((type*)(intptr_t)x.addr)
typedef union fatptr_t{
void *ptr;
struct{
intptr_t addr:48;
int16_t n_elems;
};
} fatptr_t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment