diff options
author | obrien <obrien@FreeBSD.org> | 2008-09-23 14:45:10 +0000 |
---|---|---|
committer | obrien <obrien@FreeBSD.org> | 2008-09-23 14:45:10 +0000 |
commit | d31fa364752ed3b16d83a9cfa2866990824b3eb5 (patch) | |
tree | 4bf6265433e1ef6e4e0356df3e2303dc0c576080 | |
parent | 219d6d162647c587a33965a8d84283e18c02887a (diff) | |
download | FreeBSD-src-d31fa364752ed3b16d83a9cfa2866990824b3eb5.zip FreeBSD-src-d31fa364752ed3b16d83a9cfa2866990824b3eb5.tar.gz |
The kernel implemented 'memcmp' is an alias for 'bcmp'. However, memcmp
and bcmp are not the same thing. 'man bcmp' states that the return is
"non-zero" if the two byte strings are not identical. Where as,
'man memcmp' states that the return is the "difference between the
first two differing bytes (treated as unsigned char values" if the
two byte strings are not identical.
So provide a proper memcmp(9), but it is a C implementation not a tuned
assembly implementation. Therefore bcmp(9) should be preferred over memcmp(9).
-rw-r--r-- | sys/conf/files | 1 | ||||
-rw-r--r-- | sys/fs/tmpfs/tmpfs.h | 2 | ||||
-rw-r--r-- | sys/fs/tmpfs/tmpfs_subr.c | 2 | ||||
-rw-r--r-- | sys/fs/tmpfs/tmpfs_vnops.c | 2 | ||||
-rw-r--r-- | sys/i386/i386/bios.c | 2 | ||||
-rw-r--r-- | sys/ia64/ia64/efi.c | 2 | ||||
-rw-r--r-- | sys/ia64/ia64/sal.c | 2 | ||||
-rw-r--r-- | sys/libkern/memcmp.c | 53 | ||||
-rw-r--r-- | sys/mips/mips/support.S | 2 | ||||
-rw-r--r-- | sys/sys/libkern.h | 7 |
10 files changed, 61 insertions, 14 deletions
diff --git a/sys/conf/files b/sys/conf/files index 42e1f20..ff019e7 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1733,6 +1733,7 @@ libkern/iconv_xlat16.c optional libiconv libkern/index.c standard libkern/inet_ntoa.c standard libkern/mcount.c optional profiling-routine +libkern/memcmp.c standard libkern/qsort.c standard libkern/qsort_r.c standard libkern/random.c standard diff --git a/sys/fs/tmpfs/tmpfs.h b/sys/fs/tmpfs/tmpfs.h index b656439..5b3be11 100644 --- a/sys/fs/tmpfs/tmpfs.h +++ b/sys/fs/tmpfs/tmpfs.h @@ -445,7 +445,7 @@ int tmpfs_truncate(struct vnode *, off_t); */ #define TMPFS_DIRENT_MATCHES(de, name, len) \ (de->td_namelen == (uint16_t)len && \ - memcmp((de)->td_name, (name), (de)->td_namelen) == 0) + bcmp((de)->td_name, (name), (de)->td_namelen) == 0) /* --------------------------------------------------------------------- */ diff --git a/sys/fs/tmpfs/tmpfs_subr.c b/sys/fs/tmpfs/tmpfs_subr.c index f5eea18..fad3196 100644 --- a/sys/fs/tmpfs/tmpfs_subr.c +++ b/sys/fs/tmpfs/tmpfs_subr.c @@ -585,7 +585,7 @@ tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp) TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { MPASS(cnp->cn_namelen < 0xffff); if (de->td_namelen == (uint16_t)cnp->cn_namelen && - memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { + bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { found = 1; break; } diff --git a/sys/fs/tmpfs/tmpfs_vnops.c b/sys/fs/tmpfs/tmpfs_vnops.c index 8365b97..54795c1 100644 --- a/sys/fs/tmpfs/tmpfs_vnops.c +++ b/sys/fs/tmpfs/tmpfs_vnops.c @@ -969,7 +969,7 @@ tmpfs_rename(struct vop_rename_args *v) /* Ensure that we have enough memory to hold the new name, if it * has to be changed. */ if (fcnp->cn_namelen != tcnp->cn_namelen || - memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { + bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) { newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK); } else newname = NULL; diff --git a/sys/i386/i386/bios.c b/sys/i386/i386/bios.c index 525659b..128bda8 100644 --- a/sys/i386/i386/bios.c +++ b/sys/i386/i386/bios.c @@ -522,7 +522,7 @@ bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen) s = (u_char *)BIOS_PADDRTOVADDR(from); se = (u_char *)BIOS_PADDRTOVADDR(to-len); for (; s<se; s++) { - if (!memcmp(str, s, len)) { + if (!bcmp(str, s, len)) { bios_str = s; break; } diff --git a/sys/ia64/ia64/efi.c b/sys/ia64/ia64/efi.c index a10c208..161572d 100644 --- a/sys/ia64/ia64/efi.c +++ b/sys/ia64/ia64/efi.c @@ -106,7 +106,7 @@ efi_get_table(struct uuid *uuid) count = efi_systbl->st_entries; ct = efi_cfgtbl; while (count--) { - if (!memcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) + if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) return ((void *)IA64_PHYS_TO_RR7(ct->ct_data)); ct++; } diff --git a/sys/ia64/ia64/sal.c b/sys/ia64/ia64/sal.c index 251c1ee..6a55f39 100644 --- a/sys/ia64/ia64/sal.c +++ b/sys/ia64/ia64/sal.c @@ -96,7 +96,7 @@ ia64_sal_init(void) if (sal_systbl == NULL) return; - if (memcmp(sal_systbl->sal_signature, SAL_SIGNATURE, 4)) { + if (bcmp(sal_systbl->sal_signature, SAL_SIGNATURE, 4)) { printf("Bad signature for SAL System Table\n"); return; } diff --git a/sys/libkern/memcmp.c b/sys/libkern/memcmp.c new file mode 100644 index 0000000..6a6e69c --- /dev/null +++ b/sys/libkern/memcmp.c @@ -0,0 +1,53 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/libkern.h> + +/* + * Compare memory regions. + */ +int +memcmp(const void *s1, const void *s2, size_t n) +{ + if (n != 0) { + const unsigned char *p1 = s1, *p2 = s2; + + do { + if (*p1++ != *p2++) + return (*--p1 - *--p2); + } while (--n != 0); + } + return (0); +} diff --git a/sys/mips/mips/support.S b/sys/mips/mips/support.S index 269042c..d821361 100644 --- a/sys/mips/mips/support.S +++ b/sys/mips/mips/support.S @@ -870,10 +870,8 @@ END(bzero) /* * bcmp(s1, s2, n) - * memcmp(s1, s2, n) */ LEAF(bcmp) -ALEAF(memcmp) .set noreorder blt a2, 16, smallcmp # is it worth any trouble? xor v0, a0, a1 # compare low two bits of addresses diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index 0f5493e..88b4444 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -92,6 +92,7 @@ int flsl(long); int fnmatch(const char *, const char *, int); void gets(char *, size_t, int); int locc(int, char *, u_int); +int memcmp(const void *b1, const void *b2, size_t len); void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, @@ -140,12 +141,6 @@ crc32(const void *buf, size_t size) return (crc ^ ~0U); } -static __inline int -memcmp(const void *b1, const void *b2, size_t len) -{ - return (bcmp(b1, b2, len)); -} - LIBKERN_INLINE void *memset(void *, int, size_t); #ifdef LIBKERN_BODY LIBKERN_INLINE void * |