Skip to content

Commit

Permalink
[klibc] organize and renmae macros, and add CI check
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterywolf committed Apr 21, 2024
1 parent 8149eb2 commit d68e47e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# scons: --strict

CONFIG_RT_KLIBC_USING_PRINTF_LONGLONG=y
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# scons: --strict

CONFIG_RT_KLIBC_USING_STDLIB=y
CONFIG_RT_KLIBC_USING_STDLIB_MEMORY=y
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# scons: --strict

CONFIG_RT_KLIBC_USING_TINY_SIZE=y
20 changes: 0 additions & 20 deletions include/rtklibc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extern "C" {
#endif

/* kstdio */

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_sprintf(char *buf, const char *format, ...);
Expand All @@ -37,38 +36,19 @@ const char *rt_strerror(rt_err_t error);

/* kstring */

#ifndef RT_KSERVICE_USING_STDLIB_MEMORY
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_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);
#endif /* RT_KSERVICE_USING_STDLIB_MEMORY */
char *rt_strdup(const char *s);
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);
rt_int32_t rt_strcasecmp(const char *a, const char *b);
char *rt_strcpy(char *dst, const char *src);
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_strcmp(const char *cs, const char *ct);
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
}
Expand Down
26 changes: 14 additions & 12 deletions src/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,30 @@ if RT_USING_TIMER_SOFT
endif

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
bool "Enable kservice to use standard C library"
default y
menu "klibc optimization"

if RT_KSERVICE_USING_STDLIB
config RT_KSERVICE_USING_STDLIB_MEMORY
config RT_KLIBC_USING_STDLIB
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)"
default n
help
e.g. use memcpy to replace rt_memcpy
endif

config RT_KSERVICE_USING_TINY_SIZE
bool "Enable kservice to use tiny size"
default n

config RT_USING_TINY_FFS
bool "Enable kservice to use tiny finding first bit set method"
config RT_KLIBC_USING_TINY_SIZE
bool "Enable tiny size of klibc"
default n

config RT_KPRINTF_USING_LONGLONG
config RT_KLIBC_USING_PRINTF_LONGLONG
bool "Enable rt_printf-family functions to support long-long format"
default y if ARCH_CPU_64BIT
default n
Expand Down
26 changes: 13 additions & 13 deletions src/klibc/kstdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
*
* @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)
#else
rt_inline int divide(unsigned long *n, int base)
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
{
int res;

/* optimized for processor which does not support divide instructions. */
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
res = (int)((*n) % base);
*n = (long long)((*n) / base);
#else
Expand Down Expand Up @@ -71,23 +71,23 @@ rt_inline int skip_atoi(const char **s)

static char *print_number(char *buf,
char *end,
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
unsigned long long num,
#else
unsigned long num,
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int base,
int qualifier,
int s,
int precision,
int type)
{
char c = 0, sign = 0;
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
char tmp[64] = {0};
#else
char tmp[32] = {0};
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int precision_bak = precision;
const char *digits = RT_NULL;
static const char small_digits[] = "0123456789abcdef";
Expand Down Expand Up @@ -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)
{
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
unsigned long long num = 0;
#else
unsigned long num = 0;
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
int i = 0, len = 0;
char *str = RT_NULL, *end = RT_NULL, c = 0;
const char *s = RT_NULL;
Expand Down Expand Up @@ -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 */

if (*fmt == 'h' || *fmt == 'l' ||
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
*fmt == 'L' ||
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
*fmt == 'z')
{
qualifier = *fmt;
++fmt;
#ifdef RT_KPRINTF_USING_LONGLONG
#ifdef RT_KLIBC_USING_PRINTF_LONGLONG
if (qualifier == 'l' && *fmt == 'l')
{
qualifier = 'L';
++fmt;
}
#endif /* RT_KPRINTF_USING_LONGLONG */
#endif /* RT_KLIBC_USING_PRINTF_LONGLONG */
if (qualifier == 'h' && *fmt == 'h')
{
qualifier = 'H';
Expand Down
Loading

0 comments on commit d68e47e

Please sign in to comment.