X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fmem%2Fmem.h;h=a3403c4ee31dc8794f89acfd95836e5c17c4672a;hb=aae40e493625a07f4ac95476664447546b28661a;hp=dd66cc62cd8cce990aeac17da41962aa2bf74a74;hpb=428d3b0217b32892c2fd309358619a4877113833;p=ccan diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h index dd66cc62..a3403c4e 100644 --- a/ccan/mem/mem.h +++ b/ccan/mem/mem.h @@ -149,6 +149,19 @@ static inline bool memeqstr(const void *data, size_t length, const char *string) return memeq(data, length, string, strlen(string)); } +/** + * memeqzero - Is a byte array all zeroes? + * @data: byte array + * @length: length of @data in bytes + * + * Example: + * if (memeqzero(somebytes, bytes_len)) { + * printf("somebytes == 0!\n"); + * } + */ +PURE_FUNCTION +bool memeqzero(const void *data, size_t length); + /** * memstarts_str - Does this byte array start with a string prefix? * @a: byte array @@ -227,4 +240,39 @@ static inline bool memoverlaps(const void *a_, size_t al, */ void memswap(void *a, void *b, size_t n); +#if HAVE_VALGRIND_MEMCHECK_H +#include +static inline void *memcheck_(const void *data, size_t len) +{ + VALGRIND_CHECK_MEM_IS_DEFINED(data, len); + return (void *)data; +} +#else +static inline void *memcheck_(const void *data, size_t len) +{ + return (void *)data; +} +#endif + +#if HAVE_TYPEOF +/** + * memcheck - check that a memory region is initialized + * @data: start of region + * @len: length in bytes + * + * When running under valgrind, this causes an error to be printed + * if the entire region is not defined. Otherwise valgrind only + * reports an error when an undefined value is used for a branch, or + * written out. + * + * Example: + * // Search for space, but make sure it's all initialized. + * if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) { + * printf("space was found!\n"); + * } + */ +#define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len))) +#else +#define memcheck(data, len) memcheck_((data), (len)) +#endif #endif /* CCAN_MEM_H */