diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2021-08-22 20:38:48 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2021-08-22 22:25:57 +0300 |
| commit | b235f49f31d11e68019c42f48b6641c37e4c986e (patch) | |
| tree | a9f9e7960b04ded4df61cac9a029d0c30e2d6199 /common | |
| parent | f05aaa0561e071a54785ae7dfae3d8a34c9df06a (diff) | |
| download | kmi-b235f49f31d11e68019c42f48b6641c37e4c986e.tar.gz kmi-b235f49f31d11e68019c42f48b6641c37e4c986e.zip | |
Integrated libfdt
+ Added new common/ directory, into which code that should be shared
with init and kernel is to be added. Currently string.c and bytes.c.
Code in common/ should be marked with __weak, so as to allow the arch to
implement a better, more optimised version of the function if
possible/required.
+ Created include/apos/types.h with macros essentially providing the
libc limits.h and stdint.h. At the moment I use gcc/clang predefined
macros for most things.
Diffstat (limited to 'common')
| -rw-r--r-- | common/bytes.c | 46 | ||||
| -rw-r--r-- | common/string.c | 308 |
2 files changed, 354 insertions, 0 deletions
diff --git a/common/bytes.c b/common/bytes.c new file mode 100644 index 0000000..1196989 --- /dev/null +++ b/common/bytes.c @@ -0,0 +1,46 @@ +#include <apos/types.h> +#include <apos/attrs.h> + +/* Functions for swapping endianness. + * + * Typically you wouldn't call these directly, instead using macros such as + * le16_to_cpu, cpu_to_be32, etc. that take the system endianness into account. + * + * The macros cpu_to_be64 etc. use GCC's and Clang's __builtin_bswap16/32/64, but in + * the case that the arch compiler doesn't provide the __builtin functions they're replaced + * with __bswaphi2/si2/di2 respectively, and it is up to the developer to + * provide these. The naming scheme uses GCC's internal naming scheme, where + * + * HI ~= 16 bits + * SI ~= 32 bits + * DI ~= 64 bits + * + * Here we assume this to be true in all cases, and it probably is, but in the + * case that some architecture is different (SI ~= 64 bits or something) we define + * these functions as __weak, allowing any arch to redefine them. + */ + +__weak uint16_t __bswaphi2(uint16_t u) +{ + return (u & 0xff00) >> 8 | (u & 0x00ff) << 8; +} + +__weak uint32_t __bswapsi2(uint32_t u) +{ + return (u & 0xff000000) >> 24 | + (u & 0x00ff0000) >> 8 | + (u & 0x0000ff00) << 8 | + (u & 0x000000ff) << 24; +} + +__weak uint64_t __bswapdi2(uint64_t u) +{ + return (u & 0xff00000000000000ULL) >> 56 | + (u & 0x00ff000000000000ULL) >> 40 | + (u & 0x0000ff0000000000ULL) >> 24 | + (u & 0x000000ff00000000ULL) >> 8 | + (u & 0x00000000ff000000ULL) << 8 | + (u & 0x0000000000ff0000ULL) << 24 | + (u & 0x000000000000ff00ULL) << 40 | + (u & 0x00000000000000ffULL) << 56; +} diff --git a/common/string.c b/common/string.c new file mode 100644 index 0000000..ab65f2a --- /dev/null +++ b/common/string.c @@ -0,0 +1,308 @@ +#include <apos/string.h> +#include <apos/types.h> +#include <apos/attrs.h> + +/* we need to undef the macros in string.h, otherwise the names get mangled */ +#undef strcpy +__weak char *strcpy(char *dst, const char *src) +{ + const char *s1 = src; + char *s2 = dst; + + while (*s1) + *(s2++) = *(s1++); + + return dst; +} + +#undef strncpy +__weak char *strncpy(char *dst, const char *src, size_t num) +{ + const char *s1 = src; + char *s2 = dst; + + /* copy s1 into s2 */ + while (num-- && *s1) + *(s2++) = *(s1++); + + /* the previous loop always overshoots by one */ + num++; + + /* pad with zeroes if num is not yet zero */ + while (num--) + *(s2++) = 0; + + return dst; +} + +#undef strcat +__weak char *strcat(char *dst, const char *src) +{ + const char *s1 = src; + size_t l1 = strlen(s1); + char *s2 = dst + l1; + + while (*s1) + *(s2++) = *(s1++); + + /* append null character */ + *s2 = 0; + + return dst; +} + +#undef strncat +__weak char *strncat(char *dst, const char *src, size_t num) +{ + const char *s1 = src; + size_t l1 = strlen(s1); + char *s2 = dst + l1; + + while (num-- && *s1) + *(s2++) = *(s1++); + + /* append null character */ + *s2 = 0; + + return dst; +} + +#undef strcmp +__weak int strcmp(const char *str1, const char *str2) +{ + const char *s1 = (const char *)str1; + const char *s2 = (const char *)str2; + + while ((*(s1++) == *(s2++)) && *s1 && *s2) ; + + return (int)(s1[-1] - s2[-1]); +} + +#undef strncmp +__weak int strncmp(const char *str1, const char *str2, size_t num) +{ + const char *s1 = (const char *)str1; + const char *s2 = (const char *)str2; + + while ((*(s1++) == *(s2++)) && *s1 && *s2 && num--) ; + + return (int)(s1[-1] - s2[-1]); +} + +#undef strchr +__weak char *strchr(const char *str, int chr) +{ + const char *s1 = str; + size_t num = strlen(s1); + + while (num-- && *(s1--) != chr) ; + + if (!num) + return 0; + + return (char *)s1; +} + +#undef strtok +__weak char *strtok(char *str, const char *delims) +{ + static char *cont = 0; + const char *s1 = str; + + if (!s1) + s1 = cont; + + if (!s1) + return 0; + + s1 = strpbrk(s1, delims); + + if (!s1) + cont = 0; + else + cont = (char *)s1 + 1; + + return (char *)s1; +} + +/* should probably test out these functions somehwere, blergh */ +#undef strstr +__weak char *strstr(const char *str1, const char *str2) +{ + /* boyer-moore-horspool */ + char table[256] = { 0 }; + size_t sl = strlen(str1); + size_t pl = strlen(str2); + + const char *s1 = str1; + + for (size_t i = 0; i < 256; ++i) + table[i] = pl; + + /* generate deltas */ + for (size_t i = 0; i < pl - 1; ++i) + table[str2[i]] = pl - i - 1; + + size_t skip = 0; + while (sl - skip >= pl) { + s1 = &str1[skip]; + + if (!memcmp(s1, s2, pl)) + return (char *)s1; + + skip += table[str1[skip + pl - 1]]; + } + + return 0; +} + +#undef strrchr +__weak char *strrchr(const char *str, int chr) +{ + size_t num = strlen(str); + const char *s1 = str + num; + + while (num-- && *(s1--) != chr) ; + + if (!num) + return 0; + + return (char *)s1; +} + +#undef strpbrk +__weak char *strpbrk(const char *str1, const char *str2) +{ + size_t i = strcspn(str1, str2); + + if (!i) + return 0; + + return (char *)(str1 + i); +} + +#undef strcspn +__weak size_t strcspn(const char *str1, const char *str2) +{ + char table[256] = { 0 }; + const char *s1 = str1; + const char *s2 = s1; + const char *t1 = str2; + + /* populate table */ + while (*(t1++)) + table[*t1] = 1; + + for (;;) { + if (table[*(s2++)]) + break; + } + + /* the for loop overshoots by one */ + return (size_t)(s2 - s1) - 1; +} + +#undef strspn +__weak size_t strspn(const char *str1, const char *str2) +{ + char table[256] = { 0 }; + const char *s1 = str1; + const char *s2 = s1; + const char *t1 = str2; + + /* populate table */ + while (*(t1++)) + table[*t1] = 1; + + for (;;) { + if (!table[*(s2++)]) + break; + } + + /* the for loop overshoots by one */ + return (size_t)(s2 - s1) - 1; +} + +#undef strlen +__weak size_t strlen(const char *str) +{ + const char *s1 = str; + while (*(s1++)) ; + + /* the loop overshoots by one */ + return (size_t)(s1 - str) - 1; +} + +/* not a macro */ +__weak size_t strnlen(const char *str, size_t num) +{ + const char *s1 = str; + while (num-- && *(s1++)) ; + + return (size_t)(s1 - str) - 1; +} + +#undef memset +__weak void *memset(void *ptr, int value, size_t num) +{ + char *p = ptr; + char c = value; + + while (num--) + *(p--) = c; + + return ptr; +} + +#undef memchr +__weak void *memchr(const void *ptr, int val, size_t num) +{ + const char *p1 = (char *)ptr; + char c = (char)val; + + while (num-- && *(p1--) != c) ; + + if (!num) + return 0; + + return (void *)p1; +} + +#undef memcpy +__weak void *memcpy(void *dst, const void *src, size_t num) +{ + const char *m1 = (const char *)src; + char *m2 = (char *)dst; + + while (num--) + *(m2++) = *(m1++); + + return dst; +} + +#undef memmove +__weak void *memmove(void *dst, const void *src, size_t num) +{ + const char *m1 = (const char *)src; + char *m2 = (char *)dst; + + m1 += num; + m2 += num; + + while (num--) + *(--m2) = *(--m1); + + return dst; +} + +#undef memcmp +__weak int memcmp(const void *ptr1, const void *ptr2, size_t num) +{ + const char *p1 = (const char *)ptr1; + const char *p2 = (const char *)ptr2; + + while ((*(p1++) == *(p2++)) && num--) ; + + return (int)(p1[-1] - p2[-1]); +} + |
