From e97109184ab0a2de680a90a432d94f86e5be7b0f Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Thu, 25 Aug 2016 14:18:31 +0200 Subject: [PATCH] Drop global __ctype_ptr__ entirely in favor of using locale_t::ctype_ptr Keep __ctype_ptr__ available on Cygwin only, for backward compatibility with existing apps referencing it via the ctype macros. Otherwise initialize __global_locale.ctype_ptr and __C_locale.ctype_ptr and use them throughout. Signed-off-by: Corinna Vinschen --- newlib/libc/ctype/ctype_.c | 31 +++++++------------------------ newlib/libc/ctype/ctype_.h | 18 ++++++++++++++++++ newlib/libc/locale/locale.c | 21 ++++++++------------- winsup/cygwin/ctype.cc | 8 ++++---- 4 files changed, 37 insertions(+), 41 deletions(-) create mode 100644 newlib/libc/ctype/ctype_.h diff --git a/newlib/libc/ctype/ctype_.c b/newlib/libc/ctype/ctype_.c index 8945c5631..9e3cdcf05 100644 --- a/newlib/libc/ctype/ctype_.c +++ b/newlib/libc/ctype/ctype_.c @@ -35,7 +35,7 @@ static char sccsid[] = "@(#)ctype_.c 5.6 (Berkeley) 6/1/90"; #endif /* LIBC_SCCS and not lint */ -#include +#include "ctype_.h" #include "../locale/setlocale.h" #define _CTYPE_DATA_0_127 \ @@ -74,10 +74,6 @@ static char sccsid[] = "@(#)ctype_.c 5.6 (Berkeley) 6/1/90"; 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0 -#if (defined(__GNUC__) && !defined(__CHAR_UNSIGNED__) && !defined(COMPACT_CTYPE)) || defined (__CYGWIN__) -#define ALLOW_NEGATIVE_CTYPE_INDEX -#endif - #if defined(_MB_CAPABLE) #if defined(_MB_EXTENDED_CHARSETS_ISO) #include "ctype_iso.h" @@ -91,7 +87,7 @@ static char sccsid[] = "@(#)ctype_.c 5.6 (Berkeley) 6/1/90"; /* No static const on Cygwin since it's referenced and potentially overwritten for compatibility with older applications. */ #ifndef __CYGWIN__ -static _CONST +_CONST #endif char _ctype_b[128 + 256] = { _CTYPE_DATA_128_255, @@ -99,12 +95,10 @@ char _ctype_b[128 + 256] = { _CTYPE_DATA_128_255 }; -#ifndef _MB_CAPABLE -_CONST -#endif -char __EXPORT *__ctype_ptr__ = (char *) _ctype_b + 127; - # ifdef __CYGWIN__ +/* For backward compatibility */ +char __EXPORT *__ctype_ptr__ = DEFAULT_CTYPE_PTR; + # ifdef __x86_64__ __asm__ (" \n\ .data \n\ @@ -137,11 +131,6 @@ _CONST char _ctype_[1 + 256] = { _CTYPE_DATA_128_255 }; -#ifndef _MB_CAPABLE -_CONST -#endif -char *__ctype_ptr__ = (char *) _ctype_; - #endif /* !ALLOW_NEGATIVE_CTYPE_INDEX */ #if defined(_MB_CAPABLE) @@ -191,15 +180,9 @@ __set_ctype (struct __locale_t *loc, const char *charset) # endif } # if defined(ALLOW_NEGATIVE_CTYPE_INDEX) - if (loc) - loc->ctype_ptr = ctype_ptr + 127; - else - __ctype_ptr__ = ctype_ptr + 127; + loc->ctype_ptr = ctype_ptr + 127; # else - if (loc) - loc->ctype_ptr = ctype_ptr; - else - __ctype_ptr__ = ctype_ptr; + loc->ctype_ptr = ctype_ptr; # endif } #endif /* !__CYGWIN__ */ diff --git a/newlib/libc/ctype/ctype_.h b/newlib/libc/ctype/ctype_.h new file mode 100644 index 000000000..3b59a27d5 --- /dev/null +++ b/newlib/libc/ctype/ctype_.h @@ -0,0 +1,18 @@ +#include + +#if (defined(__GNUC__) && !defined(__CHAR_UNSIGNED__) && !defined(COMPACT_CTYPE)) || defined (__CYGWIN__) +#define ALLOW_NEGATIVE_CTYPE_INDEX +#endif + +#ifdef ALLOW_NEGATIVE_CTYPE_INDEX + + extern char _ctype_b[]; +# define DEFAULT_CTYPE_PTR ((char *) _ctype_b + 127) + +#else /* !ALLOW_NEGATIVE_CTYPE_INDEX */ + + extern char _ctype_[]; +# define DEFAULT_CTYPE_PTR ((char *) _ctype_) + +#endif /* !ALLOW_NEGATIVE_CTYPE_INDEX */ + diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c index fe04395b6..b7ced4d40 100644 --- a/newlib/libc/locale/locale.c +++ b/newlib/libc/locale/locale.c @@ -172,13 +172,13 @@ No supporting OS subroutines are required. #include #include -#include #include #include #include #include #include #include "setlocale.h" +#include "../ctype/ctype_.h" #include "../stdlib/local.h" #ifdef __CYGWIN__ /* Has to be kept available as exported symbol for @@ -225,7 +225,7 @@ const struct __locale_t __C_locale = __ascii_wctomb, __ascii_mbtowc, 0, - NULL, + DEFAULT_CTYPE_PTR, { ".", "", "", "", "", "", "", "", "", "", CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, @@ -266,7 +266,7 @@ struct __locale_t __global_locale = __ascii_mbtowc, #endif 0, - NULL, + DEFAULT_CTYPE_PTR, { ".", "", "", "", "", "", "", "", "", "", CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, @@ -901,7 +901,7 @@ restart: #endif loc->wctomb = l_wctomb; loc->mbtowc = l_mbtowc; - __set_ctype (loc == __get_global_locale () ? NULL : loc, charset); + __set_ctype (loc, charset); /* Determine the width for the "CJK Ambiguous Width" category of characters. This is used in wcwidth(). Assume single width for single-byte charsets, and double width for multi-byte charsets @@ -988,20 +988,15 @@ _DEFUN_VOID (__locale_mb_cur_max) } const char * -__locale_ctype_ptr (void) +__locale_ctype_ptr_l (struct __locale_t *locale) { - /* Only check if the current thread/reent has a locale. ctype_ptr is unused - in __global_locale, rather the global variable __ctype_ptr__ is used. */ - extern char *__ctype_ptr__; - return __get_locale_r (_REENT) ? __get_locale_r (_REENT)->ctype_ptr - : __ctype_ptr__; + return locale->ctype_ptr; } const char * -__locale_ctype_ptr_l (struct __locale_t *locale) +__locale_ctype_ptr (void) { - extern char *__ctype_ptr__; - return locale->ctype_ptr ?: __ctype_ptr__; + return __get_current_locale (_REENT)->ctype_ptr; } #ifndef _REENT_ONLY diff --git a/winsup/cygwin/ctype.cc b/winsup/cygwin/ctype.cc index 3eb6b34b7..0fe243b2d 100644 --- a/winsup/cygwin/ctype.cc +++ b/winsup/cygwin/ctype.cc @@ -64,10 +64,10 @@ __set_ctype (struct __locale_t *loc, const char *charset) } ctype_ptr = (char *) _ctype_b; } - if (loc) - loc->ctype_ptr = ctype_ptr + 127; - else - __ctype_ptr__ = ctype_ptr + 127; + loc->ctype_ptr = ctype_ptr + 127; + /* For backward compatibilty */ + if (loc == __get_global_locale ()) + __ctype_ptr__ = loc->ctype_ptr; } } /* extern "C" */