[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [[UNIKRAFT] 1/4] lib/isrlib: Introduce libc-like interrupt-context-safe routines
libisr library means to introduce interrupt-context-safe routines, to be used in interrupt handlers and drivers. It is derived from nolibc. Signed-off-by: cristian-vijelie <cristianvijelie@xxxxxxxxx> --- lib/isrlib/Config.uk | 3 + lib/isrlib/Makefile.uk | 10 + lib/isrlib/include/uk/isr/stdlib.h | 60 +++++ lib/isrlib/include/uk/isr/string.h | 72 +++++ lib/isrlib/stdlib.c | 417 +++++++++++++++++++++++++++++ lib/isrlib/string.c | 325 ++++++++++++++++++++++ 6 files changed, 887 insertions(+) create mode 100644 lib/isrlib/Config.uk create mode 100644 lib/isrlib/Makefile.uk create mode 100644 lib/isrlib/include/uk/isr/stdlib.h create mode 100644 lib/isrlib/include/uk/isr/string.h create mode 100644 lib/isrlib/stdlib.c create mode 100644 lib/isrlib/string.c diff --git a/lib/isrlib/Config.uk b/lib/isrlib/Config.uk new file mode 100644 index 0000000..128b9b4 --- /dev/null +++ b/lib/isrlib/Config.uk @@ -0,0 +1,3 @@ +config LIBISRLIB + bool "isrlib: Interrupt-safe standard routines" + default n diff --git a/lib/isrlib/Makefile.uk b/lib/isrlib/Makefile.uk new file mode 100644 index 0000000..9b60e04 --- /dev/null +++ b/lib/isrlib/Makefile.uk @@ -0,0 +1,10 @@ +$(eval $(call addlib_s,libisrlib,$(CONFIG_LIBISRLIB))) + +LIBISRLIB_GLOBAL_INCLUDES-y = $(LIBISRLIB_BASE)/include + +CINCLUDES-$(CONFIG_LIBISRLIB) += $(LIBISRLIB_GLOBAL_INCLUDES) +CXXINCLUDES-$(CONFIG_LIBISRLIB) += $(LIBISRLIB_GLOBAL_INCLUDES) + +LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/string.c|isr +LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/stdlib.c|isr +LIBISRLIB_SRCS-y += $(LIBISRLIB_BASE)/qsort.c|isr diff --git a/lib/isrlib/include/uk/isr/stdlib.h b/lib/isrlib/include/uk/isr/stdlib.h new file mode 100644 index 0000000..3c439c2 --- /dev/null +++ b/lib/isrlib/include/uk/isr/stdlib.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2009 Citrix Systems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef __UK_ISR_STDLIB_H__ +#define __UK_ISR_STDLIB_H__ + +#include <stdlib.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Convert a string to an unsigned long integer. + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + * + * @nptr: The start of the string + * @endptr: A pointer to the end of the parsed string will be placed here + * @base: The number base to use + */ +long strtol_isr(const char *nptr, char **endptr, int base); +unsigned long strtoul_isr(const char *nptr, char **endptr, int base); +long long strtoll_isr(const char *nptr, char **endptr, int base); +unsigned long long strtoull_isr(const char *nptr, char **endptr, int base); + +/** + * Convert a string to an integer + * @s: The start of the string + */ +int atoi_isr(const char *s); + +#ifdef __cplusplus +} +#endif + +#endif /* __UK_ISR_STDLIB_H__ */ diff --git a/lib/isrlib/include/uk/isr/string.h b/lib/isrlib/include/uk/isr/string.h new file mode 100644 index 0000000..bcf9fef --- /dev/null +++ b/lib/isrlib/include/uk/isr/string.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Authors: Simon Kuenzer <simon.kuenzer@xxxxxxxxx> + * Cristian Vijelie <cristianvijelie@xxxxxxxxx> + * + * + * Copyright (c) 2017, NEC Europe Ltd., NEC Corporation. All rights reserved. + * Copyright (c) 2020, University Politehnica of Bucharest. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef __UK_ISR_STRING_H__ +#define __UK_ISR_STRING_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <string.h> + +void *memcpy_isr(void *dst, const void *src, size_t len); +void *memset_isr(void *ptr, int val, size_t len); +void *memchr_isr(const void *ptr, int val, size_t len); +void *memrchr_isr(const void *m, int c, size_t n); +int memcmp_isr(const void *ptr1, const void *ptr2, size_t len); +void *memmove_isr(void *dst, const void *src, size_t len); + +char *strncpy_isr(char *dst, const char *src, size_t len); +char *strcpy_isr(char *dst, const char *src); +size_t strlcpy_isr(char *d, const char *s, size_t n); +size_t strlcat_isr(char *d, const char *s, size_t n); +size_t strnlen_isr(const char *str, size_t maxlen); +size_t strlen_isr(const char *str); +char *strchrnul_isr(const char *s, int c); +char *strchr_isr(const char *str, int c); +char *strrchr_isr(const char *s, int c); +int strncmp_isr(const char *str1, const char *str2, size_t len); +int strcmp_isr(const char *str1, const char *str2); +size_t strcspn_isr(const char *s, const char *c); +size_t strspn_isr(const char *s, const char *c); +char *strtok_isr(char *restrict s, const char *restrict sep, char **restrict p); + +#ifdef __cplusplus +} +#endif + +#endif /* __UK_ISR_STRING_H__ */ diff --git a/lib/isrlib/stdlib.c b/lib/isrlib/stdlib.c new file mode 100644 index 0000000..d1aad75 --- /dev/null +++ b/lib/isrlib/stdlib.c @@ -0,0 +1,417 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + **************************************************************************** + * + * File: printf.c + * Author: Juergen Gross <jgross@xxxxxxxx> + * + * Date: Jun 2016 + * + * Environment: Xen Minimal OS + * Description: Library functions for printing + * (FreeBSD port) + * + **************************************************************************** + */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Copyright (c) 2011 The FreeBSD Foundation + * All rights reserved. + * Portions of this software were developed by David Chisnall + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> +#include <limits.h> +#include <ctype.h> +#include <uk/print.h> +#include <uk/plat/bootstrap.h> +#include <uk/arch/limits.h> + +#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) + +/* + * Convert a string to an unsigned long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +unsigned long strtoul_isr(const char *nptr, char **endptr, int base) +{ + const char *s = nptr; + unsigned long acc; + unsigned char c; + unsigned long cutoff; + int neg = 0, any, cutlim; + + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + + /* + * See strtol for comments as to the logic used. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else if (c == '+') + c = *s++; + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; + cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= base; + acc += c; + } + } + if (any < 0) { + acc = ULONG_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +long strtol_isr(const char *nptr, char **endptr, int base) +{ + const char *s; + unsigned long acc; + unsigned char c; + unsigned long qbase, cutoff; + int neg, any, cutlim; + + s = nptr; + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-2147483648..2147483647] and the input base + * is 10, cutoff will be set to 2147483647 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 2147483647, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned int)base; + cutoff = neg + ? (unsigned long)LONG_MAX + - (unsigned long)(LONG_MIN + LONG_MAX) + : LONG_MAX; + cutlim = cutoff % qbase; + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + acc = neg ? LONG_MIN : LONG_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +/* + * Convert a string to a long long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +long long strtoll_isr(const char *nptr, char **endptr, int base) +{ + const char *s; + unsigned long long acc; + unsigned char c; + unsigned long long qbase, cutoff; + int neg, any, cutlim; + + s = nptr; + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-9223372036854775808..9223372036854775807] and the input base + * is 10, cutoff will be set to 922337203685477580 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 922337203685477580, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned int)base; + cutoff = neg + ? (unsigned long long)LLONG_MAX + - (unsigned long long)(LLONG_MIN + LLONG_MAX) + : LLONG_MAX; + cutlim = cutoff % qbase; + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + errno = ERANGE; + acc = neg ? LLONG_MIN : LLONG_MAX; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +/* + * Convert a string to an unsigned long long integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +unsigned long long strtoull_isr(const char *nptr, char **endptr, int base) +{ + const char *s = nptr; + unsigned long long acc; + unsigned char c; + unsigned long long qbase, cutoff; + int neg, any, cutlim; + + if (base < 0 || base == 1 || base > 36) { + errno = -EINVAL; + any = 0; + acc = 0; + goto exit; + } + /* + * See strtoq for comments as to the logic used. + */ + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + qbase = (unsigned int)base; + cutoff = (unsigned long long)ULLONG_MAX / qbase; + cutlim = (unsigned long long)ULLONG_MAX % qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!isascii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + errno = ERANGE; + acc = ULLONG_MAX; + } else if (neg) + acc = -acc; + +exit: + if (endptr != 0) + *endptr = __DECONST(char *, any ? s - 1 : nptr); + return acc; +} + +int atoi_isr(const char *s) +{ + long long atoll; + + atoll = strtoll_isr(s, NULL, 10); + atoll = (atoll > __I_MAX) ? __I_MAX : atoll; + atoll = (atoll < __I_MIN) ? __I_MIN : atoll; + + return (int) atoll; +} diff --git a/lib/isrlib/string.c b/lib/isrlib/string.c new file mode 100644 index 0000000..d599294 --- /dev/null +++ b/lib/isrlib/string.c @@ -0,0 +1,325 @@ +/* SPDX-License-Identifier: BSD-3-Clause AND MIT */ +/* + * Authors: Simon Kuenzer <simon.kuenzer@xxxxxxxxx> + * Authors: Cristian Vijelie <cristianvijelie@xxxxxxxxx> + * + * Copyright (c) 2017, NEC Europe Ltd., NEC Corporation. All rights reserved. + * Copyright (c) 2020, University Politehnica of Bucharest. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ +/* For the parts taken from musl (marked as such below), the MIT licence + * applies instead: + * ---------------------------------------------------------------------- + * Copyright (c) 2005-2014 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * ---------------------------------------------------------------------- + */ + +#include <uk/isr/string.h> +#include <uk/arch/types.h> +#include <stdint.h> +#include <limits.h> + +void *memcpy_isr(void *dst, const void *src, size_t len) +{ + size_t p; + + for (p = 0; p < len; ++p) + *((__u8 *)(((__uptr)dst) + p)) = *((__u8 *)(((__uptr)src) + p)); + + return dst; +} + +void *memset_isr(void *ptr, int val, size_t len) +{ + __u8 *p = (__u8 *) ptr; + + for (; len > 0; --len) + *(p++) = (__u8)val; + + return ptr; +} + +void *memchr_isr(const void *ptr, int val, size_t len) +{ + uintptr_t o = 0; + + for (o = 0; o < (uintptr_t)len; ++o) + if (*((const uint8_t *)(((uintptr_t)ptr) + o)) == (uint8_t)val) + return (void *)((uintptr_t)ptr + o); + + return NULL; /* did not find val */ +} + +void *memrchr_isr(const void *m, int c, size_t n) +{ + const unsigned char *s = m; + + c = (unsigned char) c; + while (n--) + if (s[n] == c) + return (void *) (s + n); + return 0; +} + +void *memmove_isr(void *dst, const void *src, size_t len) +{ + uint8_t *d = dst; + const uint8_t *s = src; + + if (src > dst) { + for (; len > 0; --len) + *(d++) = *(s++); + } else { + s += len; + d += len; + + for (; len > 0; --len) + *(d--) = *(s--); + } + + return dst; +} + +int memcmp_isr(const void *ptr1, const void *ptr2, size_t len) +{ + const unsigned char *c1 = (const unsigned char *)ptr1; + const unsigned char *c2 = (const unsigned char *)ptr2; + + for (; len > 0; --len, ++c1, ++c2) { + if ((*c1) != (*c2)) + return ((*c1) - (*c2)); + } + + return 0; +} + +size_t strlen_isr(const char *str) +{ + return strnlen_isr(str, SIZE_MAX); +} + +size_t strnlen_isr(const char *str, size_t len) +{ + const char *p = memchr_isr(str, 0, len); + return p ? (size_t) (p - str) : len; +} + +char *strncpy_isr(char *dst, const char *src, size_t len) +{ + size_t clen; + + clen = strnlen_isr(src, len); + memcpy_isr(dst, src, clen); + + /* instead of filling up the rest of left space with zeros, + * append a termination character if we did not copy one + */ + if (clen < len && dst[clen - 1] != '\0') + dst[clen] = '\0'; + return dst; +} + +char *strcpy_isr(char *dst, const char *src) +{ + return strncpy_isr(dst, src, SIZE_MAX); +} + +int strncmp_isr(const char *str1, const char *str2, size_t len) +{ + const char *c1 = (const char *)str1; + const char *c2 = (const char *)str2; + + for (; len > 0; --len, ++c1, ++c2) { + if ((*c1) != (*c2)) + return (int)((*c1) - (*c2)); + if ((*c1) == '\0') + break; + } + return 0; +} + +int strcmp_isr(const char *str1, const char *str2) +{ + register signed char __res; + + while ((__res = *str1 - *str2++) == 0 && *str1++) + ; + + return __res; +} + +/* The following code is taken from musl libc */ +#define ALIGN (sizeof(size_t)) +#define ONES ((size_t) -1 / UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX / 2 + 1)) +#define HASZERO(x) (((x) - ONES) & ~(x) & HIGHS) +#define BITOP(a, b, op) \ + ((a)[(size_t)(b) / (8*sizeof *(a))] op \ + (size_t)1 << ((size_t)(b) % (8 * sizeof *(a)))) + +char *strchrnul_isr(const char *s, int c) +{ + size_t *w, k; + + c = (unsigned char)c; + if (!c) + return (char *)s + strlen_isr(s); + + for (; (uintptr_t)s % ALIGN; s++) + if (!*s || *(unsigned char *)s == c) + return (char *)s; + k = ONES * c; + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w ^ k); w++) + ; + for (s = (void *)w; *s && *(unsigned char *)s != c; s++) + ; + return (char *)s; +} + +char *strchr_isr(const char *str, int c) +{ + char *r = strchrnul_isr(str, c); + return *(unsigned char *)r == (unsigned char)c ? r : 0; +} + +char *strrchr_isr(const char *s, int c) +{ + return memrchr_isr(s, c, strlen_isr(s) + 1); +} + +size_t strcspn_isr(const char *s, const char *c) +{ + const char *a = s; + size_t byteset[32 / sizeof(size_t)]; + + if (!c[0] || !c[1]) + return strchrnul_isr(s, *c)-a; + + memset_isr(byteset, 0, sizeof(byteset)); + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) + ; + for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++) + ; + return s-a; +} + +size_t strspn_isr(const char *s, const char *c) +{ + const char *a = s; + size_t byteset[32 / sizeof(size_t)] = { 0 }; + + if (!c[0]) + return 0; + if (!c[1]) { + for (; *s == *c; s++) + ; + return s-a; + } + + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) + ; + for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++) + ; + return s-a; +} + +char *strtok_isr(char *restrict s, const char *restrict sep, char **restrict p) +{ + if (!s && !(s = *p)) + return NULL; + s += strspn_isr(s, sep); + if (!*s) + return *p = 0; + *p = s + strcspn_isr(s, sep); + if (**p) + *(*p)++ = 0; + else + *p = 0; + return s; +} + +/* strlcpy has different ALIGN */ +#undef ALIGN +#define ALIGN (sizeof(size_t)-1) +size_t strlcpy_isr(char *d, const char *s, size_t n) +{ + char *d0 = d; + size_t *wd; + const size_t *ws; + + if (!n--) + goto finish; + + if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { + for (; ((uintptr_t) s & ALIGN) && n && (*d = *s); + n--, s++, d++) + ; + + if (n && *s) { + wd = (void *)d; ws = (const void *)s; + for (; n >= sizeof(size_t) && !HASZERO(*ws); + n -= sizeof(size_t), ws++, wd++) + *wd = *ws; + + d = (void *)wd; s = (const void *)ws; + } + } + + for (; n && (*d = *s); n--, s++, d++) + ; + *d = 0; +finish: + return d-d0 + strlen_isr(s); +} + +size_t strlcat_isr(char *d, const char *s, size_t n) +{ + size_t l = strnlen_isr(d, n); + if (l == n) + return l + strlen_isr(s); + return l + strlcpy_isr(d+l, s, n-l); +} -- 2.25.1
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |