鉴于博主我是个汇编渣渣(底层逻辑是大佬们用汇编写的,淦),本文不是要深入探析memcpy的细节,而仅仅是在宏观层面上搞清楚这个函数的逻辑。
如果有想搞清楚具体细节的友友们,我会在文章最下面贴上我写本文参考的文章,如果还想深入探究,最好是参阅书籍。
- 函数原型:
void *memcpy (void *dstpp, const void *srcpp, size_t len)
-
函数作用:
将srcpp指向内存的后面len个字节拷贝 到 以dstpp指向内存的后面len个字节中。
既然是以字节为操作单位,那么就不会局限于某种数据类型,char可以,int也可以,结构体当然也行。 -
函数执行过程:
1.首先将dstpp和srcpp转换为无符号长整型(这个后面是有特殊目的的)。
2.判断len是否大于等于OP_T_THRES,如果成立,则进行分段拷贝,否则直接进行字节拷贝。
3.假设len大于等于OP_T_THRES成立了,则首先使用表达式(-dstp) % OPSIZ
算出srcpp到最近的内存对齐地址的距离。这个地方是个特殊的逻辑,也是上面将地址转换为无符号长整型的目的。
4.使用字节拷贝来拷贝前面没有内存对齐的字节。
5.如果有可能的话,下一步使用页拷贝,这个效率最高了。
6.页拷贝做完后,使用字拷贝,比如:32位系统中一次拷贝4个字节。
7.如果做完了字拷贝,数据还没有拷贝完,则再次使用字节拷贝来拷贝完剩余字节。
(注:上面的拷贝方式都是宏,宏里是用汇编编写的逻辑。) -
流程图:
-
下面是加了个人理解的源代码:(英文部分为原作者注释)
/* Copy memory to memory until the specified number of bytes
has been copied. Overlap is NOT handled correctly.
Copyright (C) 1991-2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Torbjorn Granlund (tege@sics.se).
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <memcopy.h>
#ifndef MEMCPY
# define MEMCPY memcpy
#endif
void *
MEMCPY (void *dstpp, const void *srcpp, size_t len)
{
//首先将指针转换为无符号长整型
unsigned long int dstp = (long int) dstpp;
unsigned long int srcp = (long int) srcpp;
/* Copy from the beginning to the end. */
/* If there not too few bytes to copy, use word copy. */
//如果拷贝长度大于等于OP_T_THRES,则进行分段拷贝,OP_T_THRES定义再sysdeps文件夹下的
//memcopy.h中,一般取16或者8
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
//(-dstp) % OPSIZ是srcpp到最近的内存对齐地址的距离
len -= (-dstp) % OPSIZ;
//前面没有对齐的内存使用字节拷贝
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
/* Copy whole pages from SRCP to DSTP by virtual address manipulation,
as much as possible. */
//如果有可能的话,使用页拷贝,这个效率最高了
PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
/* Copy from SRCP to DSTP taking advantage of the known alignment of
DSTP. Number of bytes remaining is put in the third argument,
i.e. in LEN. This number may vary from machine to machine. */
//页拷贝做完后,使用字拷贝,32位系统中一次拷贝4个字节
WORD_COPY_FWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
//如果尾部还有没对齐的字节,再使用字节拷贝
BYTE_COPY_FWD (dstp, srcp, len);
return dstpp;
}
libc_hidden_builtin_def (MEMCPY)
参考文献:
1.glibc memcpy() 源码浅谈
2.带你深入理解内存对齐最底层原理