X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fmem%2Fmem.h;h=68a4aa3c18885c207a9fd11828bc5174bead6905;hp=dd66cc62cd8cce990aeac17da41962aa2bf74a74;hb=e8d1e7304feb9318a841ad4a7d5a8773271ab814;hpb=1b7100ad101992adeaeb5c4ab85ee9a1b368b842 diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h index dd66cc62..68a4aa3c 100644 --- a/ccan/mem/mem.h +++ b/ccan/mem/mem.h @@ -227,4 +227,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 */