Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the source code of memcpy()

Tags:

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?

like image 675
Angus Avatar asked Jul 11 '13 11:07

Angus


People also ask

What is memcpy and explain the syntax?

memcpy() function is an inbuilt function in C++ STL, which is defined in <cstring> header file. memcpy() function is used to copy blocks of memory. This function is used to copy the number of values from one memory location to another. The result of the function is a binary copy of the data.

How memcpy function works?

memcpy() function in C/C++ The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.

How is memcpy implemented in C?

How to implement your own memcpy implementation in C? Implementation of memcpy is not a big deal, you need to typecast the given source and destination address to char* (1 byte). After the typecasting copy the data from the source to destination one by one until n (given length).

What is memcpy used for in C++?

The memcpy() function in C++ copies specified bytes of data from the source to the destination. It is defined in the cstring header file.


2 Answers

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

like image 98
Andreas Fester Avatar answered Oct 27 '22 11:10

Andreas Fester


to understand how it differs from using a loop. But I couldn't any difference of using a loop rather than memcpy, as memcpy uses loop again internally to copy

Well then it uses a loop. Maybe other implementations of libc doesn't do it like that. Anyway, what's the problem/question if it does use a loop? Also as you see it does more than a loop: it checks for alignment and performs a different kind of loop depending on the alignment.

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

This is checking for memory word alignment. If the destination and source addresses are word-aligned, and the length copy is multiple of word-size, then it performs an aligned copy by word (long), which is faster than using bytes (char), not only because of the size, but also because most architectures do word-aligned copies much faster.

like image 27
m0skit0 Avatar answered Oct 27 '22 12:10

m0skit0