From 81352a9df9d7c8ce9f9dc63126e4cfbd8ff4a082 Mon Sep 17 00:00:00 2001 From: Matt Joyce Date: Thu, 3 Feb 2022 12:24:26 +0100 Subject: [PATCH] Add _REENT_CVTBUF(ptr) Add a _REENT_CVTBUF() macro to encapsulate access to the _cvtbuf member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch. --- newlib/libc/include/sys/reent.h | 1 + newlib/libc/reent/reent.c | 4 ++-- newlib/libc/stdlib/ecvtbuf.c | 12 ++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h index 8c88fae20..ef05dd8c9 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -719,6 +719,7 @@ struct _reent #endif /* !_REENT_SMALL */ #define _REENT_CLEANUP(_ptr) ((_ptr)->__cleanup) +#define _REENT_CVTBUF(_ptr) ((_ptr)->_cvtbuf) #define _REENT_CVTLEN(_ptr) ((_ptr)->_cvtlen) #define _REENT_EMERGENCY(_ptr) ((_ptr)->_emergency) #define _REENT_ERRNO(_ptr) ((_ptr)->_errno) diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c index 16ac04e22..2cfec5363 100644 --- a/newlib/libc/reent/reent.c +++ b/newlib/libc/reent/reent.c @@ -78,8 +78,8 @@ _reclaim_reent (struct _reent *ptr) _free_r (ptr, ptr->_misc); #endif - if (ptr->_cvtbuf) - _free_r (ptr, ptr->_cvtbuf); + if (_REENT_CVTBUF(ptr)) + _free_r (ptr, _REENT_CVTBUF(ptr)); /* We should free _sig_func to avoid a memory leak, but how to do it safely considering that a signal may be delivered immediately after the free? diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c index 0e776ab65..05f315108 100644 --- a/newlib/libc/stdlib/ecvtbuf.c +++ b/newlib/libc/stdlib/ecvtbuf.c @@ -232,14 +232,14 @@ fcvtbuf (double invalue, { if (_REENT_CVTLEN(reent) <= ndigit + 35) { - if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf, + if ((fcvt_buf = (char *) _realloc_r (reent, _REENT_CVTBUF(reent), ndigit + 36)) == NULL) return NULL; _REENT_CVTLEN(reent) = ndigit + 36; - reent->_cvtbuf = fcvt_buf; + _REENT_CVTBUF(reent) = fcvt_buf; } - fcvt_buf = reent->_cvtbuf ; + fcvt_buf = _REENT_CVTBUF(reent) ; } save = fcvt_buf; @@ -281,14 +281,14 @@ ecvtbuf (double invalue, { if (_REENT_CVTLEN(reent) <= ndigit) { - if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf, + if ((fcvt_buf = (char *) _realloc_r (reent, _REENT_CVTBUF(reent), ndigit + 1)) == NULL) return NULL; _REENT_CVTLEN(reent) = ndigit + 1; - reent->_cvtbuf = fcvt_buf; + _REENT_CVTBUF(reent) = fcvt_buf; } - fcvt_buf = reent->_cvtbuf ; + fcvt_buf = _REENT_CVTBUF(reent) ; } save = fcvt_buf;