这两个函数用于拷贝字符串或者一段连续的内存,函数原型:
void * memcpy ( void * destination, const void * source, size_t num );
void * memmove ( void * destination, const void * source, size_t num ); 这里有一点需要注意:num指的是需要拷贝的字节数,所以在将void*转型成实际的类型的时候一定要考虑重新计算拷贝的单元数 比如,转成WORD型,则实际需要拷贝的单元数位num / 2 参看glibc里面对于这两个函数的实现:
void* memmove(void* dest, const void* src, size_t len) { unsigned long int dstp = (long int) dest; unsigned long int srcp = (long int) src; /* This test makes the forward copying code be used whenever possible. Reduces the working set. */ if (dstp - srcp >= len) /* *Unsigned* compare! */ { /* Copy from the beginning to the end. */ /* If there not too few bytes to copy, use word copy. */ if (len >= OP_T_THRES) { /* Copy just a few bytes to make DSTP aligned. */ 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. */ 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); } else { /* Copy from the end to the beginning. */ srcp += len; dstp += len; /* If there not too few bytes to copy, use word copy. */ if (len >= OP_T_THRES) { /* Copy just a few bytes to make DSTP aligned. */ len -= dstp % OPSIZ; BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ); /* 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. */ WORD_COPY_BWD (dstp, srcp, len, len); /* Fall out and copy the tail. */ } /* There are just a few bytes to copy. Use byte memory operations. */ BYTE_COPY_BWD (dstp, srcp, len); } return dest; }
void * memcpy (void* dst, const void* src, size_t len) { unsigned long int dstp = (long int) dst; unsigned long int srcp = (long int) src; /* Copy from the beginning to the end. */ /* If there not too few bytes to copy, use word copy. */ if (len >= OP_T_THRES) { /* Copy just a few bytes to make DSTP aligned. */ 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. */ 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 dst; }
可以发现memcpy比memmove少了检查destp - srcp >= len的部分,这带来了memmove的优越之处:可以处理目的地址于源地址重叠的情形!
系统内置的memmove和memcpy是利用汇编优化的,当自己实现的时候,可以这么写:
void* n_memmove(void *dst, const void *src, size_t len) { char* dstp = (char*)dst; char* srcp = (char*)src; if (len == 0) return dst; assert(dst != NULL && src != NULL); if (dstp - srcp >= len) { //byte_copy_forward(dstp, srcp, len); for (int i = 0; i < len; i++) dstp[i] = srcp[i]; } else { //copy from the end to the beginning //byte_copy_bwd(dstp, srcp, len); for (int i = len - 1; i >= 0; i--) dstp[i] = srcp[i]; } return dst; } void* n_memcpy(void* dst, const void* src, size_t len) { char* dstp = (char*)dst; char* srcp = (char*)src; assert(dst != NULL && src != NULL); if (len == 0) return dst; //byte_copy_forwar(dstp, srcp, len); for (int i = 0; i < len; i++) dstp[i] = srcp[i]; return dst; }
时间: 2024-10-09 23:00:54