Blame nss/lib/dbm/src/memmove.c

Packit 40b132
#if defined(__sun) && !defined(__SVR4)
Packit 40b132
/*-
Packit 40b132
 * Copyright (c) 1990, 1993
Packit 40b132
 *	The Regents of the University of California.  All rights reserved.
Packit 40b132
 *
Packit 40b132
 * This code is derived from software contributed to Berkeley by
Packit 40b132
 * Chris Torek.
Packit 40b132
 *
Packit 40b132
 * Redistribution and use in source and binary forms, with or without
Packit 40b132
 * modification, are permitted provided that the following conditions
Packit 40b132
 * are met:
Packit 40b132
 * 1. Redistributions of source code must retain the above copyright
Packit 40b132
 *    notice, this list of conditions and the following disclaimer.
Packit 40b132
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 40b132
 *    notice, this list of conditions and the following disclaimer in the
Packit 40b132
 *    documentation and/or other materials provided with the distribution.
Packit 40b132
 * 3. ***REMOVED*** - see 
Packit 40b132
 *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
Packit 40b132
 * 4. Neither the name of the University nor the names of its contributors
Packit 40b132
 *    may be used to endorse or promote products derived from this software
Packit 40b132
 *    without specific prior written permission.
Packit 40b132
 *
Packit 40b132
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 40b132
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 40b132
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 40b132
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 40b132
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 40b132
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 40b132
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 40b132
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 40b132
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 40b132
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 40b132
 * SUCH DAMAGE.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#if defined(LIBC_SCCS) && !defined(lint)
Packit 40b132
static char sccsid[] = "@(#)bcopy.c	8.1 (Berkeley) 6/4/93";
Packit 40b132
#endif /* LIBC_SCCS and not lint */
Packit 40b132
Packit 40b132
#include <string.h>
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * sizeof(word) MUST BE A POWER OF TWO
Packit 40b132
 * SO THAT wmask BELOW IS ALL ONES
Packit 40b132
 */
Packit 40b132
typedef	int word;		/* "word" used for optimal copy speed */
Packit 40b132
Packit 40b132
#define	wsize	sizeof(word)
Packit 40b132
#define	wmask	(wsize - 1)
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Copy a block of memory, handling overlap.
Packit 40b132
 * This is the routine that actually implements
Packit 40b132
 * (the portable versions of) bcopy, memcpy, and memmove.
Packit 40b132
 */
Packit 40b132
#ifdef MEMCOPY
Packit 40b132
void *
Packit 40b132
memcpy(dst0, src0, length)
Packit 40b132
#else
Packit 40b132
#ifdef MEMMOVE
Packit 40b132
void *
Packit 40b132
memmove(dst0, src0, length)
Packit 40b132
#else
Packit 40b132
void
Packit 40b132
bcopy(src0, dst0, length)
Packit 40b132
#endif
Packit 40b132
#endif
Packit 40b132
	void *dst0;
Packit 40b132
	const void *src0;
Packit 40b132
	register size_t length;
Packit 40b132
{
Packit 40b132
	register char *dst = dst0;
Packit 40b132
	register const char *src = src0;
Packit 40b132
	register size_t t;
Packit 40b132
Packit 40b132
	if (length == 0 || dst == src)		/* nothing to do */
Packit 40b132
		goto done;
Packit 40b132
Packit 40b132
	/*
Packit 40b132
	 * Macros: loop-t-times; and loop-t-times, t>0
Packit 40b132
	 */
Packit 40b132
#define	TLOOP(s) if (t) TLOOP1(s)
Packit 40b132
#define	TLOOP1(s) do { s; } while (--t)
Packit 40b132
Packit 40b132
	if ((unsigned long)dst < (unsigned long)src) {
Packit 40b132
		/*
Packit 40b132
		 * Copy forward.
Packit 40b132
		 */
Packit 40b132
		t = (int)src;	/* only need low bits */
Packit 40b132
		if ((t | (int)dst) & wmask) {
Packit 40b132
			/*
Packit 40b132
			 * Try to align operands.  This cannot be done
Packit 40b132
			 * unless the low bits match.
Packit 40b132
			 */
Packit 40b132
			if ((t ^ (int)dst) & wmask || length < wsize)
Packit 40b132
				t = length;
Packit 40b132
			else
Packit 40b132
				t = wsize - (t & wmask);
Packit 40b132
			length -= t;
Packit 40b132
			TLOOP1(*dst++ = *src++);
Packit 40b132
		}
Packit 40b132
		/*
Packit 40b132
		 * Copy whole words, then mop up any trailing bytes.
Packit 40b132
		 */
Packit 40b132
		t = length / wsize;
Packit 40b132
		TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
Packit 40b132
		t = length & wmask;
Packit 40b132
		TLOOP(*dst++ = *src++);
Packit 40b132
	} else {
Packit 40b132
		/*
Packit 40b132
		 * Copy backwards.  Otherwise essentially the same.
Packit 40b132
		 * Alignment works as before, except that it takes
Packit 40b132
		 * (t&wmask) bytes to align, not wsize-(t&wmask).
Packit 40b132
		 */
Packit 40b132
		src += length;
Packit 40b132
		dst += length;
Packit 40b132
		t = (int)src;
Packit 40b132
		if ((t | (int)dst) & wmask) {
Packit 40b132
			if ((t ^ (int)dst) & wmask || length <= wsize)
Packit 40b132
				t = length;
Packit 40b132
			else
Packit 40b132
				t &= wmask;
Packit 40b132
			length -= t;
Packit 40b132
			TLOOP1(*--dst = *--src);
Packit 40b132
		}
Packit 40b132
		t = length / wsize;
Packit 40b132
		TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
Packit 40b132
		t = length & wmask;
Packit 40b132
		TLOOP(*--dst = *--src);
Packit 40b132
	}
Packit 40b132
done:
Packit 40b132
#if defined(MEMCOPY) || defined(MEMMOVE)
Packit 40b132
	return (dst0);
Packit 40b132
#else
Packit 40b132
	return;
Packit 40b132
#endif
Packit 40b132
}
Packit 40b132
#endif /* no __sgi */
Packit 40b132
Packit 40b132
/* Some compilers don't like an empty source file. */
Packit 40b132
static int dummy = 0;