Search code examples
cperformancegnuglibcmemcpy

Understanding page copying in C


I heard that memcpy was a very efficient algorithm for copying data. To try to learn some lessons for my code, I decided to study its implementation in GNU. However, for large memory blocks, it does some checks, and then calls a PAGE_COPY_FWD macro-- which I cannot find defined anywhere.

Last link of the source code of mem_cpy says:

System-specific pagecopy.h files should define these macros and then
  #include this file:

  ...

  PAGE_COPY_FWD (dstp, srcp, nbytes_left, nbytes)

Two questions:

1) How can I find the implementation of this macro? (anywhere-- either on my machine, or on someone else's-- even if that means I have to read the assembly code.)

2) What do I have to include to be able to call this macro directly, i.e. without going through memcpy first?


Solution

  • I found this definition of PAGE_COPY_FWD

    #define PAGE_COPY_FWD(dstp, srcp, nbytes_left, nbytes)  \ 
         ((nbytes_left) = ((nbytes) - \
          (__vm_copy (__mach_task_self (),  \
           (vm_address_t) srcp, trunc_page (nbytes), \
            (vm_address_t) dstp) == KERN_SUCCESS    \
                     ? trunc_page (nbytes) \
                     : 0)))
    

    Found it here https://www.sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/mach/pagecopy.h