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).
This commit is contained in:
David E. O'Brien 2008-09-23 14:45:10 +00:00
parent b386eb9139
commit ae72afe0f2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=183299
10 changed files with 61 additions and 14 deletions

View File

@ -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

View File

@ -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)
/* --------------------------------------------------------------------- */

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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++;
}

View File

@ -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;
}

53
sys/libkern/memcmp.c Normal file
View File

@ -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);
}

View File

@ -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

View File

@ -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 *