-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemfuncs.c
62 lines (42 loc) · 1.02 KB
/
memfuncs.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
#include<stddef.h>
#include<stdint.h>
void *memcpy(void *dest, const void *src, size_t len){
char *d = dest;
const char *s = src;
while (len--)
*d++ = *s++;
return dest;
}
void *memmove(void *dest, const void *src, size_t len){
char *d = dest;
const char *s = src;
//Si chevauchement pas grave car le début de src aura déjà été recopié dans dest
if(d < s){
while(len--)
*d++ = *s++;
}
else{
const char *endsrc = s + (len-1);
char *enddest = d + (len-1);
while(len--)
*enddest++ = *endsrc++;
}
return dest;
}
void *memset(void *dest, int val, size_t len){
unsigned char *ptr = dest;
while(len-- > 0){
*ptr++ = val;
}
return dest;
}
int memcmp(const void *s1, const void *s2, size_t len){
const char *s = s1;
const char *c = s2;
for(uint8_t i=0; i<len; i++){
int diff = *(s+i) - *(c+i);
if(diff != 0)
return diff;
}
return 0;
}