Skip to content

Commit

Permalink
libc string:Separate code.
Browse files Browse the repository at this point in the history
Separate the code that follows the BSD license into independent files.

Signed-off-by: yangguangcai <[email protected]>
  • Loading branch information
yangguangcai1 authored and xiaoxiang781216 committed Oct 23, 2024
1 parent cdccce4 commit f07aba5
Show file tree
Hide file tree
Showing 35 changed files with 1,841 additions and 1,134 deletions.
32 changes: 16 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -8667,22 +8667,22 @@ drivers/i3c/internals.h

SPDX-License-Identifier: Apache-2.0

libs/libc/string/lib_memccpy.c
libs/libc/string/lib_memchr.c
libs/libc/string/lib_memcmp.c
libs/libc/string/lib_memcpy.c
libs/libc/string/lib_memrchr.c
libs/libc/string/lib_stpcpy.c
libs/libc/string/lib_stpncpy.c
libs/libc/string/lib_strcat.c
libs/libc/string/lib_strchr.c
libs/libc/string/lib_strchrnul.c
libs/libc/string/lib_strcmp.c
libs/libc/string/lib_strcpy.c
libs/libc/string/lib_strlen.c
libs/libc/string/lib_strncmp.c
libs/libc/string/lib_strncpy.c
libs/libc/string/lib_strrchr.c
libs/libc/string/lib_bsdmemccpy.c
libs/libc/string/lib_bsdmemchr.c
libs/libc/string/lib_bsdmemcmp.c
libs/libc/string/lib_bsdmemcpy.c
libs/libc/string/lib_bsdmemrchr.c
libs/libc/string/lib_bsdstpcpy.c
libs/libc/string/lib_bsdstpncpy.c
libs/libc/string/lib_bsdstrcat.c
libs/libc/string/lib_bsdstrchr.c
libs/libc/string/lib_bsdstrchrnul.c
libs/libc/string/lib_bsdstrcmp.c
libs/libc/string/lib_bsdstrcpy.c
libs/libc/string/lib_bsdstrlen.c
libs/libc/string/lib_bsdstrncmp.c
libs/libc/string/lib_bsdstrncpy.c
libs/libc/string/lib_bsdstrrchr.c
======================

