From 4c980c1a943c2fc4f54278a481f0a1a6bfbe3f42 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Thu, 21 Jul 2022 22:22:35 +0200 Subject: [PATCH] refactor(mem): move builtin memcpy, memset, strcpy, strlen into a separate file --- .../{lv_mem_builtin.c => lv_malloc_builtin.c} | 4 +- .../{lv_mem_builtin.h => lv_malloc_builtin.h} | 26 +-- src/misc/lv_mem.c | 6 +- src/misc/lv_memcpy_builtin.c | 175 ++++++++++++++++++ src/misc/lv_memcpy_builtin.h | 59 ++++++ 5 files changed, 244 insertions(+), 26 deletions(-) rename src/misc/{lv_mem_builtin.c => lv_malloc_builtin.c} (99%) rename src/misc/{lv_mem_builtin.h => lv_malloc_builtin.h} (60%) create mode 100644 src/misc/lv_memcpy_builtin.c create mode 100644 src/misc/lv_memcpy_builtin.h diff --git a/src/misc/lv_mem_builtin.c b/src/misc/lv_malloc_builtin.c similarity index 99% rename from src/misc/lv_mem_builtin.c rename to src/misc/lv_malloc_builtin.c index fd7ee35cc..40551e5d1 100644 --- a/src/misc/lv_mem_builtin.c +++ b/src/misc/lv_malloc_builtin.c @@ -1,5 +1,5 @@ /** - * @file lv_mem_builtin.c + * @file lv_malloc_builtin.c */ /********************* @@ -7,7 +7,7 @@ *********************/ #include "lv_mem.h" #if LV_USE_BUILTIN_MALLOC -#include "lv_mem_builtin.h" +#include "lv_malloc_builtin.h" #include "lv_tlsf.h" #include "lv_assert.h" #include "lv_log.h" diff --git a/src/misc/lv_mem_builtin.h b/src/misc/lv_malloc_builtin.h similarity index 60% rename from src/misc/lv_mem_builtin.h rename to src/misc/lv_malloc_builtin.h index e413da96c..998fd31ec 100644 --- a/src/misc/lv_mem_builtin.h +++ b/src/misc/lv_malloc_builtin.h @@ -1,10 +1,10 @@ /** - * @file lv_mem_builtin.h + * @file lv_malloc_builtin.h * */ -#ifndef LV_MEM_BUILTIN_H -#define LV_MEM_BUILTIN_H +#ifndef LV_MALLOC_BUILTIN_H +#define LV_MALLOC_BUILTIN_H #ifdef __cplusplus extern "C" { @@ -42,26 +42,6 @@ void * lv_malloc_builtin(size_t size); void * lv_realloc_builtin(void * p, size_t new_size); void lv_free_builtin(void * p); -/** - * Same as `memcpy` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param src pointer to the source buffer - * @param len number of byte to copy - */ -LV_ATTRIBUTE_FAST_MEM void * lv_memcpy_builtin(void * dst, const void * src, size_t len); - -/** - * Same as `memset` but optimized for 4 byte operation. - * @param dst pointer to the destination buffer - * @param v value to set [0..255] - * @param len number of byte to set - */ -LV_ATTRIBUTE_FAST_MEM void lv_memset_builtin(void * dst, uint8_t v, size_t len); - -size_t lv_strlen_builtin(const char * str); - -size_t lv_strncpy_builtin(char * dst, size_t dest_size, const char * src); - /** * * @return diff --git a/src/misc/lv_mem.c b/src/misc/lv_mem.c index b4a7f6860..09b8eacd0 100644 --- a/src/misc/lv_mem.c +++ b/src/misc/lv_mem.c @@ -13,7 +13,11 @@ #include "lv_log.h" #include LV_STDLIB_INCLUDE #if LV_USE_BUILTIN_MALLOC - #include "lv_mem_builtin.h" + #include "lv_malloc_builtin.h" +#endif + +#if LV_USE_BUILTIN_MEMCPY + #include "lv_memcpy_builtin.h" #endif /********************* diff --git a/src/misc/lv_memcpy_builtin.c b/src/misc/lv_memcpy_builtin.c new file mode 100644 index 000000000..7ed0c2654 --- /dev/null +++ b/src/misc/lv_memcpy_builtin.c @@ -0,0 +1,175 @@ +/** + * @file lv_mem_builtin.c + */ + +/********************* + * INCLUDES + *********************/ +#include "lv_mem.h" +#if LV_USE_BUILTIN_MEMCPY +#include "lv_memcpy_builtin.h" +#include "lv_assert.h" +#include "lv_log.h" +#include "lv_math.h" + +/********************* + * DEFINES + *********************/ +#ifdef LV_ARCH_64 + #define MEM_UNIT uint64_t + #define ALIGN_MASK 0x7 +#else + #define MEM_UNIT uint32_t + #define ALIGN_MASK 0x3 +#endif + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * STATIC VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ +#if LV_LOG_TRACE_MEM + #define MEM_TRACE(...) LV_LOG_TRACE(__VA_ARGS__) +#else + #define MEM_TRACE(...) +#endif + +#define _COPY(d, s) *d = *s; d++; s++; +#define _SET(d, v) *d = v; d++; +#define _REPEAT8(expr) expr expr expr expr expr expr expr expr + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +LV_ATTRIBUTE_FAST_MEM void * lv_memcpy_builtin(void * dst, const void * src, size_t len) +{ + uint8_t * d8 = dst; + const uint8_t * s8 = src; + + /*Simplify for small memories*/ + if(len < 16) { + while(len) { + *d8 = *s8; + d8++; + s8++; + len--; + } + return dst; + } + + lv_uintptr_t d_align = (lv_uintptr_t)d8 & ALIGN_MASK; + lv_uintptr_t s_align = (lv_uintptr_t)s8 & ALIGN_MASK; + + /*Byte copy for unaligned memories*/ + if(s_align != d_align) { + while(len > 32) { + _REPEAT8(_COPY(d8, s8)); + _REPEAT8(_COPY(d8, s8)); + _REPEAT8(_COPY(d8, s8)); + _REPEAT8(_COPY(d8, s8)); + len -= 32; + } + while(len) { + _COPY(d8, s8) + len--; + } + return dst; + } + + /*Make the memories aligned*/ + if(d_align) { + d_align = ALIGN_MASK + 1 - d_align; + while(d_align && len) { + _COPY(d8, s8); + d_align--; + len--; + } + } + + uint32_t * d32 = (uint32_t *)d8; + const uint32_t * s32 = (uint32_t *)s8; + while(len > 32) { + _REPEAT8(_COPY(d32, s32)) + len -= 32; + } + + d8 = (uint8_t *)d32; + s8 = (const uint8_t *)s32; + while(len) { + _COPY(d8, s8) + len--; + } + + return dst; +} + +/** + * Same as `memset` but optimized for 4 byte operation. + * @param dst pointer to the destination buffer + * @param v value to set [0..255] + * @param len number of byte to set + */ +LV_ATTRIBUTE_FAST_MEM void lv_memset_builtin(void * dst, uint8_t v, size_t len) +{ + uint8_t * d8 = (uint8_t *)dst; + uintptr_t d_align = (lv_uintptr_t) d8 & ALIGN_MASK; + + /*Make the address aligned*/ + if(d_align) { + d_align = ALIGN_MASK + 1 - d_align; + while(d_align && len) { + _SET(d8, v); + len--; + d_align--; + } + } + + uint32_t v32 = (uint32_t)v + ((uint32_t)v << 8) + ((uint32_t)v << 16) + ((uint32_t)v << 24); + uint32_t * d32 = (uint32_t *)d8; + + while(len > 32) { + _REPEAT8(_SET(d32, v32)); + len -= 32; + } + + d8 = (uint8_t *)d32; + while(len) { + _SET(d8, v); + len--; + } +} + +size_t lv_strlen_builtin(const char * str) +{ + size_t i = 0; + while(str[i]) i++; + + return i + 1; +} + +size_t lv_strncpy_builtin(char * dst, size_t dest_size, const char * src) +{ + size_t i; + for(i = 0; i < dest_size - 1 && *src; i++) { + dst[i] = src[i]; + } + dst[i] = '\0'; + return i; +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif /*LV_USE_BUILTIN_MEMCPY*/ diff --git a/src/misc/lv_memcpy_builtin.h b/src/misc/lv_memcpy_builtin.h new file mode 100644 index 000000000..2fac08276 --- /dev/null +++ b/src/misc/lv_memcpy_builtin.h @@ -0,0 +1,59 @@ +/** + * @file lv_memcpy_builtin.h + * + */ + +#ifndef LV_MEMCPY_BUILTIN_H +#define LV_MEMCPY_BUILTIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../lv_conf_internal.h" +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Same as `memcpy` but optimized for 4 byte operation. + * @param dst pointer to the destination buffer + * @param src pointer to the source buffer + * @param len number of byte to copy + */ +LV_ATTRIBUTE_FAST_MEM void * lv_memcpy_builtin(void * dst, const void * src, size_t len); + +/** + * Same as `memset` but optimized for 4 byte operation. + * @param dst pointer to the destination buffer + * @param v value to set [0..255] + * @param len number of byte to set + */ +LV_ATTRIBUTE_FAST_MEM void lv_memset_builtin(void * dst, uint8_t v, size_t len); + +size_t lv_strlen_builtin(const char * str); + +size_t lv_strncpy_builtin(char * dst, size_t dest_size, const char * src); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_MEMCPY_BUILTIN_H*/