X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fmem%2Fmem.h;h=68a4aa3c18885c207a9fd11828bc5174bead6905;hb=e8d1e7304feb9318a841ad4a7d5a8773271ab814;hp=8d6bba943c8eab31243cadfdec3998cc1ed809f2;hpb=2c20abc2e2fdaf1888472968e811d00260dcf8fa;p=ccan diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h index 8d6bba94..68a4aa3c 100644 --- a/ccan/mem/mem.h +++ b/ccan/mem/mem.h @@ -183,4 +183,83 @@ static inline bool memends(const void *s, size_t s_len, const void *suffix, size suffix, suffix_len) == 0); } +/** + * memends_str - Does this byte array end with a string suffix? + * @a: byte array + * @al: length in bytes + * @s: string suffix + * + * Example: + * if (memends_str(somebytes, bytes_len, "It")) { + * printf("somebytes ends with with 'It'\n"); + * } + */ +PURE_FUNCTION +static inline bool memends_str(const void *a, size_t al, const char *s) +{ + return memends(a, al, s, strlen(s)); +} + +/** + * memoverlaps - Do two memory ranges overlap? + * @a: pointer to first memory range + * @al: length of first memory range + * @b: pointer to second memory range + * @al: length of second memory range + */ +CONST_FUNCTION +static inline bool memoverlaps(const void *a_, size_t al, + const void *b_, size_t bl) +{ + const char *a = a_; + const char *b = b_; + + return (a < (b + bl)) && (b < (a + al)); +} + +/* + * memswap - Exchange two memory regions + * @a: first region + * @b: second region + * @n: length of the regions + * + * Undefined results if the two memory regions overlap. + */ +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 */