public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
* [committed] Add util.h
@ 2021-04-09 15:15 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2021-04-09 15:15 UTC (permalink / raw)
  To: dwz, jakub, mark

Hi,

Factor out utility macros into new file util.h.

Committed to trunk.

Thanks,
- Tom

Add util.h

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

	* args.c (IMPLIES): Remove.
	* dwz.c	(pool_alloc): Use ALIGNOF_STRUCT.
	(USE_GNUC, likely, unlikely, FORCE_INLINE, UNUSED, USED, IMPLIES):
	(MAX, MIN, ALIGN_STRUCT): Move ...
	* util.h: ... here.  New file. Add ALIGNOF_STRUCT.

---
 args.c |  2 +-
 dwz.c  | 46 +++-------------------------------------------
 util.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 44 deletions(-)

diff --git a/args.c b/args.c
index 23bf1d4..cb11f25 100644
--- a/args.c
+++ b/args.c
@@ -31,7 +31,7 @@
 
 #include "args.h"
 
-#define IMPLIES(A, B) (!((A) && !(B)))
+#include "util.h"
 
 #if DEVEL
 int tracing;
diff --git a/dwz.c b/dwz.c
index dfae64f..07c576e 100644
--- a/dwz.c
+++ b/dwz.c
@@ -43,14 +43,7 @@
 #include "hashtab.h"
 #include "sha1.h"
 #include "args.h"
-
-#ifndef USE_GNUC
-#ifdef __GNUC__
-#define USE_GNUC 1
-#else
-#define USE_GNUC 0
-#endif
-#endif
+#include "util.h"
 
 #ifndef SHF_COMPRESSED
  /* Glibc elf.h contains SHF_COMPRESSED starting v2.22.  Libelf libelf.h has
@@ -118,29 +111,6 @@
 # define NT_GNU_BUILD_ID 3
 #endif
 
-#if USE_GNUC && __GNUC__ >= 3
-# define likely(x) __builtin_expect (!!(x), 1)
-# define unlikely(x) __builtin_expect (!!(x), 0)
-#else
-# define likely(x) (x)
-# define unlikely(x) (x)
-#endif
-
-#if USE_GNUC
-# define FORCE_INLINE __attribute__((always_inline))
-# define UNUSED __attribute__((unused))
-# define USED __attribute__((used))
-#else
-# define FORCE_INLINE
-# define UNUSED
-# define USED
-#endif
-
-/* Utility macro.  */
-#define IMPLIES(A, B) (!((A) && !(B)))
-#define MAX(A, B) ((A) > (B) ? (A) : (B))
-#define MIN(A, B) ((A) < (B) ? (A) : (B))
-
 /* Print memory amount M (in kb) in both exact and human readable, like so:
    1382508 (1.3G).  */
 static void
@@ -1129,11 +1099,6 @@ die_cu (dw_die_ref die)
 #define die_safe_nextdup(die) \
   ((die)->die_toplevel ? (die)->die_nextdup : (dw_die_ref) NULL)
 
-#if USE_GNUC
-# define ALIGN_STRUCT(name)
-#else
-# define ALIGN_STRUCT(name) struct align_##name { char c; struct name s; };
-#endif
 ALIGN_STRUCT (abbrev_tag)
 ALIGN_STRUCT (dw_file)
 ALIGN_STRUCT (dw_cu)
@@ -1194,13 +1159,8 @@ pool_destroy (void)
     }
 }
 
-#if USE_GNUC
-# define pool_alloc(name, size) \
-  (struct name *) pool_alloc_1 (__alignof__ (struct name), size)
-#else
-# define pool_alloc(name, size) \
-  (struct name *) pool_alloc_1 (offsetof (struct align_##name, s), size)
-#endif
+#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)
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..7caac06
--- /dev/null
+++ b/util.h
@@ -0,0 +1,60 @@
+/* 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.  */
+
+/* Utility macros.  */
+
+#define IMPLIES(A, B) (!((A) && !(B)))
+
+#define MAX(A, B) ((A) > (B) ? (A) : (B))
+#define MIN(A, B) ((A) < (B) ? (A) : (B))
+
+#ifndef USE_GNUC
+#ifdef __GNUC__
+#define USE_GNUC 1
+#else
+#define USE_GNUC 0
+#endif
+#endif
+
+#if USE_GNUC && __GNUC__ >= 3
+# define likely(x) __builtin_expect (!!(x), 1)
+# define unlikely(x) __builtin_expect (!!(x), 0)
+#else
+# define likely(x) (x)
+# define unlikely(x) (x)
+#endif
+
+#if USE_GNUC
+# define FORCE_INLINE __attribute__((always_inline))
+# define UNUSED __attribute__((unused))
+# define USED __attribute__((used))
+#else
+# define FORCE_INLINE
+# define UNUSED
+# define USED
+#endif
+
+#if USE_GNUC
+# define ALIGN_STRUCT(name)
+# define ALIGNOF_STRUCT(name) __alignof__ (struct name)
+#else
+# define ALIGN_STRUCT(name) struct align_##name { char c; struct name s; };
+# define ALIGNOF_STRUCT(name) offsetof (struct align_##name, s)
+#endif

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

only message in thread, other threads:[~2021-04-09 15:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-09 15:15 [committed] Add util.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).