[klibc] organize and renmae macros, and add CI check

This commit is contained in:
Meco Man 2024-04-20 15:25:17 -04:00
parent 8149eb2e1f
commit d68e47eb25
7 changed files with 85 additions and 65 deletions

View File

@ -0,0 +1,3 @@
# scons: --strict
CONFIG_RT_KLIBC_USING_PRINTF_LONGLONG=y

View File

@ -0,0 +1,4 @@
# scons: --strict
CONFIG_RT_KLIBC_USING_STDLIB=y
CONFIG_RT_KLIBC_USING_STDLIB_MEMORY=y

View File

@ -0,0 +1,3 @@
# scons: --strict
CONFIG_RT_KLIBC_USING_TINY_SIZE=y

View File

@ -19,7 +19,6 @@ extern "C" {
#endif #endif
/* kstdio */ /* kstdio */
int rt_vsprintf(char *dest, const char *format, va_list arg_ptr); int rt_vsprintf(char *dest, const char *format, va_list arg_ptr);
int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args); int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args);
int rt_sprintf(char *buf, const char *format, ...); int rt_sprintf(char *buf, const char *format, ...);
@ -37,15 +36,12 @@ const char *rt_strerror(rt_err_t error);
/* kstring */ /* kstring */
#ifndef RT_KSERVICE_USING_STDLIB_MEMORY
void *rt_memset(void *src, int c, rt_ubase_t n); void *rt_memset(void *src, int c, rt_ubase_t n);
void *rt_memcpy(void *dest, const void *src, rt_ubase_t n); void *rt_memcpy(void *dest, const void *src, rt_ubase_t n);
void *rt_memmove(void *dest, const void *src, rt_size_t n); void *rt_memmove(void *dest, const void *src, rt_size_t n);
rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count); rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count);
#endif /* RT_KSERVICE_USING_STDLIB_MEMORY */
char *rt_strdup(const char *s); char *rt_strdup(const char *s);
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen); rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen);
#ifndef RT_KSERVICE_USING_STDLIB
char *rt_strstr(const char *str1, const char *str2); char *rt_strstr(const char *str1, const char *str2);
rt_int32_t rt_strcasecmp(const char *a, const char *b); rt_int32_t rt_strcasecmp(const char *a, const char *b);
char *rt_strcpy(char *dst, const char *src); char *rt_strcpy(char *dst, const char *src);
@ -53,22 +49,6 @@ char *rt_strncpy(char *dest, const char *src, rt_size_t n);
rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count); rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count);
rt_int32_t rt_strcmp(const char *cs, const char *ct); rt_int32_t rt_strcmp(const char *cs, const char *ct);
rt_size_t rt_strlen(const char *src); rt_size_t rt_strlen(const char *src);
#else
#include <string.h>
#ifdef RT_KSERVICE_USING_STDLIB_MEMORY
#define rt_memset(s, c, count) memset(s, c, count)
#define rt_memcpy(dst, src, count) memcpy(dst, src, count)
#define rt_memmove(dest, src, n) memmove(dest, src, n)
#define rt_memcmp(cs, ct, count) memcmp(cs, ct, count)
#endif /* RT_KSERVICE_USING_STDLIB_MEMORY */
#define rt_strstr(str1, str2) strstr(str1, str2)
#define rt_strcasecmp(a, b) strcasecmp(a, b)
#define rt_strcpy(dest, src) strcpy(dest, src)
#define rt_strncpy(dest, src, n) strncpy(dest, src, n)
#define rt_strncmp(cs, ct, count) strncmp(cs, ct, count)
#define rt_strcmp(cs, ct) strcmp(cs, ct)
#define rt_strlen(src) strlen(src)
#endif /*RT_KSERVICE_USING_STDLIB*/
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -182,28 +182,30 @@ if RT_USING_TIMER_SOFT
endif endif
menu "kservice optimization" menu "kservice optimization"
config RT_USING_TINY_FFS
bool "Enable kservice to use tiny finding first bit set method"
default n
endmenu
config RT_KSERVICE_USING_STDLIB menu "klibc optimization"
bool "Enable kservice to use standard C library"
default y
if RT_KSERVICE_USING_STDLIB config RT_KLIBC_USING_STDLIB
config RT_KSERVICE_USING_STDLIB_MEMORY bool "Enable klibc to use standard C library"
default n
if RT_KLIBC_USING_STDLIB
config RT_KLIBC_USING_STDLIB_MEMORY
bool "Use stdlib memory functions to replace (faster, but not safe)" bool "Use stdlib memory functions to replace (faster, but not safe)"
default n default n
help help
e.g. use memcpy to replace rt_memcpy e.g. use memcpy to replace rt_memcpy
endif endif
config RT_KSERVICE_USING_TINY_SIZE config RT_KLIBC_USING_TINY_SIZE
bool "Enable kservice to use tiny size" bool "Enable tiny size of klibc"
default n default n
config RT_USING_TINY_FFS config RT_KLIBC_USING_PRINTF_LONGLONG
bool "Enable kservice to use tiny finding first bit set method"
default n
config RT_KPRINTF_USING_LONGLONG
bool "Enable rt_printf-family functions to support long-long format" bool "Enable rt_printf-family functions to support long-long format"
default y if ARCH_CPU_64BIT default y if ARCH_CPU_64BIT
default n default n

