util: New function nullable_xstrdup().
authorBen Pfaff <blp@ovn.org>
Sat, 25 Jun 2016 04:23:16 +0000 (21:23 -0700)
committerBen Pfaff <blp@ovn.org>
Mon, 27 Jun 2016 03:31:28 +0000 (20:31 -0700)
It's a pretty common pattern so create a function for it.

Signed-off-by: Ben Pfaff <blp@ovn.org>
16 files changed:
lib/dpif-netdev.c
lib/jsonrpc.c
lib/ovsdb-error.c
lib/syslog-libc.c
lib/util.c
lib/util.h
lib/vlog.c
ofproto/ofproto-dpif-ipfix.c
ofproto/ofproto-dpif-sflow.c
ofproto/ofproto.c
ovn/utilities/ovn-nbctl.c
ovn/utilities/ovn-sbctl.c
ovsdb/replication.c
utilities/ovs-vsctl.c
vswitchd/bridge.c
vtep/vtep-ctl.c

index 70f320d..ff4227c 100644 (file)
@@ -2531,7 +2531,7 @@ dpif_netdev_pmd_set(struct dpif *dpif, const char *cmask)
 
     if (!cmask_equals(dp->requested_pmd_cmask, cmask)) {
         free(dp->requested_pmd_cmask);
-        dp->requested_pmd_cmask = cmask ? xstrdup(cmask) : NULL;
+        dp->requested_pmd_cmask = nullable_xstrdup(cmask);
     }
 
     return 0;
@@ -2690,9 +2690,7 @@ reconfigure_pmd_threads(struct dp_netdev *dp)
     /* Reconfigures the cpu mask. */
     ovs_numa_set_cpu_mask(dp->requested_pmd_cmask);
     free(dp->pmd_cmask);
-    dp->pmd_cmask = dp->requested_pmd_cmask
-                    ? xstrdup(dp->requested_pmd_cmask)
-                    : NULL;
+    dp->pmd_cmask = nullable_xstrdup(dp->requested_pmd_cmask);
 
     /* Restores the non-pmd. */
     dp_netdev_set_nonpmd(dp);
index 1112b4a..aba742c 100644 (file)
@@ -512,7 +512,7 @@ jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
 {
     struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
     msg->type = type;
-    msg->method = method ? xstrdup(method) : NULL;
+    msg->method = nullable_xstrdup(method);
     msg->params = params;
     msg->result = result;
     msg->error = error;
index d3549cb..dbe8149 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+/* Copyright (c) 2009, 2010, 2011, 2012, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -183,8 +183,8 @@ ovsdb_error_clone(const struct ovsdb_error *old)
     if (old) {
         struct ovsdb_error *new = xmalloc(sizeof *new);
         new->tag = old->tag;
-        new->details = old->details ? xstrdup(old->details) : NULL;
-        new->syntax = old->syntax ? xstrdup(old->syntax) : NULL;
+        new->details = nullable_xstrdup(old->details);
+        new->syntax = nullable_xstrdup(old->syntax);
         new->errno_ = old->errno_;
         return new;
     } else {
index 8858c3c..b702d41 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Nicira, Inc.
+ * Copyright (c) 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -64,7 +64,7 @@ syslog_libc_open(struct syslogger *this OVS_UNUSED, int facility)
      * 'program_name', so make a private copy just for openlog().  (We keep
      * a pointer to the private copy to suppress memory leak warnings in
      * case openlog() does make its own copy.) */
-    ident = program_name ? xstrdup(program_name) : NULL;
+    ident = nullable_xstrdup(program_name);
 
     openlog(ident, LOG_NDELAY, facility);
 }
index 6ca04ad..e1dc3d2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -151,6 +151,12 @@ xstrdup(const char *s)
     return xmemdup0(s, strlen(s));
 }
 
