-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmemmove.c
85 lines (73 loc) · 1.71 KB
/
memmove.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright (c) 2010, ETH Zurich.
* All rights reserved.
*
* This file is distributed under the terms in the attached LICENSE file.
* If you do not find this file, copies can be found by writing to:
* ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
*/
#include <string.h>
#include <stdint.h>
#define LOWBITS (sizeof(uintptr_t)-1)
void *memmove(void *s1, const void *s2, size_t n)
{
uintptr_t from = (uintptr_t)s2;
uintptr_t to = (uintptr_t)s1;
if (to <= from) {
// Work forwards
if (((from ^ to) & LOWBITS) == 0) {
// They have the same alignment
// Copy bytes until aligned to a word boundary
while (n != 0 && ((from & LOWBITS) != 0)) {
*(char *)to = *(const char *)from;
from++;
to++;
n--;
}
// Copy words
while(n >= sizeof(uintptr_t)) {
*(uintptr_t *)to = *(const uintptr_t *)from;
from += sizeof(uintptr_t);
to += sizeof(uintptr_t);
n -= sizeof(uintptr_t);
}
}
// Copy (remaining) bytes
while (n != 0) {
*(char *)to = *(const char *)from;
from++;
to++;
n--;
}
}
else {
// Work backwards
from += n;
to += n;
if (((from ^ to) & LOWBITS) == 0) {
// They have the same alignment
// Copy bytes until aligned to a word boundary
while (n != 0 && ((from & LOWBITS) != 0)) {
from--;
to--;
*(char *)to = *(const char *)from;
n--;
}
// Copy words
while(n >= sizeof(uintptr_t)) {
from -= sizeof(uintptr_t);
to -= sizeof(uintptr_t);
*(uintptr_t *)to = *(const uintptr_t *)from;
n -= sizeof(uintptr_t);
}
}
// Copy (remaining) bytes
while (n != 0) {
from--;
to--;
*(char *)to = *(const char *)from;
n--;
}
}
return s1;
}