From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 305B4389680F for ; Mon, 12 Apr 2021 08:10:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 305B4389680F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 3D5D7AF4F; Mon, 12 Apr 2021 08:10:45 +0000 (UTC) Date: Mon, 12 Apr 2021 10:10:42 +0200 From: Tom de Vries To: dwz@sourceware.org, jakub@redhat.com, mark@klomp.org Subject: [committed] Factor out pool.{c,h} Message-ID: <20210412081041.GA1885@delia.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: dwz@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Dwz mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Apr 2021 08:10:48 -0000 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 * 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 , 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 +#include +#include + +#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 , 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)