-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemcpy.c
More file actions
50 lines (48 loc) · 1.36 KB
/
Copy pathmemcpy.c
File metadata and controls
50 lines (48 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//考虑内存对齐 并且一个字一个字的复制提高效率
//android内核版本
typedef long word;
#define lsize sizeof(word)
#define lmask (lsize-1)
void *memcpy(void *dest,const void *src,size_t count)
{
char *d=(char *)dest;
const char *s=(const char *)src;
int len;
if(count==0||dest==src)
return dest;
if(((long)d|(long)s)&lmask) //有一个指针不对齐
{
// src and/or dest do not align on word boundary
if((((long)d^(long)s)&lmask)||(count<lsize)) //第一个证明 两个指针对边界的差值不一样 所以只能一个个复制
{
len=count;// copy the rest of the buffer with the byte mover
}
else
{
len=lsize-((long)d&mask);// move the ptrs up to a word boundary
}
count-=len;
for(;len>0;len--)
*d++=*s++;
}
for(len=count/lsize;len>0;len--)
{
*(word *)d=*(word *)s;
d+=lsize;
s+=lsize;
}
for(len=count&lmask;len>0;len--)
{
*d++=*s++;
}
return dest;
}
void* my_memcpy(void* dest, const void* src, size_t n) {
if (dest == src) return dest; // 地址相同直接返回
unsigned char* d = (unsigned char*)dest;
const unsigned char* s = (const unsigned char*)src;
for (size_t i = 0; i < n; ++i) {
d[i] = s[i];
}
return dest;
}