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