memcpy的实现(转载)

本文介绍了一个高效的内存复制函数实现,该实现能够处理源和目标内存区域重叠的情况,并且通过利用处理器的字大小来优化复制速度。代码展示了如何对齐内存地址以提高复制效率,并分别处理了向前复制和向后复制两种情况。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*	$NetBSD: bcopy.c,v 1.3 2007/06/04 18:19:27 christos Exp $	*/


/*-

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

 * 3. 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>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)bcopy.c	8.1 (Berkeley) 6/4/93";
#else

__RCSID("$NetBSD: bcopy.c,v 1.3 2007/06/04 18:19:27 christos Exp ___FCKpd___1quot;);
#endif
#endif /* LIBC_SCCS and not lint */


#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <assert.h>
#include <string.h>
#else
#include <lib/libkern/libkern.h>
#endif

#ifdef _FORTIFY_SOURCE
#undef bcopy
#undef memcpy
#undef memmove
#endif

/*

 * sizeof(word) MUST BE A POWER OF TWO

 * SO THAT wmask BELOW IS ALL ONES

 */

typedef	long word;		/* "word" used for optimal copy speed */

#define	wsize	sizeof(word)
#define	wmask	(wsize - 1)

/*

 * Copy a block of memory, handling overlap.

 * This is the routine that actually implements

 * (the portable versions of) bcopy, memcpy, and memmove.

 */

#ifdef MEMCOPY
void *
memcpy(void *dst0, const void *src0, size_t length)
#else

#ifdef MEMMOVE
void *
memmove(void *dst0, const void *src0, size_t length)
#else

void
bcopy(const void *src0, void *dst0, size_t length)
#endif
#endif
{
	char *dst = dst0;
	const char *src = src0;
	size_t t;
	unsigned long u;

#if !defined(_KERNEL)
	_DIAGASSERT(dst0 != 0);
	_DIAGASSERT(src0 != 0);
#endif
	if (length == 0 || dst == src)		/* nothing to do */

		goto done;

	/*

	 * Macros: loop-t-times; and loop-t-times, t>0

	 */

#define	TLOOP(s) if (t) TLOOP1(s)
#define	TLOOP1(s) do { s; } while (--t)

	if ((unsigned long)dst < (unsigned long)src) {
		/*

		 * Copy forward.

		 */

		u = (unsigned long)src;	/* only need low bits */

		if ((u | (unsigned long)dst) & wmask) {
			/*

			 * Try to align operands.  This cannot be done

			 * unless the low bits match.

			 */

			if ((u ^ (unsigned long)dst) & wmask || length < wsize)
				t = length;
			else

				t = wsize - (size_t)(u & wmask);
			length -= t;
			TLOOP1(*dst++ = *src++);
		}
		/*

		 * Copy whole words, then mop up any trailing bytes.

		 */

		t = length / wsize;
		TLOOP(*(word *)(void *)dst = *(const word *)(const void *)src; src += wsize; dst += wsize);
		t = length & wmask;
		TLOOP(*dst++ = *src++);
	} else {
		/*

		 * Copy backwards.  Otherwise essentially the same.

		 * Alignment works as before, except that it takes

		 * (t&wmask) bytes to align, not wsize-(t&wmask).

		 */

		src += length;
		dst += length;
		_DIAGASSERT((unsigned long)dst >= (unsigned long)dst0);
		_DIAGASSERT((unsigned long)src >= (unsigned long)src0);
		u = (unsigned long)src;
		if ((u | (unsigned long)dst) & wmask) {
			if ((u ^ (unsigned long)dst) & wmask || length <= wsize)
				t = length;
			else

				t = (size_t)(u & wmask);
			length -= t;
			TLOOP1(*--dst = *--src);
		}
		t = length / wsize;
		TLOOP(src -= wsize; dst -= wsize; *(word *)(void *)dst = *(const word *)(const void *)src);
		t = length & wmask;
		TLOOP(*--dst = *--src);
	}
done:
#if defined(MEMCOPY) || defined(MEMMOVE)
	return (dst0);
#else

	return;
#endif
}
 
 
`memcpy` 是 C 标准库中的一个函数,用于在两个内存块之间进行数据复制。其函数原型定义在 `<string.h>` 头文件中,通常声明为: ```c void* memcpy(void* dest, const void* src, size_t n); ``` 该函数的功能是将从源指针 `src` 指向的内存位置开始的 `n` 个字节复制到目标指针 `dest` 指向的内存区域。需要注意的是,`memcpy` 不会处理内存重叠的情况,如果需要处理内存重叠的情况,应该使用 `memmove` 函数。 ### 实现原理 `memcpy` 的实现原理主要依赖于对内存的直接操作。其核心操作是逐字节地将源内存中的数据复制到目标内存中。为了提高性能,现代实现通常会根据硬件特性进行优化,例如: - 使用指针移动来逐字节复制数据。 - 利用对齐特性,将数据以更大的单位(如 `unsigned long`)进行复制。 - 使用汇编语言或特定指令集(如 SSE、NEON)加速复制过程。 以下是一个简单的 `memcpy` 实现示例,展示其基本操作: ```c void* my_memcpy(void* dest, const void* src, size_t n) { char* d = (char*)dest; const char* s = (const char*)src; while (n--) { *d++ = *s++; } return dest; } ``` 上述代码通过逐字节复制的方式实现了 `memcpy` 的功能。首先将输入的指针转换为 `char` 类型的指针,以便按字节进行操作。然后通过一个 `while` 循环逐字节地复制数据,直到复制完 `n` 个字节。 ### 优化实现 为了提高性能,可以对上述实现进行优化。例如,可以利用更大的数据类型(如 `unsigned long`)进行复制,前提是确保内存对齐。以下是一个优化的实现示例: ```c void* my_memcpy_optimized(void* dest, const void* src, size_t n) { unsigned long* d = (unsigned long*)dest; const unsigned long* s = (const unsigned long*)src; // 复制以 unsigned long 为单位 while (n >= sizeof(unsigned long)) { *d++ = *s++; n -= sizeof(unsigned long); } // 处理剩余的字节 char* cd = (char*)d; const char* cs = (const char*)s; while (n--) { *cd++ = *cs++; } return dest; } ``` 在上述代码中,首先尝试以 `unsigned long` 为单位进行复制,这样可以减少循环的次数。当剩余的字节数不足以以 `unsigned long` 为单位复制时,再以字节为单位处理剩余的部分。 需要注意的是,这种优化方式依赖于内存对齐和硬件特性。实际的 `memcpy` 实现可能会更加复杂,并利用底层硬件指令来进一步优化性能[^1]。 ### 总结 `memcpy` 是一个用于内存复制的高效函数,其实现原理主要依赖于直接操作内存。通过逐字节复制或利用更大的数据类型进行优化,可以实现高效的内存复制操作。实际的实现可能会结合硬件特性进行进一步优化。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值