It's a pretty common pattern so create a function for it.
Signed-off-by: Ben Pfaff <blp@ovn.org>
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;
/* 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);
{
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;
-/* 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.
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 {
/*
- * 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.
* '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);
}
/*
- * 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.
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)
{
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);
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) {
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,
/*
- * 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");
{
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;
}
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
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
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
- optarg ? xstrdup(optarg) : NULL);
+ nullable_xstrdup(optarg));
break;
case 'h':
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
- optarg ? xstrdup(optarg) : NULL);
+ nullable_xstrdup(optarg));
break;
case 'h':
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
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
- optarg ? xstrdup(optarg) : NULL);
+ nullable_xstrdup(optarg));
break;
case 'h':
"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) {
"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++;
}
}
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
- optarg ? xstrdup(optarg) : NULL);
+ nullable_xstrdup(optarg));
break;
case 'h':