From cb7c5f9921c1228d4823dae492a37c18254c38ce Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 22 Feb 2017 10:20:58 -0600 Subject: [PATCH] Implement strings.h macros as inline functions when possible for better C++ compatibility. --- include/cxx/cstring | 18 +++----- include/strings.h | 68 ++++++++++++++++++++++++++----- sched/pthread/pthread_mutexlock.c | 2 +- 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/include/cxx/cstring b/include/cxx/cstring index e2b7367ab7..038080e75d 100644 --- a/include/cxx/cstring +++ b/include/cxx/cstring @@ -44,19 +44,6 @@ #include #include -//*************************************************************************** -// Pre-processor Definitions -//*************************************************************************** - -// Remove macros defined in strings.h. The index() definition, in -// particular, can cause naming collision problems. - -#undef bcmp -#undef bcopy -#undef bzero -#undef index -#undef rindex - //*************************************************************************** // Namespace //*************************************************************************** @@ -100,6 +87,11 @@ namespace std // Declared in legacy strings.h + using ::bcmp; + using ::bcopy; + using ::bzero; + using ::index; + using ::rindex; using ::ffs; using ::strcasecmp; using ::strncasecmp; diff --git a/include/strings.h b/include/strings.h index e9812335f7..6fdaec2275 100644 --- a/include/strings.h +++ b/include/strings.h @@ -47,24 +47,28 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + +#if !defined(CONFIG_HAVE_INLINE) && !defined(__cplusplus) /* Compatibility definitions * * Marked LEGACY in Open Group Base Specifications Issue 6/IEEE Std 1003.1-2004 * Removed from Open Group Base Specifications Issue 7/IEEE Std 1003.1-2008 */ -#define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len) -#define bcopy(b1,b2,len) (void)memmove(b2,b1,len) +# define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len) +# define bcopy(b1,b2,len) (void)memmove(b2,b1,len) -#ifndef CONFIG_LIBC_ARCH_BZERO -# define bzero(s,n) (void)memset(s,0,n) -#endif +# ifndef CONFIG_LIBC_ARCH_BZERO +# define bzero(s,n) (void)memset(s,0,n) +# endif -#define index(s,c) strchr(s,c) -#define rindex(s,c) strrchr(s,c) +# define index(s,c) strchr(s,c) +# define rindex(s,c) strrchr(s,c) + +#endif /* !CONFIG_HAVE_INLINE && !__cplusplus */ /**************************************************************************** - * Public Function Prototypes + * Inline Functions ****************************************************************************/ #undef EXTERN @@ -76,9 +80,51 @@ extern "C" #define EXTERN extern #endif -int ffs(int j); -int strcasecmp(FAR const char *, FAR const char *); -int strncasecmp(FAR const char *, FAR const char *, size_t); +#if defined(CONFIG_HAVE_INLINE) || defined(__cplusplus) +/* Compatibility inline functions. + * + * Marked LEGACY in Open Group Base Specifications Issue 6/IEEE Std 1003.1-2004 + * Removed from Open Group Base Specifications Issue 7/IEEE Std 1003.1-2008 + */ + +static inline int bcmp(FAR const void *b1, FAR const void *b2, size_t len) +{ + return memcmp(b1, b2, len); +} + +static inline void bcopy(FAR const void *b1, FAR void *b2, size_t len) +{ + (void)memmove(b1, b2, len); +} + +#ifndef CONFIG_LIBC_ARCH_BZERO +static inline void bzero(FAR void *s, size_t len) +{ + (void)memset(s, 0, len); +} +#endif + +static inline FAR char *index(FAR const char *s, int c) +{ + return strchr(s, c); +} + +static inline FAR char *rindex(FAR const char *s, int c) +{ + return strrchr(s, c); +} +#endif /* CONFIG_HAVE_INLINE || __cplusplus */ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_LIBC_ARCH_BZERO +void bzero(FAR void *s, size_t len); +#endif +int ffs(int j); +int strcasecmp(FAR const char *, FAR const char *); +int strncasecmp(FAR const char *, FAR const char *, size_t); #undef EXTERN #if defined(__cplusplus) diff --git a/sched/pthread/pthread_mutexlock.c b/sched/pthread/pthread_mutexlock.c index ebd650df6f..f94890505d 100644 --- a/sched/pthread/pthread_mutexlock.c +++ b/sched/pthread/pthread_mutexlock.c @@ -150,7 +150,7 @@ int pthread_mutex_lock(FAR pthread_mutex_t *mutex) ret = pthread_takesemaphore((FAR sem_t *)&mutex->sem); - /* If we succussfully obtained the semaphore, then indicate + /* If we successfully obtained the semaphore, then indicate * that we own it. */