Copyright (c) 1994-2009 Red Hat, Inc. All rights reserved.
Expand Down
57 changes: 42 additions & 15 deletions libs/libc/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,20 @@ set(SRCS
lib_flsl.c
lib_flsll.c
lib_isbasedigit.c
lib_memccpy.c
lib_memrchr.c
lib_memmem.c
lib_popcount.c
lib_popcountl.c
lib_popcountll.c
lib_skipspace.c
lib_stpcpy.c
lib_stpncpy.c
lib_strcasecmp.c
lib_strcat.c
lib_strcspn.c
lib_strchrnul.c
lib_strdup.c
lib_strerror.c
lib_strncasecmp.c
lib_strncat.c
lib_strncmp.c
lib_strndup.c
lib_strcasestr.c
lib_strpbrk.c
lib_strrchr.c
lib_strspn.c
lib_strstr.c
lib_strtok.c
Expand All @@ -68,21 +60,16 @@ set(SRCS
lib_strverscmp.c
lib_mempcpy.c
lib_rawmemchr.c
lib_memchr.c
lib_memcmp.c
lib_memmove.c
lib_memset.c
lib_strchr.c
lib_strcmp.c
lib_strcpy.c
lib_strlcat.c
lib_strlcpy.c
lib_strlen.c
lib_strncpy.c
lib_strnlen.c)

if(CONFIG_MEMCPY_VIK)
list(APPEND SRCS lib_vikmemcpy.c)
elseif(CONFIG_LIBC_STRING_OPTIMIZE)
list(APPEND SRCS lib_bsdmemcpy.c)
else()
list(APPEND SRCS lib_memcpy.c)
endif()
Expand All @@ -91,4 +78,44 @@ if(CONFIG_LIBC_LOCALE)
list(APPEND SRCS lib_strcoll.c lib_strxfrm.c)
endif()

if(CONFIG_LIBC_STRING_OPTIMIZE)
list(
APPEND
SRCS
lib_bsdmemccpy.c
lib_bsdmemcmp.c
lib_bsdmemrchr.c
lib_bsdstpncpy.c
lib_bsdstrchr.c
lib_bsdstrcmp.c
lib_bsdstrlen.c
lib_bsdstrncpy.c
lib_bsdmemchr.c
lib_bsdstpcpy.c
lib_bsdstrcat.c
lib_bsdstrchrnul.c
lib_bsdstrcpy.c
lib_bsdstrncmp.c
lib_bsdstrrchr.c)
else()
list(
APPEND
SRCS
lib_memccpy.c
lib_memcmp.c
lib_memrchr.c
lib_stpncpy.c
lib_strchr.c
lib_strcmp.c
lib_strlen.c
lib_strncpy.c
lib_memchr.c
lib_stpcpy.c
lib_strcat.c
lib_strchrnul.c
lib_strcpy.c
lib_strncmp.c
lib_strrchr.c)
endif()

target_sources(c PRIVATE ${SRCS})
30 changes: 22 additions & 8 deletions libs/libc/string/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@
# Add the string C files to the build

CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c
CSRCS += lib_flsll.c lib_isbasedigit.c lib_memccpy.c lib_memrchr.c lib_memmem.c
CSRCS += lib_flsll.c lib_isbasedigit.c lib_memmem.c
CSRCS += lib_popcount.c lib_popcountl.c lib_popcountll.c
CSRCS += lib_skipspace.c lib_stpcpy.c lib_stpncpy.c lib_strcasecmp.c
CSRCS += lib_strcat.c lib_strcspn.c lib_strchrnul.c lib_strdup.c
CSRCS += lib_strerror.c lib_strncasecmp.c lib_strncat.c lib_strncmp.c
CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c lib_strrchr.c
CSRCS += lib_skipspace.c lib_strcasecmp.c
CSRCS += lib_strcspn.c lib_strdup.c
CSRCS += lib_strerror.c lib_strncasecmp.c lib_strncat.c
CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c
CSRCS += lib_strspn.c lib_strstr.c lib_strtok.c lib_strtokr.c
CSRCS += lib_strsep.c lib_strerrorr.c lib_explicit_bzero.c lib_strsignal.c
CSRCS += lib_index.c lib_rindex.c lib_timingsafe_bcmp.c lib_strverscmp.c
CSRCS += lib_mempcpy.c lib_rawmemchr.c lib_bzero.c

CSRCS += lib_memchr.c lib_memcmp.c lib_memmove.c lib_memset.c
CSRCS += lib_strchr.c lib_strcmp.c lib_strcpy.c lib_strlcat.c
CSRCS += lib_strlcpy.c lib_strlen.c lib_strncpy.c lib_strnlen.c
CSRCS += lib_memmove.c lib_memset.c
CSRCS += lib_strlcat.c
CSRCS += lib_strlcpy.c lib_strnlen.c

ifeq ($(CONFIG_MEMCPY_VIK),y)
CSRCS += lib_vikmemcpy.c
else ifeq ($(CONFIG_LIBC_STRING_OPTIMIZE),y)
CSRCS += lib_bsdmemcpy.c
else
CSRCS += lib_memcpy.c
endif
Expand All @@ -48,6 +50,18 @@ ifeq ($(CONFIG_LIBC_LOCALE),y)
CSRCS += lib_strcoll.c lib_strxfrm.c
endif

ifeq ($(CONFIG_LIBC_STRING_OPTIMIZE),y)
CSRCS += lib_bsdmemccpy.c lib_bsdmemcmp.c lib_bsdmemrchr.c lib_bsdstpncpy.c
CSRCS += lib_bsdstrchr.c lib_bsdstrcmp.c lib_bsdstrlen.c lib_bsdstrncpy.c
CSRCS += lib_bsdmemchr.c lib_bsdstpcpy.c lib_bsdstrcat.c lib_bsdstrchrnul.c
CSRCS += lib_bsdstrcpy.c lib_bsdstrncmp.c lib_bsdstrrchr.c
else
CSRCS += lib_memccpy.c lib_memcmp.c lib_memrchr.c lib_stpncpy.c
CSRCS += lib_strchr.c lib_strcmp.c lib_strlen.c lib_strncpy.c
CSRCS += lib_memchr.c lib_stpcpy.c lib_strcat.c lib_strchrnul.c
CSRCS += lib_strcpy.c lib_strncmp.c lib_strrchr.c
endif

# Add the string directory to the build

DEPPATH += --dep-path string
Expand Down
140 changes: 140 additions & 0 deletions libs/libc/string/lib_bsdmemccpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/****************************************************************************
* libs/libc/string/lib_bsdmemccpy.c
*
* Copyright (c) 1994-2009 Red Hat, Inc. All rights reserved.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the BSD License. This program is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
* including the implied warranties of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. A copy of this license is available at
* http://www.opensource.org/licenses. Any Red Hat trademarks that are
* incorporated in the source code or documentation are not subject to
* the BSD License and may only be used or replicated with the express
* permission of Red Hat, Inc.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <sys/types.h>
#include <string.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Nonzero if either x or y is not aligned on a "long" boundary. */

#define UNALIGNED(x, y) \
(((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & (sizeof(long) - 1)))

/* How many bytes are copied each iteration of the word copy loop. */

#define LITTLEBLOCKSIZE (sizeof(long))

/* Threshhold for punting to the byte copier. */

#define TOO_SMALL(len) ((len) < LITTLEBLOCKSIZE)

/* Macros for detecting endchar */

#if LONG_MAX == 2147483647
# define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
#elif LONG_MAX == 9223372036854775807
/* Nonzero if x (a long int) contains a NULL byte. */

# define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 0x8080808080808080)
#endif

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: memccpy
*
* Description:
* The memccpy() function copies bytes from memory area s2 into s1,
* stopping after the first occurrence of byte c (converted to an unsigned
* char) is copied, or after n bytes are copied, whichever comes first. If
* copying takes place between objects that overlap, the behavior is
* undefined.
*
* Returned Value:
* The memccpy() function returns a pointer to the byte after the copy of c
* in s1, or a null pointer if c was not found in the first n bytes of s2.
*
****************************************************************************/

#undef memccpy /* See mm/README.txt */
FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, size_t n)
{
FAR void *ptr = NULL;
FAR unsigned char *pout = (FAR unsigned char *)s1;
FAR const unsigned char *pin = (FAR const unsigned char *)s2;
FAR long *paligned_out;
FAR const long *paligned_in;
unsigned char endchar = c & 0xff;

/* If the size is small, or either pin or pout is unaligned,
* then punt into the byte copy loop. This should be rare.
*/

if (!TOO_SMALL(n) && !UNALIGNED(pin, pout))
{
unsigned int i;
unsigned long mask = 0;

paligned_out = (FAR long *)pout;
paligned_in = (FAR long *)pin;

/* The fast code reads the ASCII one word at a time and only
* performs the bytewise search on word-sized segments if they
* contain the search character, which is detected by XORing
* the word-sized segment with a word-sized block of the search
* character and then detecting for the presence of NULL in the
* result.
*/

for (i = 0; i < LITTLEBLOCKSIZE; i++)
{
mask = (mask << 8) + endchar;
}

/* Copy one long word at a time if possible. */

while (n >= LITTLEBLOCKSIZE)
{
unsigned long buffer = (unsigned long)(*paligned_in);
buffer ^= mask;
if (DETECTNULL(buffer))
{
break; /* endchar is found, go byte by byte from here */
}

*paligned_out++ = *paligned_in++;
n -= LITTLEBLOCKSIZE;
}

/* Pick up any residual with a byte copier. */

pout = (FAR unsigned char *)paligned_out;
pin = (FAR unsigned char *)paligned_in;
}

while (n--)
{
if ((*pout++ = *pin++) == endchar)
{
ptr = pout;
break;
}
}

return ptr;
}
Loading

0 comments on commit f07aba5

Please sign in to comment.