diff options
author | Tom Hughes <tomhughes@chromium.org> | 2022-06-17 15:14:11 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-07-07 18:43:42 +0000 |
commit | ff355e3dc31830f70b5ad603c5fce17e1ca012ce (patch) | |
tree | e6989a180a9eca76e315b146ea752fb78cd8c842 | |
parent | 54e603413f987e995486ede3ddccce1aebbe2c93 (diff) | |
download | chrome-ec-ff355e3dc31830f70b5ad603c5fce17e1ca012ce.tar.gz |
util: Move stdlib declarations to builtin directory
The "builtin" directory is EC's copy of the C standard library headers.
Move the declarations for functions that are provided by the standard
library to the "builtin" directory. This change makes it easier for
future changes to optionally build with the C standard library instead
of the standalone EC subset.
BRANCH=none
BUG=b:172020503, b:234181908
TEST=./util/compare_build.sh -b all -j 120
=> MATCH
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: I4822e49677e0c0e1b092711b533f2aaa762cb4e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3712033
Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r-- | builtin/ctype.h | 16 | ||||
-rw-r--r-- | builtin/stdlib.c | 10 | ||||
-rw-r--r-- | builtin/stdlib.h | 11 | ||||
-rw-r--r-- | builtin/string.h | 24 | ||||
-rw-r--r-- | builtin/strings.h | 14 | ||||
-rw-r--r-- | include/util.h | 55 |
6 files changed, 78 insertions, 52 deletions
diff --git a/builtin/ctype.h b/builtin/ctype.h new file mode 100644 index 0000000000..682cc05694 --- /dev/null +++ b/builtin/ctype.h @@ -0,0 +1,16 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_CTYPE_H__ +#define __CROS_EC_CTYPE_H__ + +int isdigit(int c); +int isspace(int c); +int isalpha(int c); +int isupper(int c); +int isprint(int c); +int tolower(int c); + +#endif /* __CROS_EC_CTYPE_H__ */ diff --git a/builtin/stdlib.c b/builtin/stdlib.c index e42788c4af..1d07b64869 100644 --- a/builtin/stdlib.c +++ b/builtin/stdlib.c @@ -8,6 +8,16 @@ #include "common.h" #include "console.h" #include "util.h" +/* + * The following macros are defined in stdlib.h in the C standard library, which + * conflict with the definitions in this file. + */ +#undef isspace +#undef isdigit +#undef isalpha +#undef isupper +#undef isprint +#undef tolower /* * TODO(b/237712836): Zephyr's libc should provide strcasecmp. For now we'll diff --git a/builtin/stdlib.h b/builtin/stdlib.h new file mode 100644 index 0000000000..fa62a18589 --- /dev/null +++ b/builtin/stdlib.h @@ -0,0 +1,11 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_STDLIB_H__ +#define __CROS_EC_STDLIB_H__ + +int atoi(const char *nptr); + +#endif /* __CROS_EC_STDLIB_H__ */ diff --git a/builtin/string.h b/builtin/string.h index 8c9a71bd75..1e4c45b082 100644 --- a/builtin/string.h +++ b/builtin/string.h @@ -3,10 +3,8 @@ * found in the LICENSE file. */ -/* This header is only needed for CR50 compatibility */ - -#ifndef __CROS_EC_STRINGS_H__ -#define __CROS_EC_STRINGS_H__ +#ifndef __CROS_EC_STRING_H__ +#define __CROS_EC_STRING_H__ #include <stddef.h> @@ -20,12 +18,28 @@ void *memmove(void *dest, const void *src, size_t n); void *memset(void *dest, int c, size_t len); void *memchr(const void *buffer, int c, size_t n); +size_t strlen(const char *s); size_t strnlen(const char *s, size_t maxlen); char *strncpy(char *dest, const char *src, size_t n); int strncmp(const char *s1, const char *s2, size_t n); +/** + * Calculates the length of the initial segment of s which consists + * entirely of bytes not in reject. + */ +size_t strcspn(const char *s, const char *reject); + +/** + * Find the first occurrence of the substring <s2> in the string <s1> + * + * @param s1 String where <s2> is searched. + * @param s2 Substring to be located in <s1> + * @return Pointer to the located substring or NULL if not found. + */ +char *strstr(const char *s1, const char *s2); + #ifdef __cplusplus } #endif -#endif /* __CROS_EC_STRINGS_H__ */ +#endif /* __CROS_EC_STRING_H__ */ diff --git a/builtin/strings.h b/builtin/strings.h new file mode 100644 index 0000000000..767706d579 --- /dev/null +++ b/builtin/strings.h @@ -0,0 +1,14 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_STRINGS_H__ +#define __CROS_EC_STRINGS_H__ + +#include <stddef.h> + +int strcasecmp(const char *s1, const char *s2); +int strncasecmp(const char *s1, const char *s2, size_t size); + +#endif /* __CROS_EC_STRINGS_H__ */ diff --git a/include/util.h b/include/util.h index 8339838abd..28fdb7b9fe 100644 --- a/include/util.h +++ b/include/util.h @@ -13,10 +13,18 @@ #include "panic.h" #include "builtin/assert.h" /* For ASSERT(). */ +#include <ctype.h> #include <stdbool.h> #include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> #ifdef CONFIG_ZEPHYR #include <zephyr/sys/util.h> +/** + * TODO(b/237712836): Remove once Zephyr's libc has strcasecmp. + */ +#include "builtin/strings.h" #endif #ifdef __cplusplus @@ -101,52 +109,6 @@ extern "C" { b = __t__; \ } while (0) -#ifndef HIDE_EC_STDLIB - -/* Standard library functions */ -int atoi(const char *nptr); - -#ifdef CONFIG_ZEPHYR -#include <ctype.h> -#include <string.h> -#else -int isdigit(int c); -int isspace(int c); -int isalpha(int c); -int isupper(int c); -int isprint(int c); -int tolower(int c); - -int memcmp(const void *s1, const void *s2, size_t len); -void *memcpy(void *dest, const void *src, size_t len); -void *memset(void *dest, int c, size_t len); -void *memmove(void *dest, const void *src, size_t len); -void *memchr(const void *buffer, int c, size_t n); - -/** - * Find the first occurrence of the substring <s2> in the string <s1> - * - * @param s1 String where <s2> is searched. - * @param s2 Substring to be located in <s1> - * @return Pointer to the located substring or NULL if not found. - */ -char *strstr(const char *s1, const char *s2); - -/** - * Calculates the length of the initial segment of s which consists - * entirely of bytes not in reject. - */ -size_t strcspn(const char *s, const char *reject); - -size_t strlen(const char *s); -char *strncpy(char *dest, const char *src, size_t n); -int strncmp(const char *s1, const char *s2, size_t n); -#endif - -int strcasecmp(const char *s1, const char *s2); -int strncasecmp(const char *s1, const char *s2, size_t size); -size_t strnlen(const char *s, size_t maxlen); - /* Like strtol(), but for integers. */ int strtoi(const char *nptr, char **endptr, int base); @@ -173,7 +135,6 @@ char *strzcpy(char *dest, const char *src, int len); * Other strings return 0 and leave *dest unchanged. */ int parse_bool(const char *s, int *dest); -#endif /* !HIDE_EC_STDLIB */ /** * Constant time implementation of memcmp to avoid timing side channels. |