public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
* [committed] Factor out pool.{c,h}
@ 2021-04-12  8:10 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2021-04-12  8:10 UTC (permalink / raw)
  To: dwz, jakub, mark

Hi,

Factor out pool functions into files pool.c and pool.h.

Committed to trunk.

Thanks,
- Tom

Factor out pool.{c,h}

2021-04-12  Tom de Vries  <tdevries@suse.de>

	* Makefile (OBJECTS): Add pool.o.
	* dwz.c (dwz_oom): Remove static.
	(cleanup): Update call to pool_destroy.
	(read_multifile): Call finalize_pool.
	(dwz_files_1): Call pool_destroy.
	(pool_alloc_1, pool_destroy): Move ...
	* pool.c: ... here.  New file.
	(pool_destroy): Add pool parameter.
	* dwz.c (pool_alloc): Move ...
	* pool.h: ... here.  New file.

---
 Makefile |   2 +-
 dwz.c    |  64 ++++-----------------------------------
 pool.c   | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pool.h   |  26 ++++++++++++++++
 4 files changed, 135 insertions(+), 60 deletions(-)

diff --git a/Makefile b/Makefile
index 02da6c4..95fc80b 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@ exec_prefix = $(prefix)
 bindir = $(exec_prefix)/bin
 datarootdir = $(prefix)/share
 mandir = $(datarootdir)/man
-OBJECTS = args.o dwz.o hashtab.o sha1.o dwarfnames.o
+OBJECTS = args.o dwz.o hashtab.o pool.o sha1.o dwarfnames.o
 LIBS=-lelf
 dwz: $(OBJECTS)
 	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
diff --git a/dwz.c b/dwz.c
index 07c576e..879fae6 100644
--- a/dwz.c
+++ b/dwz.c
@@ -44,6 +44,7 @@
 #include "sha1.h"
 #include "args.h"
 #include "util.h"
+#include "pool.h"
 
 #ifndef SHF_COMPRESSED
  /* Glibc elf.h contains SHF_COMPRESSED starting v2.22.  Libelf libelf.h has
@@ -196,7 +197,7 @@ static jmp_buf oom_buf;
 /* Handle OOM situation.  If handling more than one file, we might
    just fail to handle some large file due to OOM, but could very well
    handle other smaller files after it.  */
