nostril

A C cli tool for creating nostr events
git clone git://jb55.com/nostril
Log | Files | Refs | Submodules | README | LICENSE

compiler.h (2391B)


      1 
      2 #ifndef COMPILER_H
      3 #define COMPILER_H
      4 
      5 #include <stdbool.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include "config.h"
      9 
     10 #if HAVE_UNALIGNED_ACCESS
     11 #define alignment_ok(p, n) 1
     12 #else
     13 #define alignment_ok(p, n) ((size_t)(p) % (n) == 0)
     14 #endif
     15 
     16 #define UNUSED __attribute__((__unused__))
     17 
     18 /**
     19  * BUILD_ASSERT - assert a build-time dependency.
     20  * @cond: the compile-time condition which must be true.
     21  *
     22  * Your compile will fail if the condition isn't true, or can't be evaluated
     23  * by the compiler.  This can only be used within a function.
     24  *
     25  * Example:
     26  *	#include <stddef.h>
     27  *	...
     28  *	static char *foo_to_char(struct foo *foo)
     29  *	{
     30  *		// This code needs string to be at start of foo.
     31  *		BUILD_ASSERT(offsetof(struct foo, string) == 0);
     32  *		return (char *)foo;
     33  *	}
     34  */
     35 #define BUILD_ASSERT(cond) \
     36 	do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
     37 
     38 /**
     39  * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
     40  * @cond: the compile-time condition which must be true.
     41  *
     42  * Your compile will fail if the condition isn't true, or can't be evaluated
     43  * by the compiler.  This can be used in an expression: its value is "0".
     44  *
     45  * Example:
     46  *	#define foo_to_char(foo)					\
     47  *		 ((char *)(foo)						\
     48  *		  + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
     49  */
     50 #define BUILD_ASSERT_OR_ZERO(cond) \
     51 	(sizeof(char [1 - 2*!(cond)]) - 1)
     52 
     53 #define memclear(mem, size) memset(mem, 0, size)
     54 #define memclear_2(m1, s1, m2, s2) { memclear(m1, s1); memclear(m2, s2); }
     55 #define memclear_3(m1, s1, m2, s2, m3, s3) { memclear(m1, s1); memclear(m2, s2); memclear(m3, s3); }
     56 
     57 static inline void *memcheck_(const void *data, size_t len)
     58 {
     59 	(void)len;
     60 	return (void *)data;
     61 }
     62 
     63 #if HAVE_TYPEOF
     64 /**
     65  * memcheck - check that a memory region is initialized
     66  * @data: start of region
     67  * @len: length in bytes
     68  *
     69  * When running under valgrind, this causes an error to be printed
     70  * if the entire region is not defined.  Otherwise valgrind only
     71  * reports an error when an undefined value is used for a branch, or
     72  * written out.
     73  *
     74  * Example:
     75  *	// Search for space, but make sure it's all initialized.
     76  *	if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) {
     77  *		printf("space was found!\n");
     78  *	}
     79  */
     80 #define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len)))
     81 #else
     82 #define memcheck(data, len) memcheck_((data), (len))
     83 #endif
     84 
     85 #endif /* COMPILER_H */