memcpy.c

来自「在x86平台上运行不可信任代码的sandbox。」· C语言 代码 · 共 51 行

C
51
字号
#include <string.h>#include <stdint.h>void *memcpy(void *dest, const void *s, size_t n){	void *d = dest;	uint32_t nn;	// It's not worth trying to optimize really short copies.	if (n < 16)		goto bytecopy;	// Try to get the dest and src pointers on a 32-byte boundary	if ((uint32_t)d & 3) {		if (((uint32_t)d & 3) != ((uint32_t)s & 3))			goto bytecopy;		nn = 4 - ((uint32_t)d & 3);		if (nn > n)			nn = n;		asm volatile("rep movsb"				: "=D" (d), "=S" (s)				: "D" (d), "S" (s), "c" (nn)				: "memory");	}	// Copy 32-bit words	if (n >> 2) {		asm volatile("rep movsl"				: "=D" (d), "=S" (s)				: "D" (d), "S" (s), "c" (n >> 2)				: "memory");		n &= 3;	}	// Copy any remaining bytes	bytecopy:	if (n > 0) {		asm volatile("rep movsb"				: "=D" (d), "=S" (s)				: "D" (d), "S" (s), "c" (n)				: "memory");	}	return dest;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?