[libvirt] [PATCH 2/7] maint: avoid static zero init in core files

Eric Blake eblake at redhat.com
Tue Oct 28 20:34:45 UTC 2014


C guarantees that static variables are zero-initialized.  Some older
compilers (and also gcc -fno-zero-initialized-in-bss) create larger
binaries if you explicitly zero-initialize a static variable.

* src/libvirt.c: Fix initialization.
* src/util/viralloc.c: Likewise.
* src/util/virdbus.c: Likewise.
* src/util/virevent.c: Likewise.
* src/util/virfile.c (safezero): Likewise.
* src/util/virlog.c: Likewise.
* src/util/virnetlink.c: Likewise.
* src/util/virthread.h (VIR_ONCE_GLOBAL_INIT): Likewise.
* src/util/virprocess.c (virProcessGetStartTime): Likewise.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/libvirt.c         | 18 +++++++++---------
 src/util/viralloc.c   | 10 +++++-----
 src/util/virdbus.c    |  4 ++--
 src/util/virevent.c   | 14 +++++++-------
 src/util/virfile.c    |  2 +-
 src/util/virlog.c     | 16 ++++++++--------
 src/util/virnetlink.c |  2 +-
 src/util/virprocess.c |  3 +--
 src/util/virthread.h  |  4 ++--
 9 files changed, 36 insertions(+), 37 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index 5594883..3abedb4 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -122,22 +122,22 @@ VIR_LOG_INIT("libvirt");
     } while (0)

 static virHypervisorDriverPtr virHypervisorDriverTab[MAX_DRIVERS];
-static int virHypervisorDriverTabCount = 0;
+static int virHypervisorDriverTabCount;
 static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS];
-static int virNetworkDriverTabCount = 0;
+static int virNetworkDriverTabCount;
 static virInterfaceDriverPtr virInterfaceDriverTab[MAX_DRIVERS];
-static int virInterfaceDriverTabCount = 0;
+static int virInterfaceDriverTabCount;
 static virStorageDriverPtr virStorageDriverTab[MAX_DRIVERS];
-static int virStorageDriverTabCount = 0;
+static int virStorageDriverTabCount;
 static virNodeDeviceDriverPtr virNodeDeviceDriverTab[MAX_DRIVERS];
-static int virNodeDeviceDriverTabCount = 0;
+static int virNodeDeviceDriverTabCount;
 static virSecretDriverPtr virSecretDriverTab[MAX_DRIVERS];
-static int virSecretDriverTabCount = 0;
+static int virSecretDriverTabCount;
 static virNWFilterDriverPtr virNWFilterDriverTab[MAX_DRIVERS];
-static int virNWFilterDriverTabCount = 0;
+static int virNWFilterDriverTabCount;
 #ifdef WITH_LIBVIRTD
 static virStateDriverPtr virStateDriverTab[MAX_DRIVERS];
-static int virStateDriverTabCount = 0;
+static int virStateDriverTabCount;
 #endif


