[kernel] 修复 rt_memset 使其 word size independent.

For compatibility with 16bit addressing 32bit CPU (e.g. TI C28x).
This commit is contained in:
Yunjie Gu 2022-08-24 22:12:11 +01:00 committed by GitHub
parent a38b39ac25
commit d49ac097ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 13 deletions

View File

@ -21,6 +21,7 @@
* 2021-12-20 Meco Man implement rt_strcpy()
* 2022-01-07 Gabriel add __on_rt_assert_hook
* 2022-06-04 Meco Man remove strnlen
* 2022-08-24 Yunjie make rt_memset word-independent to adapt to ti c28x (16bit word)
*/
#include <rtthread.h>
@ -182,7 +183,7 @@ RT_WEAK void *rt_memset(void *s, int c, rt_ubase_t count)
return s;
#else
#define LBLOCKSIZE (sizeof(long))
#define LBLOCKSIZE (sizeof(rt_ubase_t))
#define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
@ -190,8 +191,10 @@ RT_WEAK void *rt_memset(void *s, int c, rt_ubase_t count)
char *m = (char *)s;
unsigned long buffer;
unsigned long *aligned_addr;
unsigned int d = c & 0xff; /* To avoid sign extension, copy C to an
unsigned variable. */
unsigned char d = (unsigned int)c & (unsigned char)(-1); /* To avoid sign extension, copy C to an
unsigned variable. (unsigned)((char)(-1))=0xFF for 8bit and =0xFFFF for 16bit: word independent */
RT_ASSERT(LBLOCKSIZE == 2 || LBLOCKSIZE == 4 || LBLOCKSIZE == 8);
if (!TOO_SMALL(count) && !UNALIGNED(s))
{
@ -201,16 +204,9 @@ RT_WEAK void *rt_memset(void *s, int c, rt_ubase_t count)
/* Store d into each char sized location in buffer so that
* we can set large blocks quickly.
*/
if (LBLOCKSIZE == 4)
for (i = 0; i < LBLOCKSIZE; i++)
{
buffer = (d << 8) | d;
buffer |= (buffer << 16);
}
else
{
buffer = 0;
for (i = 0; i < LBLOCKSIZE; i ++)
buffer = (buffer << 8) | d;
*(((unsigned char *)&buffer)+i) = d;
}
while (count >= LBLOCKSIZE * 4)
@ -643,7 +639,7 @@ void rt_show_version(void)
rt_kprintf("\n \\ | /\n");
rt_kprintf("- RT - Thread Operating System\n");
rt_kprintf(" / | \\ %d.%d.%d build %s %s\n",
RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__, __TIME__);
(rt_int32_t)RT_VERSION, (rt_int32_t)RT_SUBVERSION, (rt_int32_t)RT_REVISION, __DATE__, __TIME__);
rt_kprintf(" 2006 - 2022 Copyright by RT-Thread team\n");
}
RTM_EXPORT(rt_show_version);