From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id E7135384F016; Sat, 9 Jul 2022 12:35:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7135384F016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] gas: utility notes memory alloc functions X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: d3be5dab558ab91789800a03fc2c1dc3c529eaf5 X-Git-Newrev: c30081c1f955a9fea2da8988bab96191e3d2171e Message-Id: <20220709123504.E7135384F016@sourceware.org> Date: Sat, 9 Jul 2022 12:35:04 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Jul 2022 12:35:05 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc30081c1f955= a9fea2da8988bab96191e3d2171e commit c30081c1f955a9fea2da8988bab96191e3d2171e Author: Alan Modra Date: Thu Jul 7 08:40:26 2022 +0930 gas: utility notes memory alloc functions =20 Makes it a little easier to use the notes obstack for persistent storage. =20 * as.h (gas_mul_overflow): Define. * symbols.h (notes_alloc, notes_calloc, notes_memdup), (notes_strdup, notes_concat, notes_free): Declare. * symbols.c (notes_alloc, notes_calloc, notes_memdup), (notes_strdup, notes_concat, notes_free): New functions. (save_symbol_name): Use notes_strdup. (symbol_create, local_symbol_make, local_symbol_convert), (symbol_clone, decode_local_label_name): Use notes_alloc. Diff: --- gas/as.h | 8 ++++++ gas/symbols.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++---= ---- gas/symbols.h | 7 +++++ 3 files changed, 90 insertions(+), 10 deletions(-) diff --git a/gas/as.h b/gas/as.h index 4fac7d21a32..0ee3873bd1a 100644 --- a/gas/as.h +++ b/gas/as.h @@ -122,6 +122,14 @@ void *mempcpy(void *, const void *, size_t); =20 #define xfree free =20 +#if GCC_VERSION >=3D 7000 +#define gas_mul_overflow(a, b, res) __builtin_mul_overflow (a, b, res) +#else +/* Assumes unsigned values. Careful! Args evaluated multiple times. */ +#define gas_mul_overflow(a, b, res) \ + ((*res) =3D (a), (*res) *=3D (b), (b) !=3D 0 && (*res) / (b) !=3D (a)) +#endif + #include "asintl.h" =20 #define BAD_CASE(val) \ diff --git a/gas/symbols.c b/gas/symbols.c index e3fddee8c79..00ae49b91ed 100644 --- a/gas/symbols.c +++ b/gas/symbols.c @@ -242,6 +242,75 @@ struct xsymbol dot_symbol_x; #endif =20 struct obstack notes; + +/* Utility functions to allocate and duplicate memory on the notes + obstack, each like the corresponding function without "notes_" + prefix. All of these exit on an allocation failure. */ + +void * +notes_alloc (size_t size) +{ + return obstack_alloc (¬es, size); +} + +void * +notes_calloc (size_t n, size_t size) +{ + size_t amt; + void *ret; + if (gas_mul_overflow (n, size, &amt)) + { + obstack_alloc_failed_handler (); + abort (); + } + ret =3D notes_alloc (amt); + memset (ret, 0, amt); + return ret; +} + +void * +notes_memdup (const void *src, size_t copy_size, size_t alloc_size) +{ + void *ret =3D obstack_alloc (¬es, alloc_size); + memcpy (ret, src, copy_size); + if (alloc_size > copy_size) + memset ((char *) ret + copy_size, 0, alloc_size - copy_size); + return ret; +} + +char * +notes_strdup (const char *str) +{ + size_t len =3D strlen (str) + 1; + return notes_memdup (str, len, len); +} + +char * +notes_concat (const char *first, ...) +{ + va_list args; + const char *str; + + va_start (args, first); + for (str =3D first; str; str =3D va_arg (args, const char *)) + { + size_t size =3D strlen (str); + obstack_grow (¬es, str, size); + } + va_end (args); + obstack_1grow (¬es, 0); + return obstack_finish (¬es); +} + +/* Use with caution! Frees PTR and all more recently allocated memory + on the notes obstack. */ + +void +notes_free (void *ptr) +{ + obstack_free (¬es, ptr); +} + #ifdef TE_PE /* The name of an external symbol which is used to make weak PE symbol names unique. */ @@ -274,13 +343,10 @@ symbol_new (const char *name, segT segment, fragS *fr= ag, valueT valu) static const char * save_symbol_name (const char *name) { - size_t name_length; char *ret; =20 gas_assert (name !=3D NULL); - name_length =3D strlen (name) + 1; /* +1 for \0. */ - obstack_grow (¬es, name, name_length); - ret =3D (char *) obstack_finish (¬es); + ret =3D notes_strdup (name); =20 #ifdef tc_canonicalize_symbol_name ret =3D tc_canonicalize_symbol_name (ret); @@ -343,7 +409,7 @@ symbol_create (const char *name, segT segment, fragS *f= rag, valueT valu) preserved_copy_of_name =3D save_symbol_name (name); =20 size =3D sizeof (symbolS) + sizeof (struct xsymbol); - symbolP =3D (symbolS *) obstack_alloc (¬es, size); + symbolP =3D notes_alloc (size); =20 /* symbol must be born in some fixed state. This seems as good as any. = */ memset (symbolP, 0, size); @@ -377,7 +443,7 @@ local_symbol_make (const char *name, segT section, frag= S *frag, valueT val) =20 name_copy =3D save_symbol_name (name); =20 - ret =3D (struct local_symbol *) obstack_alloc (¬es, sizeof *ret); + ret =3D notes_alloc (sizeof *ret); ret->flags =3D flags; ret->hash =3D 0; ret->name =3D name_copy; @@ -403,7 +469,7 @@ local_symbol_convert (void *sym) =20 ++local_symbol_conversion_count; =20 - xtra =3D (struct xsymbol *) obstack_alloc (¬es, sizeof (*xtra)); + xtra =3D notes_alloc (sizeof (*xtra)); memset (xtra, 0, sizeof (*xtra)); val =3D ent->lsy.value; ent->sy.x =3D xtra; @@ -717,8 +783,7 @@ symbol_clone (symbolS *orgsymP, int replace) orgsymP =3D local_symbol_convert (orgsymP); bsymorg =3D orgsymP->bsym; =20 - newsymP =3D (symbolS *) obstack_alloc (¬es, (sizeof (symbolS) - + sizeof (struct xsymbol))); + newsymP =3D notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol)); *newsymP =3D *orgsymP; newsymP->x =3D (struct xsymbol *) (newsymP + 1); *newsymP->x =3D *orgsymP->x; @@ -2104,7 +2169,7 @@ decode_local_label_name (char *s) instance_number =3D (10 * instance_number) + *p - '0'; =20 message_format =3D _("\"%d\" (instance number %d of a %s label)"); - symbol_decode =3D (char *) obstack_alloc (¬es, strlen (message_format= ) + 30); + symbol_decode =3D notes_alloc (strlen (message_format) + 30); sprintf (symbol_decode, message_format, label_number, instance_number, t= ype); =20 return symbol_decode; diff --git a/gas/symbols.h b/gas/symbols.h index 19eb658ca68..240e08f8df6 100644 --- a/gas/symbols.h +++ b/gas/symbols.h @@ -35,6 +35,13 @@ extern int symbol_table_frozen; default. */ extern int symbols_case_sensitive; =20 +extern void *notes_alloc (size_t); +extern void *notes_calloc (size_t, size_t); +extern void *notes_memdup (const void *, size_t, size_t); +extern char *notes_strdup (const char *); +extern char *notes_concat (const char *, ...); +extern void notes_free (void *); + char * symbol_relc_make_expr (expressionS *); char * symbol_relc_make_sym (symbolS *); char * symbol_relc_make_value (offsetT);