Skip to content

Commit 5cc6157

Browse files
committed
stdlib: Use plain static for local mbstate_t vars
Functions that provide a static mbstate_t context aren't required to be re-entrant, so stop making those thread-local. Signed-off-by: Keith Packard <[email protected]>
1 parent 1b75a3a commit 5cc6157

File tree

7 files changed

+54
-62
lines changed

7 files changed

+54
-62
lines changed

newlib/libc/stdlib/mblen.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,26 @@ effects vary with the locale.
4444
#include "local.h"
4545

4646
int
47-
mblen (const char *s,
48-
size_t n)
47+
mblen (const char *s, size_t n)
4948
{
5049
#ifdef _MB_CAPABLE
51-
int retval = 0;
52-
static NEWLIB_THREAD_LOCAL _mbstate_t _mblen_state;
50+
int retval = 0;
51+
static _mbstate_t _mblen_state;
5352

54-
retval = __MBTOWC (NULL, s, n, &_mblen_state);
55-
if (retval < 0)
53+
retval = __MBTOWC (NULL, s, n, &_mblen_state);
54+
if (retval < 0)
5655
{
57-
_mblen_state.__count = 0;
58-
return -1;
56+
_mblen_state.__count = 0;
57+
return -1;
5958
}
60-
else
61-
return retval;
59+
else
60+
return retval;
6261

6362
#else /* not _MB_CAPABLE */
64-
if (s == NULL || *s == '\0')
65-
return 0;
66-
if (n == 0)
67-
return -1;
68-
return 1;
63+
if (s == NULL || *s == '\0')
64+
return 0;
65+
if (n == 0)
66+
return -1;
67+
return 1;
6968
#endif /* not _MB_CAPABLE */
7069
}

