diff options
author | Andreas Fankhauser hiddenalpha.ch | 2024-04-24 21:33:01 +0200 |
---|---|---|
committer | Andreas Fankhauser hiddenalpha.ch | 2024-04-24 21:33:01 +0200 |
commit | 37af455dff71ab08070e48f6a848e326a52902ca (patch) | |
tree | 6765a00fb4fa924b1985f4f2988f1b21cbeb367a /src/main/c/common | |
parent | e17b8876cfafae081784d5cf1258ce16c50e283a (diff) | |
download | UnspecifiedGarbage-37af455dff71ab08070e48f6a848e326a52902ca.zip UnspecifiedGarbage-37af455dff71ab08070e48f6a848e326a52902ca.tar.gz |
Add a standalone container_of impl. Fix assert_is template.
Diffstat (limited to 'src/main/c/common')
-rw-r--r-- | src/main/c/common/assert_is.h | 4 | ||||
-rw-r--r-- | src/main/c/common/offset_of.h | 9 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/main/c/common/assert_is.h b/src/main/c/common/assert_is.h index b6e3132..316bf02 100644 --- a/src/main/c/common/assert_is.h +++ b/src/main/c/common/assert_is.h @@ -2,7 +2,7 @@ #if !NDEBUG #define TPL_assert_is(T, PRED) static inline T*assert_is_##T(void*p,\ const char*f,int l){if(p==NULL){fprintf(stderr,"assert(" STR_QUOT(T)\ -" != NULL) %s:%d\n",f,l);abort();}T*obj=p;if(!PRED){fprintf(stderr,\ +" != NULL) %s:%d\n",f,l);abort();}T*obj=p;if(!(PRED)){fprintf(stderr,\ "ssert(type is \""STR_QUOT(T)"\") %s:%d\n",f,l);abort();}return p; } #else #define TPL_assert_is(T, PRED) static inline T*assert_is_##T(void*p,\ @@ -20,7 +20,7 @@ struct Person { }; /* instantiate a checker */ -TPL_assert_is(Person, !strcmp(obj->tYPE, "Hi, I'm a Person")); +TPL_assert_is(Person, !strcmp(obj->tYPE, "Hi, I'm a Person")) #define assert_is_Person(p) assert_is_Person(p, __FILE__, __LINE__) /* make sure magic is initialized (ALSO MAKE SURE TO PROPERLY INVALIDATE diff --git a/src/main/c/common/offset_of.h b/src/main/c/common/offset_of.h new file mode 100644 index 0000000..7d9179d --- /dev/null +++ b/src/main/c/common/offset_of.h @@ -0,0 +1,9 @@ +#ifndef INCGUARD_yisgKqALPG4lfEqb +#define INCGUARD_yisgKqALPG4lfEqb + + +#define container_of(P, T, M) \ + ((T*)( ((size_t)P) - ((size_t)((char*)&((T*)0)->M - (char*)0) ))) + + +#endif /* INCGUARD_yisgKqALPG4lfEqb */ |