1 /*
2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #ifndef COMMON_UTIL_H
13 #define COMMON_UTIL_H
14
15 #include <common/types.h>
16
17 #ifdef CHCORE
18 /*
19 * memcpy does not handle: dst and src overlap.
20 * memmove does.
21 */
22 void memcpy(void *dst, const void *src, size_t size);
23 void memmove(void *dst, const void *src, size_t size);
24
25 #if defined(CHCORE_ARCH_AARCH64)
26 int __copy_to_user(void *dst, const void *src, size_t size);
27 int __copy_from_user(void *dst, const void *src, size_t size);
28 #endif
29
30 /* use bzero instead of memset(buf, 0, size) */
31 void bzero(void *p, size_t size);
32
33 void memset(void *dst, char ch, size_t size);
34
strcmp(const char * src,const char * dst)35 static inline int strcmp(const char *src, const char *dst)
36 {
37 while (*src && *dst) {
38 if (*src == *dst) {
39 src++;
40 dst++;
41 continue;
42 }
43 return *src - *dst;
44 }
45 if (!*src && !*dst)
46 return 0;
47 if (!*src)
48 return -1;
49 return 1;
50 }
51
strncmp(const char * src,const char * dst,size_t size)52 static inline int strncmp(const char *src, const char *dst, size_t size)
53 {
54 size_t i;
55
56 for (i = 0; i < size; ++i) {
57 if (src[i] == '\0' || src[i] != dst[i])
58 return src[i] - dst[i];
59 }
60
61 return 0;
62 }
63
strlen(const char * src)64 static inline size_t strlen(const char *src)
65 {
66 size_t i = 0;
67
68 while (*src++)
69 i++;
70
71 return i;
72 }
73
memcmp(const void * src,const void * dst,size_t n)74 static inline int memcmp(const void *src, const void *dst, size_t n)
75 {
76 const unsigned char *l = src, *r = dst;
77 for (; n && *l == *r; n--, l++, r++)
78 ;
79 return n ? *l - *r : 0;
80 }
81 #endif
82
83 #endif /* COMMON_UTIL_H */