newlib/libc/stdlib/mbrtowc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mbrtowc (wchar_t *__restrict pwc,
1919
#ifdef _MB_CAPABLE
2020
if (ps == NULL)
2121
{
22-
static NEWLIB_THREAD_LOCAL mbstate_t _mbrtowc_state;
22+
static mbstate_t _mbrtowc_state;
2323
ps = &_mbrtowc_state;
2424
}
2525
#endif
@@ -31,7 +31,9 @@ mbrtowc (wchar_t *__restrict pwc,
3131

3232
if (retval == -1)
3333
{
34+
#ifdef _MB_CAPABLE
3435
ps->__count = 0;
36+
#endif
3537
_REENT_ERRNO(reent) = EILSEQ;
3638
return (size_t)(-1);
3739
}

newlib/libc/stdlib/mbsnrtowcs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SYNOPSIS
2020
mbstate_t *__restrict <[ps]>);
2121
2222
#include <wchar.h>
23-
size_t mbsnrtowcs(wchar_t *__ restrict <[dst]>,
23+
size_t mbsnrtowcs(wchar_t *__ restrict <[dst]>,
2424
const char **__restrict <[src]>, size_t <[nms]>,
2525
size_t <[len]>, mbstate_t *__restrict <[ps]>);
2626
@@ -78,7 +78,7 @@ mbsnrtowcs (
7878
#ifdef _MB_CAPABLE
7979
if (ps == NULL)
8080
{
81-
static NEWLIB_THREAD_LOCAL mbstate_t _mbsrtowcs_state;
81+
static mbstate_t _mbsrtowcs_state;
8282
ps = &_mbsrtowcs_state;
8383
}
8484
#endif
@@ -90,8 +90,8 @@ mbsnrtowcs (
9090
len = (size_t)-1;
9191
tmp_src = *src;
9292
src = &tmp_src;
93-
}
94-
93+
}
94+
9595
max = len;
9696
while (len > 0)
9797
{

newlib/libc/stdlib/mbtowc.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,26 @@ effects vary with the locale.
4949
#include <wchar.h>
5050
#include "local.h"
5151

52+
#ifdef _MB_CAPABLE
53+
static mbstate_t _mbtowc_state;
54+
#define ps &_mbtowc_state
55+
#else
56+
#define ps NULL
57+
#endif
58+
5259
int
5360
mbtowc (wchar_t *__restrict pwc,
5461
const char *__restrict s,
5562
size_t n)
5663
{
57-
#ifdef _MB_CAPABLE
58-
int retval = 0;
59-
static NEWLIB_THREAD_LOCAL mbstate_t _mbtowc_state;
64+
int retval;
6065

61-
retval = __MBTOWC (pwc, s, n, &_mbtowc_state);
62-
63-
if (retval == -1)
64-
{
65-
_mbtowc_state.__count = 0;
66-
_REENT_ERRNO(r) = EILSEQ;
67-
return -1;
68-
}
69-
return retval;
70-
#else /* not _MB_CAPABLE */
71-
if (s == NULL)
72-
return 0;
73-
if (n == 0)
74-
return -1;
75-
if (pwc)
76-
*pwc = (wchar_t) *s;
77-
return (*s != '\0');
78-
#endif /* not _MB_CAPABLE */
66+
retval = __MBTOWC (pwc, s, n, ps);
67+
if (retval == -1) {
68+
#ifdef _MB_CAPABLE
69+
_mbtowc_state.__count = 0;
70+
#endif
71+
errno = EILSEQ;
72+
}
73+
return retval;
7974
}

newlib/libc/stdlib/wcrtomb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ wcrtomb (char *__restrict s,
1818
#ifdef _MB_CAPABLE
1919
if (ps == NULL)
2020
{
21-
static NEWLIB_THREAD_LOCAL mbstate_t _wcrtomb_state;
21+
static mbstate_t _wcrtomb_state;
2222
ps = &_wcrtomb_state;
2323
}
2424
#endif
@@ -30,7 +30,9 @@ wcrtomb (char *__restrict s,
3030

3131
if (retval == -1)
3232
{
33+
#ifdef _MB_CAPABLE
3334
ps->__count = 0;
35+
#endif
3436
_REENT_ERRNO(reent) = EILSEQ;
3537
return (size_t)(-1);
3638
}

newlib/libc/stdlib/wcsnrtombs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PORTABILITY
7474

7575
size_t
7676
_wcsnrtombs_l (char *dst, const wchar_t **src, size_t nwc,
77-
size_t len, mbstate_t *ps, struct __locale_t *loc)
77+
size_t len, mbstate_t *ps, locale_t loc)
7878
{
7979
char *ptr = dst;
8080
char buff[10];
@@ -85,7 +85,7 @@ _wcsnrtombs_l (char *dst, const wchar_t **src, size_t nwc,
8585
#ifdef _MB_CAPABLE
8686
if (ps == NULL)
8787
{
88-
static NEWLIB_THREAD_LOCAL mbstate_t _wcsrtombs_state;
88+
static mbstate_t _wcsrtombs_state;
8989
ps = &_wcsrtombs_state;
9090
}
9191
#endif
@@ -135,7 +135,7 @@ _wcsnrtombs_l (char *dst, const wchar_t **src, size_t nwc,
135135
}
136136

137137
return n;
138-
}
138+
}
139139

140140
size_t
141141
wcsnrtombs (char *__restrict dst,

newlib/libc/stdlib/wctomb.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,25 @@ effects vary with the locale.
4646
#include <errno.h>
4747
#include "local.h"
4848

49+
#ifdef _MB_CAPABLE
50+
static mbstate_t _mbtowc_state;
51+
#define ps &_mbtowc_state
52+
#else
53+
#define ps NULL
54+
#endif
55+
4956
int
5057
wctomb (char *s,
5158
wchar_t wchar)
5259
{
53-
#ifdef _MB_CAPABLE
5460
int retval;
55-
static NEWLIB_THREAD_LOCAL mbstate_t _wctomb_state;
5661

57-
retval = __WCTOMB (s, wchar, &_wctomb_state);
62+
retval = __WCTOMB (s, wchar, ps);
5863
if (retval == -1) {
59-
_wctomb_state.__count = 0;
64+
#ifdef _MB_CAPABLE
65+
_mbtowc_state.__count = 0;
66+
#endif
6067
errno = EILSEQ;
6168
}
6269
return retval;
63-
#else /* not _MB_CAPABLE */
64-
if (s == NULL)
65-
return 0;
66-
67-
/* Verify that wchar is a valid single-byte character. */
68-
if ((size_t)wchar >= 0x100) {
69-
errno = EILSEQ;
70-
return -1;
71-
}
72-
73-
*s = (char) wchar;
74-
return 1;
75-
#endif /* not _MB_CAPABLE */
7670
}

0 commit comments

Comments
 (0)