| 1 | /* @title: container_of macro */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <compiler.h> |
| 4 | #define container_of(ptr, type, member) \ |
| 5 | ({ \ |
| 6 | static_assert(__builtin_types_compatible_p( \ |
| 7 | typeof(*(ptr)), typeof(((type *) 0)->member)), \ |
| 8 | "Incompatible types for containerof"); \ |
| 9 | (type *) ((size_t) ptr - offsetof(type, member)); \ |
| 10 | }) |
| 11 |