+char * MALLOC_LIKE
+nullable_xstrdup(const char *s)
+{
+    return s ? xstrdup(s) : NULL;
+}
+
 char *
 xvasprintf(const char *format, va_list args)
 {
index 7be4a30..e738c9f 100644 (file)
@@ -112,6 +112,7 @@ void *xrealloc(void *, size_t);
 void *xmemdup(const void *, size_t) MALLOC_LIKE;
 char *xmemdup0(const char *, size_t) MALLOC_LIKE;
 char *xstrdup(const char *) MALLOC_LIKE;
+char *nullable_xstrdup(const char *) MALLOC_LIKE;
 char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
 char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
 void *x2nrealloc(void *p, size_t *n, size_t s);
index 30b5bc2..333337b 100644 (file)
@@ -426,7 +426,7 @@ vlog_reopen_log_file(void)
     char *fn;
 
     ovs_mutex_lock(&log_file_mutex);
-    fn = log_file_name ? xstrdup(log_file_name) : NULL;
+    fn = nullable_xstrdup(log_file_name);
     ovs_mutex_unlock(&log_file_mutex);
 
     if (fn) {
index 35f481d..5744abb 100644 (file)
@@ -469,12 +469,6 @@ nullable_string_is_equal(const char *a, const char *b)
     return a ? b && !strcmp(a, b) : !b;
 }
 
-static char *
-nullable_xstrdup(const char *s)
-{
-    return s ? xstrdup(s) : NULL;
-}
-
 static bool
 ofproto_ipfix_bridge_exporter_options_equal(
     const struct ofproto_ipfix_bridge_exporter_options *a,
index 5d26b7c..7d0aa36 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  * Copyright (c) 2009 InMon Corp.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -115,8 +115,8 @@ ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
 {
     struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
     sset_clone(&new->targets, &old->targets);
-    new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
-    new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
+    new->agent_device = nullable_xstrdup(old->agent_device);
+    new->control_ip = nullable_xstrdup(old->control_ip);
     return new;
 }
 
index 087b321..cf3235a 100644 (file)
@@ -784,8 +784,7 @@ void
 ofproto_set_cpu_mask(const char *cmask)
 {
     free(pmd_cpu_mask);
-
-    pmd_cpu_mask = cmask ? xstrdup(cmask) : NULL;
+    pmd_cpu_mask = nullable_xstrdup(cmask);
 }
 
 void
@@ -811,7 +810,7 @@ void
 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
 {
     free(p->dp_desc);
-    p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
+    p->dp_desc = nullable_xstrdup(dp_desc);
 }
 
 int
index 7789cdd..345647a 100644 (file)
@@ -235,7 +235,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
index c0ee518..a12b247 100644 (file)
@@ -249,7 +249,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
index 1fa0f25..9bf3721 100644 (file)
@@ -115,7 +115,7 @@ replication_run(struct shash *all_dbs)
 void
 set_remote_ovsdb_server(const char *remote_server)
 {
-    remote_ovsdb_server = remote_server ? xstrdup(remote_server) : NULL;
+    remote_ovsdb_server = nullable_xstrdup(remote_server);
 }
 
 void
index 4d7f9f9..c4ae692 100644 (file)
@@ -287,7 +287,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':
index f8f8964..917a80c 100644 (file)
@@ -1212,9 +1212,7 @@ bridge_configure_ipfix(struct bridge *br)
                                               "enable-output-sampling", false);
 
         virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
-        be_opts.virtual_obs_id = (virtual_obs_id
-                                  ? xstrdup(virtual_obs_id)
-                                  : NULL);
+        be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
     }
 
     if (n_fe_opts > 0) {
@@ -1236,9 +1234,7 @@ bridge_configure_ipfix(struct bridge *br)
                                                   "enable-tunnel-sampling", true);
                 virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
                                           "virtual_obs_id");
-                opts->virtual_obs_id = (virtual_obs_id
-                                        ? xstrdup(virtual_obs_id)
-                                        : NULL);
+                opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
                 opts++;
             }
         }
index 5c18971..759150f 100644 (file)
@@ -237,7 +237,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
             }
             shash_add_nocopy(local_options,
                              xasprintf("--%s", options[idx].name),
-                             optarg ? xstrdup(optarg) : NULL);
+                             nullable_xstrdup(optarg));
             break;
 
         case 'h':