mirror of
https://github.com/troglobit/libite.git
synced 2026-10-17 10:39:36 +00:00
Add some very useful helper macros: NELEMS() and bitops.
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
a4cbc7a10e
commit
117ec58a90
2 files changed
+50
No files matched your search
@@ -57,6 +57,31 @@ Helper Macros
|
||||
|
||||
Mysteriously missing from GLIBC
|
||||
|
||||
- `ISCLR(word,bit)`
|
||||
|
||||
Is bit in (integer) word cleared?
|
||||
|
||||
- `ISSET(word,bit)`
|
||||
|
||||
Is bit in (integer) word set?
|
||||
|
||||
- `ISOTHER(word,bit)`
|
||||
|
||||
Is any other bits, except bit, in (integer) word set?
|
||||
|
||||
- `SETBIT(word,bit)`
|
||||
|
||||
Set bit in (integer) word.
|
||||
|
||||
- `CLRBIT(word,bit)`
|
||||
|
||||
Clear bit in (integer) word.
|
||||
|
||||
- `NELEMS(array)`
|
||||
|
||||
Returns the number of elements in an array. From the great book, The
|
||||
Practice of Programming, by Kernighan and Pike.
|
||||
|
||||
- `UNUSED(var)`
|
||||
|
||||
Shorter and more readable version of `var __attribute__ ((unused))`
|
||||
|
||||
@@ -87,9 +87,34 @@ long long strtonum (const char *numstr, long long minval, long long maxval, cons
|
||||
#ifndef blkdev
|
||||
# define blkdev(x,m,maj,min) mknod((x), S_IFBLK|(m), makedev((maj),(min)))
|
||||
#endif
|
||||
|
||||
#ifndef S_ISEXEC
|
||||
# define S_ISEXEC(m) (((m) & S_IXUSR) == S_IXUSR)
|
||||
#endif
|
||||
|
||||
/* Unline isset(), setbit() et al, these work with integers/shorts/longwords/etc. */
|
||||
#ifndef ISCLR
|
||||
#define ISCLR(word,bit) ((word & (1 << (bit)) ? 0 : 1))
|
||||
#endif
|
||||
#ifndef ISSET
|
||||
#define ISSET(word,bit) ((word & (1 << (bit)) ? 1 : 0))
|
||||
#endif
|
||||
#ifndef ISOTHER
|
||||
#define ISOTHER(word,bit) ((word & ~(1 << (bit)) ? 1 : 0)) /* Is any other bit set? */
|
||||
#endif
|
||||
#ifndef SETBIT
|
||||
#define SETBIT(word,bit) (word |= (1 << (bit)))
|
||||
#endif
|
||||
#ifndef CLRBIT
|
||||
#define CLRBIT(word,bit) (word &= ~(1 << (bit)))
|
||||
#endif
|
||||
|
||||
/* From The Practice of Programming, by Kernighan and Pike */
|
||||
#ifndef NELEMS
|
||||
#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
|
||||
#endif
|
||||
|
||||
/* Mark a function variable as unused, useful for generic callbacks */
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user