-static void
+void
 dwz_oom (void)
 {
   longjmp (oom_buf, 1);
@@ -1104,64 +1105,12 @@ ALIGN_STRUCT (dw_file)
 ALIGN_STRUCT (dw_cu)
 ALIGN_STRUCT (dw_die)
 
-/* Big pool allocator.  obstack isn't efficient, because it aligns everything
-   too much, and allocates too small chunks.  All these objects are only freed
-   together.  */
-
-/* Pointer to the start of the current pool chunk, current first free byte
-   in the chunk and byte after the end of the current pool chunk.  */
-static unsigned char *pool, *pool_next, *pool_limit;
 
 /* After read_multifile, pool variable is moved over to this variable
    as the pool from read_multifile needs to be around for subsequent dwz
    calls.  Freed only during the final cleanup at the very end.  */
 static unsigned char *alt_pool;
 
-/* Allocate SIZE bytes with ALIGN bytes alignment from the pool.  */
-static void *
-pool_alloc_1 (unsigned int align, unsigned int size)
-{
-  void *ret;
-  if (pool == NULL
-      || (size_t) (pool_limit - pool_next) < (size_t) align + size)
-    {
-      size_t new_size = (size_t) align + size;
-      unsigned char *new_pool;
-      new_size += sizeof (void *);
-      if (new_size < 16384 * 1024 - 64)
-	new_size = 16384 * 1024 - 64;
-      new_pool = (unsigned char *) malloc (new_size);
-      if (new_pool == NULL)
-	dwz_oom ();
-      *(unsigned char **) new_pool = pool;
-      pool_next = new_pool + sizeof (unsigned char *);
-      pool_limit = new_pool + new_size;
-      pool = new_pool;
-    }
-  pool_next = (unsigned char *) (((uintptr_t) pool_next + align - 1)
-				 & ~(uintptr_t) (align - 1));
-  ret = pool_next;
-  pool_next += size;
-  return ret;
-}
-
-/* Free the whole pool.  */
-static void
-pool_destroy (void)
-{
-  pool_next = NULL;
-  pool_limit = NULL;
-  while (pool)
-    {
-      void *p = (void *) pool;
-      pool = *(unsigned char **) pool;
-      free (p);
-    }
-}
-
-#define pool_alloc(name, size) \
-  (struct name *) pool_alloc_1 (ALIGNOF_STRUCT (name), size)
-
 static struct abbrev_tag *
 pool_clone_abbrev (struct abbrev_tag *t)
 {
@@ -14411,7 +14360,7 @@ cleanup (void)
   memset (&ob, '\0', sizeof (ob2));
   die_nontoplevel_freelist = NULL;
   die_collapsed_child_freelist = NULL;
-  pool_destroy ();
+  pool_destroy (NULL);
   first_cu = NULL;
   last_cu = NULL;
   ptr_size = 0;
@@ -16162,10 +16111,7 @@ read_multifile (int fd, unsigned int die_count)
       alt_macro_htab = macro_htab;
       macro_htab = NULL;
       alt_first_cu = first_cu;
-      alt_pool = pool;
-      pool = NULL;
-      pool_next = NULL;
-      pool_limit = NULL;
+      alt_pool = finalize_pool ();
       alt_ob = ob;
       alt_ob2 = ob2;
       memset (&ob, '\0', sizeof (ob));
@@ -16826,10 +16772,10 @@ dwz_files_1 (int nr_files, char *files[], bool hardlink,
   off_htab = alt_off_htab;
   dup_htab = alt_dup_htab;
   macro_htab = alt_macro_htab;
-  pool = alt_pool;
   ob = alt_ob;
   ob2 = alt_ob2;
   cleanup ();
+  pool_destroy (alt_pool);
 
   return ret;
 }
diff --git a/pool.c b/pool.c
new file mode 100644
index 0000000..c8c7171
--- /dev/null
+++ b/pool.c
@@ -0,0 +1,103 @@
+/* Copyright (C) 2001-2021 Red Hat, Inc.
+   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 SUSE LLC.
+   Written by Jakub Jelinek <jakub@redhat.com>, 2012.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; see the file COPYING.  If not, write to
+   the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
+
+/* Big pool allocator.  obstack isn't efficient, because it aligns everything
+   too much, and allocates too small chunks.  All these objects are only freed
+   together.  */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include "pool.h"
+
+/* Pointer to the start of the current pool chunk, current first free byte
+   in the chunk and byte after the end of the current pool chunk.  */
+
+static unsigned char *pool, *pool_next, *pool_limit;
+
+extern void dwz_oom (void);
+
+/* Allocate SIZE bytes with ALIGN bytes alignment from the pool.  */
+void *
+pool_alloc_1 (unsigned int align, unsigned int size)
+{
+  void *ret;
+  if (pool == NULL
+      || (size_t) (pool_limit - pool_next) < (size_t) align + size)
+    {
+      size_t new_size = (size_t) align + size;
+      unsigned char *new_pool;
+      new_size += sizeof (void *);
+      if (new_size < 16384 * 1024 - 64)
+	new_size = 16384 * 1024 - 64;
+      new_pool = (unsigned char *) malloc (new_size);
+      if (new_pool == NULL)
+	dwz_oom ();
+      *(unsigned char **) new_pool = pool;
+      pool_next = new_pool + sizeof (unsigned char *);
+      pool_limit = new_pool + new_size;
+      pool = new_pool;
+    }
+  pool_next = (unsigned char *) (((uintptr_t) pool_next + align - 1)
+				 & ~(uintptr_t) (align - 1));
+  ret = pool_next;
+  pool_next += size;
+  return ret;
+}
+
+/* Finalize a pool and return it.  */
+unsigned char *
+finalize_pool (void)
+{
+  unsigned char *ret = pool;
+  pool = NULL;
+  pool_next = NULL;
+  pool_limit = NULL;
+  return ret;
+}
+
+/* Free pool P.  */
+static void
+pool_destroy_1 (unsigned char *p)
+{
+  while (p)
+    {
+      void *elem = (void *) p;
+      p = *(unsigned char **) p;
+      free (elem);
+    }
+}
+
+/* Free pool P, or the current pool if NULL.  */
+void
+pool_destroy (unsigned char *p)
+{
+  if (p != NULL)
+    {
+      pool_destroy_1 (p);
+      return;
+    }
+
+  pool_destroy_1 (pool);
+  pool = NULL;
+  pool_next = NULL;
+  pool_limit = NULL;
+}
diff --git a/pool.h b/pool.h
new file mode 100644
index 0000000..b32d2bf
--- /dev/null
+++ b/pool.h
@@ -0,0 +1,26 @@
+/* Copyright (C) 2001-2021 Red Hat, Inc.
+   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 SUSE LLC.
+   Written by Jakub Jelinek <jakub@redhat.com>, 2012.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; see the file COPYING.  If not, write to
+   the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
+
+extern void *pool_alloc_1 (unsigned int, unsigned int);
+extern unsigned char *finalize_pool (void);
+extern void pool_destroy (unsigned char *);
+
+#define pool_alloc(name, size) \
+  (struct name *) pool_alloc_1 (ALIGNOF_STRUCT (name), size)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-12  8:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-12  8:10 [committed] Factor out pool.{c,h} Tom de Vries

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).