xref: /f-stack/freebsd/libkern/bcopy.c (revision 22ce4aff)
1*22ce4affSfengbojiang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  * Copyright (c) 1990 The Regents of the University of California.
4*22ce4affSfengbojiang  *
5*22ce4affSfengbojiang  * All rights reserved.
6*22ce4affSfengbojiang  *
7*22ce4affSfengbojiang  * This code is derived from software contributed to Berkeley by
8*22ce4affSfengbojiang  * Chris Torek.
9*22ce4affSfengbojiang  *
10*22ce4affSfengbojiang  * Redistribution and use in source and binary forms, with or without
11*22ce4affSfengbojiang  * modification, are permitted provided that the following conditions
12*22ce4affSfengbojiang  * are met:
13*22ce4affSfengbojiang  * 1. Redistributions of source code must retain the above copyright
14*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer.
15*22ce4affSfengbojiang  * 2. Redistributions in binary form must reproduce the above copyright
16*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer in the
17*22ce4affSfengbojiang  *    documentation and/or other materials provided with the distribution.
18*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
19*22ce4affSfengbojiang  *    may be used to endorse or promote products derived from this software
20*22ce4affSfengbojiang  *    without specific prior written permission.
21*22ce4affSfengbojiang  *
22*22ce4affSfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*22ce4affSfengbojiang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*22ce4affSfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*22ce4affSfengbojiang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*22ce4affSfengbojiang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*22ce4affSfengbojiang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*22ce4affSfengbojiang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*22ce4affSfengbojiang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*22ce4affSfengbojiang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*22ce4affSfengbojiang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*22ce4affSfengbojiang  * SUCH DAMAGE.
33*22ce4affSfengbojiang  */
34*22ce4affSfengbojiang 
35*22ce4affSfengbojiang #if defined(LIBC_SCCS) && !defined(lint)
36*22ce4affSfengbojiang #if 0
37*22ce4affSfengbojiang static char *sccsid = "from: @(#)bcopy.c      5.11 (Berkeley) 6/21/91";
38*22ce4affSfengbojiang #endif
39*22ce4affSfengbojiang #if 0
40*22ce4affSfengbojiang static char *rcsid = "$NetBSD: bcopy.c,v 1.2 1997/04/16 22:09:41 thorpej Exp $";
41*22ce4affSfengbojiang #endif
42*22ce4affSfengbojiang #endif /* LIBC_SCCS and not lint */
43*22ce4affSfengbojiang #include <sys/cdefs.h>
44*22ce4affSfengbojiang __FBSDID("$FreeBSD$");
45*22ce4affSfengbojiang 
46*22ce4affSfengbojiang #include <sys/param.h>
47*22ce4affSfengbojiang #ifdef _KERNEL
48*22ce4affSfengbojiang #include <sys/systm.h>
49*22ce4affSfengbojiang #else
50*22ce4affSfengbojiang #include <string.h>
51*22ce4affSfengbojiang #endif
52*22ce4affSfengbojiang 
53*22ce4affSfengbojiang #undef memcpy
54*22ce4affSfengbojiang #undef memmove
55*22ce4affSfengbojiang #undef bcopy
56*22ce4affSfengbojiang 
57*22ce4affSfengbojiang /*
58*22ce4affSfengbojiang  * sizeof(word) MUST BE A POWER OF TWO
59*22ce4affSfengbojiang  * SO THAT wmask BELOW IS ALL ONES
60*22ce4affSfengbojiang  */
61*22ce4affSfengbojiang typedef	long	word;		/* "word" used for optimal copy speed */
62*22ce4affSfengbojiang 
63*22ce4affSfengbojiang #define	wsize	sizeof(word)
64*22ce4affSfengbojiang #define wmask	(wsize - 1)
65*22ce4affSfengbojiang 
66*22ce4affSfengbojiang /*
67*22ce4affSfengbojiang  * Copy a block of memory, handling overlap.
68*22ce4affSfengbojiang  * This is the routine that actually implements
69*22ce4affSfengbojiang  * (the portable versions of) bcopy, memcpy, and memmove.
70*22ce4affSfengbojiang  */
71*22ce4affSfengbojiang void *
memcpy(void * dst0,const void * src0,size_t length)72*22ce4affSfengbojiang memcpy(void *dst0, const void *src0, size_t length)
73*22ce4affSfengbojiang {
74*22ce4affSfengbojiang 	char		*dst;
75*22ce4affSfengbojiang 	const char	*src;
76*22ce4affSfengbojiang 	size_t		t;
77*22ce4affSfengbojiang 
78*22ce4affSfengbojiang 	dst = dst0;
79*22ce4affSfengbojiang 	src = src0;
80*22ce4affSfengbojiang 
81*22ce4affSfengbojiang 	if (length == 0 || dst == src) {	/* nothing to do */
82*22ce4affSfengbojiang 		goto done;
83*22ce4affSfengbojiang 	}
84*22ce4affSfengbojiang 
85*22ce4affSfengbojiang 	/*
86*22ce4affSfengbojiang 	 * Macros: loop-t-times; and loop-t-times, t>0
87*22ce4affSfengbojiang 	 */
88*22ce4affSfengbojiang #define	TLOOP(s) if (t) TLOOP1(s)
89*22ce4affSfengbojiang #define	TLOOP1(s) do { s; } while (--t)
90*22ce4affSfengbojiang 
91*22ce4affSfengbojiang 	if ((unsigned long)dst < (unsigned long)src) {
92*22ce4affSfengbojiang 		/*
93*22ce4affSfengbojiang 		 * Copy forward.
94*22ce4affSfengbojiang 		 */
95*22ce4affSfengbojiang 		t = (size_t)src;	/* only need low bits */
96*22ce4affSfengbojiang 
97*22ce4affSfengbojiang 		if ((t | (uintptr_t)dst) & wmask) {
98*22ce4affSfengbojiang 			/*
99*22ce4affSfengbojiang 			 * Try to align operands.  This cannot be done
100*22ce4affSfengbojiang 			 * unless the low bits match.
101*22ce4affSfengbojiang 			 */
102*22ce4affSfengbojiang 			if ((t ^ (uintptr_t)dst) & wmask || length < wsize) {
103*22ce4affSfengbojiang 				t = length;
104*22ce4affSfengbojiang 			} else {
105*22ce4affSfengbojiang 				t = wsize - (t & wmask);
106*22ce4affSfengbojiang 			}
107*22ce4affSfengbojiang 
108*22ce4affSfengbojiang 			length -= t;
109*22ce4affSfengbojiang 			TLOOP1(*dst++ = *src++);
110*22ce4affSfengbojiang 		}
111*22ce4affSfengbojiang 		/*
112*22ce4affSfengbojiang 		 * Copy whole words, then mop up any trailing bytes.
113*22ce4affSfengbojiang 		 */
114*22ce4affSfengbojiang 		t = length / wsize;
115*22ce4affSfengbojiang 		TLOOP(*(word *)dst = *(const word *)src; src += wsize;
116*22ce4affSfengbojiang 		    dst += wsize);
117*22ce4affSfengbojiang 		t = length & wmask;
118*22ce4affSfengbojiang 		TLOOP(*dst++ = *src++);
119*22ce4affSfengbojiang 	} else {
120*22ce4affSfengbojiang 		/*
121*22ce4affSfengbojiang 		 * Copy backwards.  Otherwise essentially the same.
122*22ce4affSfengbojiang 		 * Alignment works as before, except that it takes
123*22ce4affSfengbojiang 		 * (t&wmask) bytes to align, not wsize-(t&wmask).
124*22ce4affSfengbojiang 		 */
125*22ce4affSfengbojiang 		src += length;
126*22ce4affSfengbojiang 		dst += length;
127*22ce4affSfengbojiang 		t = (uintptr_t)src;
128*22ce4affSfengbojiang 
129*22ce4affSfengbojiang 		if ((t | (uintptr_t)dst) & wmask) {
130*22ce4affSfengbojiang 			if ((t ^ (uintptr_t)dst) & wmask || length <= wsize) {
131*22ce4affSfengbojiang 				t = length;
132*22ce4affSfengbojiang 			} else {
133*22ce4affSfengbojiang 				t &= wmask;
134*22ce4affSfengbojiang 			}
135*22ce4affSfengbojiang 
136*22ce4affSfengbojiang 			length -= t;
137*22ce4affSfengbojiang 			TLOOP1(*--dst = *--src);
138*22ce4affSfengbojiang 		}
139*22ce4affSfengbojiang 		t = length / wsize;
140*22ce4affSfengbojiang 		TLOOP(src -= wsize; dst -= wsize;
141*22ce4affSfengbojiang 		    *(word *)dst = *(const word *)src);
142*22ce4affSfengbojiang 		t = length & wmask;
143*22ce4affSfengbojiang 		TLOOP(*--dst = *--src);
144*22ce4affSfengbojiang 	}
145*22ce4affSfengbojiang done:
146*22ce4affSfengbojiang 	return (dst0);
147*22ce4affSfengbojiang }
148*22ce4affSfengbojiang 
149*22ce4affSfengbojiang __strong_reference(memcpy, memmove);
150*22ce4affSfengbojiang 
151*22ce4affSfengbojiang void
152*22ce4affSfengbojiang (bcopy)(const void *src0, void *dst0, size_t length)
153*22ce4affSfengbojiang {
154*22ce4affSfengbojiang 
155*22ce4affSfengbojiang 	memcpy(dst0, src0, length);
156*22ce4affSfengbojiang }
157