@@ -354,7 +354,7 @@ static struct gcry_thread_cbs virTLSThreadImpl = {
 #endif /* WITH_GNUTLS_GCRYPT */


-static bool virGlobalError = false;
+static bool virGlobalError;
 static virOnceControl virGlobalOnce = VIR_ONCE_CONTROL_INITIALIZER;

 static void
diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index dc423f5..63f43d0 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -32,11 +32,11 @@
 VIR_LOG_INIT("util.alloc");

 #if TEST_OOM
-static int testMallocNext = 0;
-static int testMallocFailFirst = 0;
-static int testMallocFailLast = 0;
-static void (*testMallocHook)(int, void*) = NULL;
-static void *testMallocHookData = NULL;
+static int testMallocNext;
+static int testMallocFailFirst;
+static int testMallocFailLast;
+static void (*testMallocHook)(int, void*);
+static void *testMallocHookData;

 void virAllocTestInit(void)
 {
diff --git a/src/util/virdbus.c b/src/util/virdbus.c
index dc3a535..7c24cbf 100644
--- a/src/util/virdbus.c
+++ b/src/util/virdbus.c
@@ -35,8 +35,8 @@ VIR_LOG_INIT("util.dbus");
 #ifdef WITH_DBUS

 static bool sharedBus = true;
-static DBusConnection *systembus = NULL;
-static DBusConnection *sessionbus = NULL;
+static DBusConnection *systembus;
+static DBusConnection *sessionbus;
 static virOnceControl systemonce = VIR_ONCE_CONTROL_INITIALIZER;
 static virOnceControl sessiononce = VIR_ONCE_CONTROL_INITIALIZER;
 static DBusError systemdbuserr;
diff --git a/src/util/virevent.c b/src/util/virevent.c
index 84d28a4..54b6396 100644
--- a/src/util/virevent.c
+++ b/src/util/virevent.c
@@ -32,12 +32,12 @@

 VIR_LOG_INIT("util.event");

-static virEventAddHandleFunc addHandleImpl = NULL;
-static virEventUpdateHandleFunc updateHandleImpl = NULL;
-static virEventRemoveHandleFunc removeHandleImpl = NULL;
-static virEventAddTimeoutFunc addTimeoutImpl = NULL;
-static virEventUpdateTimeoutFunc updateTimeoutImpl = NULL;
-static virEventRemoveTimeoutFunc removeTimeoutImpl = NULL;
+static virEventAddHandleFunc addHandleImpl;
+static virEventUpdateHandleFunc updateHandleImpl;
+static virEventRemoveHandleFunc removeHandleImpl;
+static virEventAddTimeoutFunc addTimeoutImpl;
+static virEventUpdateTimeoutFunc updateTimeoutImpl;
+static virEventRemoveTimeoutFunc removeTimeoutImpl;


 /*****************************************************
@@ -291,7 +291,7 @@ int virEventRegisterDefaultImpl(void)
  * function, as it will block forever if there are no
  * registered events.
  *
- *   static bool quit = false;
+ *   static bool quit;
  *
  *   while (!quit) {
  *     if (virEventRunDefaultImpl() < 0)
diff --git a/src/util/virfile.c b/src/util/virfile.c
index c379df5..64f0e5b 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1054,7 +1054,7 @@ safezero(int fd, off_t offset, off_t len)
     char *buf;
     unsigned long long remain, bytes;
 # ifdef HAVE_MMAP
-    static long pagemask = 0;
+    static long pagemask;
     off_t map_skip;

     /* align offset and length, rounding offset down and length up */
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 056950e..286ad9e 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -1,7 +1,7 @@
 /*
  * virlog.c: internal logging and debugging
  *
- * Copyright (C) 2008, 2010-2013 Red Hat, Inc.
+ * Copyright (C) 2008, 2010-2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -61,7 +61,7 @@

 VIR_LOG_INIT("util.log");

-static regex_t *virLogRegex = NULL;
+static regex_t *virLogRegex;


 #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}"
@@ -86,8 +86,8 @@ typedef struct _virLogFilter virLogFilter;
 typedef virLogFilter *virLogFilterPtr;

 static int virLogFiltersSerial = 1;
-static virLogFilterPtr virLogFilters = NULL;
-static int virLogNbFilters = 0;
+static virLogFilterPtr virLogFilters;
+static int virLogNbFilters;

 /*
  * Outputs are used to emit the messages retained
@@ -105,8 +105,8 @@ struct _virLogOutput {
 typedef struct _virLogOutput virLogOutput;
 typedef virLogOutput *virLogOutputPtr;

-static virLogOutputPtr virLogOutputs = NULL;
-static int virLogNbOutputs = 0;
+static virLogOutputPtr virLogOutputs;
+static int virLogNbOutputs;

 /*
  * Default priorities
@@ -645,7 +645,7 @@ virLogStackTraceToFd(int fd)
 {
     void *array[100];
     int size;
-    static bool doneWarning = false;
+    static bool doneWarning;
     const char *msg = "Stack trace not available on this platform\n";

 #define STRIP_DEPTH 3
@@ -782,7 +782,7 @@ virLogOutputToSyslog(virLogSourcePtr source ATTRIBUTE_UNUSED,
     syslog(virLogPrioritySyslog(priority), "%s", str);
 }

-static char *current_ident = NULL;
+static char *current_ident;


 static void
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index 29511ad..eab888f 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -103,7 +103,7 @@ static int nextWatch = 1;
 /* Linux kernel supports up to MAX_LINKS (32 at the time) individual
  * netlink protocols. */
 static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
-static virNetlinkHandle *placeholder_nlhandle = NULL;
+static virNetlinkHandle *placeholder_nlhandle;

 /* Function definitions */

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index fe497b9..0c8a32f 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -916,11 +916,10 @@ int virProcessGetStartTime(pid_t pid,
 int virProcessGetStartTime(pid_t pid,
                            unsigned long long *timestamp)
 {
-    static int warned = 0;
+    static int warned;
     if (virAtomicIntInc(&warned) == 1) {
         VIR_WARN("Process start time of pid %llu not available on this platform",
                  (unsigned long long)pid);
-        warned = true;
     }
     *timestamp = 0;
     return 0;
diff --git a/src/util/virthread.h b/src/util/virthread.h
index 4b92a43..7146f0f 100644
--- a/src/util/virthread.h
+++ b/src/util/virthread.h
@@ -1,7 +1,7 @@
 /*
  * virthread.h: basic thread synchronization primitives
  *
- * Copyright (C) 2009-2011, 2013 Red Hat, Inc.
+ * Copyright (C) 2009-2011, 2013-2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -187,7 +187,7 @@ int virThreadLocalSet(virThreadLocalPtr l, void*) ATTRIBUTE_RETURN_CHECK;
  */
 # define VIR_ONCE_GLOBAL_INIT(classname)                                \
     static virOnceControl classname ## OnceControl = VIR_ONCE_CONTROL_INITIALIZER; \
-    static virErrorPtr classname ## OnceError = NULL;                   \
+    static virErrorPtr classname ## OnceError;                          \
                                                                         \
     static void classname ## Once(void)                                 \
     {                                                                   \
-- 
1.9.3




More information about the libvir-list mailing list