View File

@ -32,16 +32,16 @@
* *
* @return the duplicated string pointer. * @return the duplicated string pointer.
*/ */
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
rt_inline int divide(unsigned long long *n, int base) rt_inline int divide(unsigned long long *n, int base)
#else #else
rt_inline int divide(unsigned long *n, int base) rt_inline int divide(unsigned long *n, int base)
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
{ {
int res; int res;
/* optimized for processor which does not support divide instructions. */ /* optimized for processor which does not support divide instructions. */
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
res = (int)((*n) % base); res = (int)((*n) % base);
*n = (long long)((*n) / base); *n = (long long)((*n) / base);
#else #else
@ -71,11 +71,11 @@ rt_inline int skip_atoi(const char **s)
static char *print_number(char *buf, static char *print_number(char *buf,
char *end, char *end,
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
unsigned long long num, unsigned long long num,
#else #else
unsigned long num, unsigned long num,
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int base, int base,
int qualifier, int qualifier,
int s, int s,
@ -83,11 +83,11 @@ static char *print_number(char *buf,
int type) int type)
{ {
char c = 0, sign = 0; char c = 0, sign = 0;
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
char tmp[64] = {0}; char tmp[64] = {0};
#else #else
char tmp[32] = {0}; char tmp[32] = {0};
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int precision_bak = precision; int precision_bak = precision;
const char *digits = RT_NULL; const char *digits = RT_NULL;
static const char small_digits[] = "0123456789abcdef"; static const char small_digits[] = "0123456789abcdef";
@ -307,11 +307,11 @@ static char *print_number(char *buf,
*/ */
rt_weak int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args) rt_weak int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
{ {
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
unsigned long long num = 0; unsigned long long num = 0;
#else #else
unsigned long num = 0; unsigned long num = 0;
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int i = 0, len = 0; int i = 0, len = 0;
char *str = RT_NULL, *end = RT_NULL, c = 0; char *str = RT_NULL, *end = RT_NULL, c = 0;
const char *s = RT_NULL; const char *s = RT_NULL;
@ -402,20 +402,20 @@ rt_weak int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list arg
qualifier = 0; /* get the conversion qualifier */ qualifier = 0; /* get the conversion qualifier */
if (*fmt == 'h' || *fmt == 'l' || if (*fmt == 'h' || *fmt == 'l' ||
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
*fmt == 'L' || *fmt == 'L' ||
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
*fmt == 'z') *fmt == 'z')
{ {
qualifier = *fmt; qualifier = *fmt;
++fmt; ++fmt;
#ifdef RT_KPRINTF_USING_LONGLONG #ifdef RT_KLIBC_USING_PRINTF_LONGLONG
if (qualifier == 'l' && *fmt == 'l') if (qualifier == 'l' && *fmt == 'l')
{ {
qualifier = 'L'; qualifier = 'L';
++fmt; ++fmt;
} }
#endif /* RT_KPRINTF_USING_LONGLONG */ #endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
if (qualifier == 'h' && *fmt == 'h') if (qualifier == 'h' && *fmt == 'h')
{ {
qualifier = 'H'; qualifier = 'H';

View File

@ -12,6 +12,9 @@
#include <rtcompiler.h> #include <rtcompiler.h>
#include <rtklibc.h> #include <rtklibc.h>
#include <rtm.h> #include <rtm.h>
#ifdef RT_KLIBC_USING_STDLIB
#include <string.h>
#endif /* RT_KLIBC_USING_STDLIB */
#define DBG_TAG "kernel.string" #define DBG_TAG "kernel.string"
#ifdef RT_DEBUG_DEVICE #ifdef RT_DEBUG_DEVICE
@ -21,7 +24,6 @@
#endif /* defined (RT_DEBUG_DEVICE) */ #endif /* defined (RT_DEBUG_DEVICE) */
#include <rtdbg.h> #include <rtdbg.h>
#ifndef RT_KSERVICE_USING_STDLIB_MEMORY
/** /**
* @brief This function will set the content of memory to specified value. * @brief This function will set the content of memory to specified value.
* *
@ -36,7 +38,9 @@
*/ */
rt_weak void *rt_memset(void *s, int c, rt_ubase_t count) rt_weak void *rt_memset(void *s, int c, rt_ubase_t count)
{ {
#ifdef RT_KSERVICE_USING_TINY_SIZE #if defined(RT_KLIBC_USING_STDLIB_MEMORY)
return memset(s, c, count);
#elif defined(RT_KLIBC_USING_TINY_SIZE)
char *xs = (char *)s; char *xs = (char *)s;
while (count--) while (count--)
@ -99,7 +103,7 @@ rt_weak void *rt_memset(void *s, int c, rt_ubase_t count)
#undef LBLOCKSIZE #undef LBLOCKSIZE
#undef UNALIGNED #undef UNALIGNED
#undef TOO_SMALL #undef TOO_SMALL
#endif /* RT_KSERVICE_USING_TINY_SIZE */ #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
} }
RTM_EXPORT(rt_memset); RTM_EXPORT(rt_memset);
@ -116,7 +120,9 @@ RTM_EXPORT(rt_memset);
*/ */
rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count) rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
{ {
#ifdef RT_KSERVICE_USING_TINY_SIZE #if defined(RT_KLIBC_USING_STDLIB_MEMORY)
return memcpy(dst, src, count);
#elif defined(RT_KLIBC_USING_TINY_SIZE)
char *tmp = (char *)dst, *s = (char *)src; char *tmp = (char *)dst, *s = (char *)src;
rt_ubase_t len = 0; rt_ubase_t len = 0;
@ -183,7 +189,7 @@ rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
#undef BIGBLOCKSIZE #undef BIGBLOCKSIZE
#undef LITTLEBLOCKSIZE #undef LITTLEBLOCKSIZE
#undef TOO_SMALL #undef TOO_SMALL
#endif /* RT_KSERVICE_USING_TINY_SIZE */ #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
} }
RTM_EXPORT(rt_memcpy); RTM_EXPORT(rt_memcpy);
@ -202,6 +208,9 @@ RTM_EXPORT(rt_memcpy);
*/ */
void *rt_memmove(void *dest, const void *src, rt_size_t n) void *rt_memmove(void *dest, const void *src, rt_size_t n)
{ {
#ifdef RT_KLIBC_USING_STDLIB_MEMORY
return memmove(dest, src, n);
#else
char *tmp = (char *)dest, *s = (char *)src; char *tmp = (char *)dest, *s = (char *)src;
if (s < tmp && tmp < s + n) if (s < tmp && tmp < s + n)
@ -219,6 +228,7 @@ void *rt_memmove(void *dest, const void *src, rt_size_t n)
} }
return dest; return dest;
#endif /* RT_KLIBC_USING_STDLIB_MEMORY */
} }
RTM_EXPORT(rt_memmove); RTM_EXPORT(rt_memmove);
@ -238,6 +248,9 @@ RTM_EXPORT(rt_memmove);
*/ */
rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count) rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count)
{ {
#ifdef RT_KLIBC_USING_STDLIB_MEMORY
return memcmp(cs, ct, count);
#else
const unsigned char *su1 = RT_NULL, *su2 = RT_NULL; const unsigned char *su1 = RT_NULL, *su2 = RT_NULL;
int res = 0; int res = 0;
@ -246,11 +259,10 @@ rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count)
break; break;
return res; return res;
#endif /* RT_KLIBC_USING_STDLIB_MEMORY */
} }
RTM_EXPORT(rt_memcmp); RTM_EXPORT(rt_memcmp);
#endif /* RT_KSERVICE_USING_STDLIB_MEMORY*/
#ifndef RT_KSERVICE_USING_STDLIB
/** /**
* @brief This function will return the first occurrence of a string, without the * @brief This function will return the first occurrence of a string, without the
* terminator '\0'. * terminator '\0'.
@ -263,6 +275,9 @@ RTM_EXPORT(rt_memcmp);
*/ */
char *rt_strstr(const char *s1, const char *s2) char *rt_strstr(const char *s1, const char *s2)
{ {
#ifdef RT_KLIBC_USING_STDLIB
return strstr(s1, s2);
#else
int l1 = 0, l2 = 0; int l1 = 0, l2 = 0;
l2 = rt_strlen(s2); l2 = rt_strlen(s2);
@ -284,6 +299,7 @@ char *rt_strstr(const char *s1, const char *s2)
} }
return RT_NULL; return RT_NULL;
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strstr); RTM_EXPORT(rt_strstr);
@ -331,6 +347,9 @@ RTM_EXPORT(rt_strcasecmp);
*/ */
char *rt_strncpy(char *dst, const char *src, rt_size_t n) char *rt_strncpy(char *dst, const char *src, rt_size_t n)
{ {
#ifdef RT_KLIBC_USING_STDLIB
return strncpy(dst, src, n);
#else
if (n != 0) if (n != 0)
{ {
char *d = dst; char *d = dst;
@ -352,6 +371,7 @@ char *rt_strncpy(char *dst, const char *src, rt_size_t n)
} }
return (dst); return (dst);
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strncpy); RTM_EXPORT(rt_strncpy);
@ -366,6 +386,9 @@ RTM_EXPORT(rt_strncpy);
*/ */
char *rt_strcpy(char *dst, const char *src) char *rt_strcpy(char *dst, const char *src)
{ {
#ifdef RT_KLIBC_USING_STDLIB
return strcpy(dst, src);
#else
char *dest = dst; char *dest = dst;
while (*src != '\0') while (*src != '\0')
@ -377,6 +400,7 @@ char *rt_strcpy(char *dst, const char *src)
*dst = '\0'; *dst = '\0';
return dest; return dest;
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strcpy); RTM_EXPORT(rt_strcpy);
@ -396,11 +420,14 @@ RTM_EXPORT(rt_strcpy);
*/ */
rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count) rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count)
{ {
signed char __res = 0; #ifdef RT_KLIBC_USING_STDLIB
return strncmp(cs, ct, count);
#else
signed char res = 0;
while (count) while (count)
{ {
if ((__res = *cs - *ct++) != 0 || !*cs++) if ((res = *cs - *ct++) != 0 || !*cs++)
{ {
break; break;
} }
@ -408,7 +435,8 @@ rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count)
count --; count --;
} }
return __res; return res;
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strncmp); RTM_EXPORT(rt_strncmp);
@ -426,6 +454,9 @@ RTM_EXPORT(rt_strncmp);
*/ */
rt_int32_t rt_strcmp(const char *cs, const char *ct) rt_int32_t rt_strcmp(const char *cs, const char *ct)
{ {
#ifdef RT_KLIBC_USING_STDLIB
return strcmp(cs, ct);
#else
while (*cs && *cs == *ct) while (*cs && *cs == *ct)
{ {
cs++; cs++;
@ -433,6 +464,7 @@ rt_int32_t rt_strcmp(const char *cs, const char *ct)
} }
return (*cs - *ct); return (*cs - *ct);
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strcmp); RTM_EXPORT(rt_strcmp);
@ -446,17 +478,16 @@ RTM_EXPORT(rt_strcmp);
*/ */
rt_size_t rt_strlen(const char *s) rt_size_t rt_strlen(const char *s)
{ {
#ifdef RT_KLIBC_USING_STDLIB
return strlen(s);
#else
const char *sc = RT_NULL; const char *sc = RT_NULL;
for (sc = s; *sc != '\0'; ++sc);
for (sc = s; *sc != '\0'; ++sc) /* nothing */
;
return sc - s; return sc - s;
#endif /* RT_KLIBC_USING_STDLIB */
} }
RTM_EXPORT(rt_strlen); RTM_EXPORT(rt_strlen);
#endif /* RT_KSERVICE_USING_STDLIB */
/** /**
* @brief The strnlen() function returns the number of characters in the * @brief The strnlen() function returns the number of characters in the
* string pointed to by s, excluding the terminating null byte ('\0'), * string pointed to by s, excluding the terminating null byte ('\0'),
@ -473,10 +504,7 @@ RTM_EXPORT(rt_strlen);
rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen) rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
{ {
const char *sc; const char *sc;
for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc);
for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
;
return sc - s; return sc - s;
} }
RTM_EXPORT(rt_strnlen); RTM_EXPORT(rt_strnlen);