public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: Binutils <binutils@sourceware.org>
Subject: Re: use libiberty xmalloc in bfd/doc/chew.c
Date: Wed, 1 Jun 2022 10:57:03 +0930	[thread overview]
Message-ID: <YpbAZ37X9BAAXack@squeak.grove.modra.org> (raw)
In-Reply-To: <CAMe9rOrGnJ_P4Zu61788NMqyo8JW0mAutUs_OROG+MesZ5kNDw@mail.gmail.com>

On Tue, May 31, 2022 at 05:10:16PM -0700, H.J. Lu wrote:
> On Mon, May 30, 2022 at 2:36 AM Alan Modra via Binutils
> <binutils@sourceware.org> wrote:
> >
> > Catch out-of-memory.
> >
> >         * doc/chew.c: Include libibery.h.
> >         (init_string_with_size, nextword): Replace malloc with xmalloc.
> >         (newentry, add_to_definition): Likewise.
> >         (catchar, catbuf): Replace realloc with xrealloc.
> >         (add_intrinsic): Replace strdup with xstrdup.
> >         * doc/local.mk (LIBIBERTY): Define.
> >         (chew): Link against libiberty.
> >         * Makefile.in: Regenerate.
> >
> This breaks --enable-pgo-build since ../libiberty/libiberty.a is a host library
> compiled with -fprofile-generate, but chew is a build program.  If we want
> to use libiberty.a in a build program, we need to build a libiberty.a for build.

Drat, I didn't think of that.  Committing the following:

We can't use libiberty.a in chew.  libiberty is a host library, chew
a build program.  Partly revert commit 7273d78f3f7a, instead define
local versions of the libiberty functions.  ansidecl.h also isn't
needed.

	* doc/chew.c: Don't include libiberty.h or ansidecl.h.
	(xmalloc, xrealloc, xstrdup): New functions.
	* doc/local.mk (LIBIBERTY): Don't define or use.
	* Makefile.in: Regenerate.

diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index 53cac75af0e..741e08d603c 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -1301,7 +1301,6 @@ doc_bfd_TEXINFOS = $(DOCFILES) doc/bfdsumm.texi
 AM_MAKEINFOFLAGS = --no-split -I "$(srcdir)/doc" -I doc
 TEXI2DVI = texi2dvi -I "$(srcdir)/doc" -I doc
 MKDOC = doc/chew$(EXEEXT_FOR_BUILD)
-LIBIBERTY = ../libiberty/libiberty.a
 
 # We can't replace these rules with an implicit rule, because
 # makes without VPATH support couldn't find the .h files in `..'.
@@ -2488,7 +2487,7 @@ doc/chew.stamp: $(srcdir)/doc/chew.c doc/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o doc/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -Idoc -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/doc/chew.c $(LIBIBERTY) && \
+	  $(srcdir)/doc/chew.c && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  doc/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@
diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index a12c00370d9..1695173970a 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -81,8 +81,6 @@
 
    Foo.  */
 
-#include "ansidecl.h"
-#include "libiberty.h"
 #include <assert.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -145,6 +143,45 @@ die (char *msg)
   exit (1);
 }
 
+void *
+xmalloc (size_t size)
+{
+  void *newmem;
+
+  if (size == 0)
+    size = 1;
+  newmem = malloc (size);
+  if (!newmem)
+    die ("out of memory");
+
+  return newmem;
+}
+
+void *
+xrealloc (void *oldmem, size_t size)
+{
+  void *newmem;
+
+  if (size == 0)
+    size = 1;
+  if (!oldmem)
+    newmem = malloc (size);
+  else
+    newmem = realloc (oldmem, size);
+  if (!newmem)
+    die ("out of memory");
+
+  return newmem;
+}
+
+char *
+xstrdup (const char *s)
+{
+  size_t len = strlen (s) + 1;
+  char *ret = xmalloc (len);
+  return memcpy (ret, s, len);
+}
+
 static void
 init_string_with_size (string_type *buffer, unsigned int size)
 {
diff --git a/bfd/doc/local.mk b/bfd/doc/local.mk
index 8c6932802f6..931942f874c 100644
--- a/bfd/doc/local.mk
+++ b/bfd/doc/local.mk
@@ -82,14 +82,12 @@ TEXI2DVI = texi2dvi -I "$(srcdir)/%D%" -I %D%
 
 MKDOC = %D%/chew$(EXEEXT_FOR_BUILD)
 
-LIBIBERTY = ../libiberty/libiberty.a
-
 $(MKDOC): %D%/chew.stamp ; @true
 %D%/chew.stamp: $(srcdir)/%D%/chew.c %D%/$(am__dirstamp)
 	$(AM_V_CCLD)$(CC_FOR_BUILD) -o %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(CFLAGS_FOR_BUILD) \
 	  $(LDFLAGS_FOR_BUILD) $(H_CFLAGS) \
 	  -I. -I$(srcdir) -I%D% -I$(srcdir)/../include -I$(srcdir)/../intl -I../intl \
-	  $(srcdir)/%D%/chew.c $(LIBIBERTY) && \
+	  $(srcdir)/%D%/chew.c && \
 	$(SHELL) $(srcdir)/../move-if-change \
 	  %D%/chw$$$$$(EXEEXT_FOR_BUILD) $(MKDOC) && \
 	touch $@

-- 
Alan Modra
Australia Development Lab, IBM

  reply	other threads:[~2022-06-01  1:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-30  9:33 Reorganise bfd/doc/chew.c a little Alan Modra
2022-05-30  9:33 ` Update K&R functions in bfd/doc/chew.c Alan Modra
2022-05-30  9:34 ` use libiberty xmalloc " Alan Modra
2022-06-01  0:10   ` H.J. Lu
2022-06-01  1:27     ` Alan Modra [this message]
2022-05-30  9:35 ` Use a union to avoid casts " Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YpbAZ37X9BAAXack@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=hjl.tools@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).