]> git.ozlabs.org Git - ccan/blobdiff - ccan/mem/mem.h
mem: Add function to check whether memory ranges overlap
[ccan] / ccan / mem / mem.h
index 8d6bba943c8eab31243cadfdec3998cc1ed809f2..99c34c0a39719ec0a262add21e74bab5b99a8e56 100644 (file)
@@ -183,4 +183,38 @@ 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));
+}
+
 #endif /* CCAN_MEM_H */