diff --git a/include/import/ebimtree.h b/include/import/ebimtree.h index 8b12a548d..0afbdd13f 100644 --- a/include/import/ebimtree.h +++ b/include/import/ebimtree.h @@ -62,7 +62,7 @@ __ebim_lookup(struct eb_root *root, const void *x, unsigned int len) if (eb_gettag(troot) == EB_LEAF) { node = container_of(eb_untag(troot, EB_LEAF), struct ebpt_node, node.branches); - if (memcmp(node->key + pos, x, len) != 0) + if (eb_memcmp(node->key + pos, x, len) != 0) goto ret_null; else goto ret_node; @@ -76,7 +76,7 @@ __ebim_lookup(struct eb_root *root, const void *x, unsigned int len) * value, and we walk down left, or it's a different * one and we don't have our key. */ - if (memcmp(node->key + pos, x, len) != 0) + if (eb_memcmp(node->key + pos, x, len) != 0) goto ret_null; else goto walk_left; diff --git a/include/import/ebmbtree.h b/include/import/ebmbtree.h index 8262ec614..a01062494 100644 --- a/include/import/ebmbtree.h +++ b/include/import/ebmbtree.h @@ -154,7 +154,7 @@ static forceinline struct ebmb_node *__ebmb_lookup(struct eb_root *root, const v if (eb_gettag(troot) == EB_LEAF) { node = container_of(eb_untag(troot, EB_LEAF), struct ebmb_node, node.branches); - if (memcmp(node->key + pos, x, len) != 0) + if (eb_memcmp(node->key + pos, x, len) != 0) goto ret_null; else goto ret_node; @@ -168,7 +168,7 @@ static forceinline struct ebmb_node *__ebmb_lookup(struct eb_root *root, const v * value, and we walk down left, or it's a different * one and we don't have our key. */ - if (memcmp(node->key + pos, x, len) != 0) + if (eb_memcmp(node->key + pos, x, len) != 0) goto ret_null; else goto walk_left; diff --git a/include/import/ebtree.h b/include/import/ebtree.h index 73ca6d5fb..85ea31d9b 100644 --- a/include/import/ebtree.h +++ b/include/import/ebtree.h @@ -908,6 +908,7 @@ static forceinline int get_bit(const unsigned char *a, unsigned int pos) /* These functions are declared in ebtree.c */ void eb_delete(struct eb_node *node); struct eb_node *eb_insert_dup(struct eb_node *sub, struct eb_node *new); +int eb_memcmp(const void *m1, const void *m2, size_t len); #endif /* _EB_TREE_H */ diff --git a/src/ebtree.c b/src/ebtree.c index 3b904209d..db27875e1 100644 --- a/src/ebtree.c +++ b/src/ebtree.c @@ -30,3 +30,21 @@ struct eb_node *eb_insert_dup(struct eb_node *sub, struct eb_node *new) { return __eb_insert_dup(sub, new); } + +/* compares memory blocks m1 and m2 for up to bytes. Immediately stops at + * the first non-matching byte. It returns 0 on full match, non-zero otherwise. + * One byte will always be checked so this must not be called with len==0. It + * takes 2+5cy/B on x86_64 and is ~29 bytes long. + */ +int eb_memcmp(const void *m1, const void *m2, size_t len) +{ + const char *p1 = (const char *)m1 + len; + const char *p2 = (const char *)m2 + len; + ssize_t ofs = -len; + char diff; + + do { + diff = p1[ofs] - p2[ofs]; + } while (!diff && ++ofs); + return diff; +}