-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfreestanding.cc
77 lines (66 loc) · 1.6 KB
/
freestanding.cc
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
#include "freestanding.hh"
void* memcpy(void* dst, const void* src, size_t len) {
uint8_t* dst_byte = (uint8_t*)dst;
uint8_t* src_byte = (uint8_t*)src;
for (size_t i = 0; i < len; i++) {
*dst_byte++ = *src_byte++;
}
return dst;
}
void* memmove(void* dst, const void* src, size_t len) {
if (dst == src) {
return dst;
} else if (dst < src) {
return memcpy(dst, src, len);
} else {
uint8_t* dst_byte = ((uint8_t*)dst) + len - 1;
uint8_t* src_byte = ((uint8_t*)src) + len - 1;
for (size_t i = 0; i < len; i++) {
*dst_byte++ = *src_byte++;
}
return dst;
}
}
void* memset(void* dst, int val, size_t len) {
uint8_t* dst_byte = (uint8_t*)dst;
for (size_t i = 0; i < len; i++) {
*dst_byte++ = (uint8_t)val;
}
return dst;
}
int memcmp(const void* c1, const void* c2, size_t len) {
uint8_t* c1_byte = (uint8_t*)c1;
uint8_t* c2_byte = (uint8_t*)c2;
for (size_t i = 0; i < len; i++) {
if (*c1_byte != *c2_byte) {
return *c1_byte < *c2_byte ? -1 : 1;
}
c1_byte++;
c2_byte++;
}
return 0;
}
int strlen(const char* s) {
const char* sp = s;
while (*sp++)
;
return sp - s - 1;
}
void strcpy(char* dst, const char* src) {
while (*src) {
*dst++ = *src++;
}
*dst = *src;
}
void strcat(char* dst, const char* src) {
int start_index = strlen(dst);
strcpy(dst + start_index, src);
}
bool strcmp(const char* s1, const char* s2) {
while (*s1 && *s2 && (*s1 == *s2)) {
s1++;
s2++;
}
return *s1 == *s2;
}
void operator delete(void*, unsigned int) {}