了解的memcpy源$ C ​​$ C() [英] Understanding the source code of memcpy()

查看:132
本文介绍了了解的memcpy源$ C ​​$ C()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  00018无效*的memcpy(void *的DST,常量无效* SRC,为size_t LEN)
00019 {
00020为size_t我;
00021
00022 / *
00023 *的memcpy不支持重叠的缓冲区,所以总是做
00024 *前锋。 (不调整memmove与不要改变这一点。)
00025 *
00026 *对于快速复制,优化常见情况下两个指针
00027 *和长度是字对齐的,和复制字在一次一代替
00028 *字节在-A-时间。否则,复制的字节。
00029 *
00030 *下对准逻辑应该是可移植。我们依靠
00031 *编译器是相当聪明的有关优化
00032 *的分歧和模数出来。幸运的是,它是。
00033 * /
00034
00035 IF((uintptr_t形式)DST%的sizeof(长)== 0安培;&安培;
00036(uintptr_t形式)SRC%的sizeof(长)== 0安培;&安培;
00037 len个的sizeof%(长)== 0){
00038
00039长* D = DST;
00040常量长* S = SRC;
00041
00042为(i = 0; I< LEN /的sizeof(长);我++){
00043 D [i] = S [I]
00044}
00045}
00046其他{
00047的char * D = DST;
00048为const char * S = SRC;
00049
00050为(i = 0; I< LEN,我++){
00051 D [i] = S [I]
00052}
00053}
00054
00055 DST的回报;
00056}

我只是通过的memcpy 的实施下去,以了解它使用一个循环的区别。但我看不出使用一个循环而不是的memcpy ,因为的memcpy 再利用循环内部复制任何区别

我不知道他们在整数&MDASH做如果部分; I< LEN /的sizeof(长)。为什么需要这种计算?


解决方案

  

如果一部分,他们为整数做我无法理解。 I< LEN /的sizeof(长)。为什么需要这种计算?


由于它们被复制的话,而不是单个字节,在这种情况下,(作为评论说,这是一个优化 - 它需要较少的迭代和CPU可以更有效地处理字对齐的数据)。

LEN 是字节的数量,以复制和的sizeof(长)就是一个字的的大小,所以元素复制(手段,循环迭代执行)的数量 LEN /的sizeof(长)

00018 void *memcpy(void *dst, const void *src, size_t len)
00019 {
00020         size_t i;
00021 
00022         /*
00023          * memcpy does not support overlapping buffers, so always do it
00024          * forwards. (Don't change this without adjusting memmove.)
00025          *
00026          * For speedy copying, optimize the common case where both pointers
00027          * and the length are word-aligned, and copy word-at-a-time instead
00028          * of byte-at-a-time. Otherwise, copy by bytes.
00029          *
00030          * The alignment logic below should be portable. We rely on
00031          * the compiler to be reasonably intelligent about optimizing
00032          * the divides and modulos out. Fortunately, it is.
00033          */
00034 
00035         if ((uintptr_t)dst % sizeof(long) == 0 &&
00036             (uintptr_t)src % sizeof(long) == 0 &&
00037             len % sizeof(long) == 0) {
00038 
00039                 long *d = dst;
00040                 const long *s = src;
00041 
00042                 for (i=0; i<len/sizeof(long); i++) {
00043                         d[i] = s[i];
00044                 }
00045         }
00046         else {
00047                 char *d = dst;
00048                 const char *s = src;
00049 
00050                 for (i=0; i<len; i++) {
00051                         d[i] = s[i];
00052                 }
00053         }
00054 
00055         return dst;
00056 }

I was just going through an implementation of memcpy, to understand how it differs from using a loop. But I couldn't see any difference between using a loop rather than memcpy, as memcpy uses loop again internally to copy.

I couldn't understand if part they do for integers — i < len/sizeof(long). Why is this calculation required?

解决方案

I couldn't understand if part they do for integers. i < len/sizeof(long). Why is this calculation required ?

Because they are copying words, not individual bytes, in this case (as the comment says, it is an optimization - it requires less iterations and the CPU can handle word aligned data more efficiently).

len is the number of bytes to copy, and sizeof(long) is the size of a single word, so the number of elements to copy (means, loop iterations to execute) is len / sizeof(long).

这篇关于了解的memcpy源$ C ​​$ C()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