From patchwork Thu Oct 6 03:16:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 678715 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (archives.nicira.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 3sqHr93Dz0z9ryZ for ; Thu, 6 Oct 2016 14:16:57 +1100 (AEDT) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 69235108BC; Wed, 5 Oct 2016 20:16:56 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx3v3.cudamail.com (mx3.cudamail.com [64.34.241.5]) by archives.nicira.com (Postfix) with ESMTPS id A50D1108B4 for ; Wed, 5 Oct 2016 20:16:55 -0700 (PDT) Received: from bar6.cudamail.com (localhost [127.0.0.1]) by mx3v3.cudamail.com (Postfix) with ESMTPS id 3D2C7161510 for ; Wed, 5 Oct 2016 21:16:55 -0600 (MDT) X-ASG-Debug-ID: 1475723814-0b3237258c61370001-byXFYA Received: from mx1-pf2.cudamail.com ([192.168.24.2]) by bar6.cudamail.com with ESMTP id 9UA9cm4YXaA7p92T (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 05 Oct 2016 21:16:54 -0600 (MDT) X-Barracuda-Envelope-From: blp@ovn.org X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.2 Received: from unknown (HELO relay3-d.mail.gandi.net) (217.70.183.195) by mx1-pf2.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 6 Oct 2016 03:16:54 -0000 Received-SPF: pass (mx1-pf2.cudamail.com: SPF record at ovn.org designates 217.70.183.195 as permitted sender) X-Barracuda-Apparent-Source-IP: 217.70.183.195 X-Barracuda-RBL-IP: 217.70.183.195 Received: from mfilter22-d.gandi.net (mfilter22-d.gandi.net [217.70.178.150]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 1F212A80D0; Thu, 6 Oct 2016 05:16:52 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter22-d.gandi.net Received: from relay3-d.mail.gandi.net ([IPv6:::ffff:217.70.183.195]) by mfilter22-d.gandi.net (mfilter22-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id ZLdWOtCNQgAA; Thu, 6 Oct 2016 05:16:50 +0200 (CEST) X-Originating-IP: 173.228.112.90 Received: from sigabrt.gateway.sonic.net (173-228-112-90.dsl.dynamic.fusionbroadband.com [173.228.112.90]) (Authenticated sender: blp@ovn.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id D1FAEA80CB; Thu, 6 Oct 2016 05:16:49 +0200 (CEST) X-CudaMail-Envelope-Sender: blp@ovn.org From: Ben Pfaff To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E2-1004093872 X-CudaMail-DTE: 100516 X-CudaMail-Originating-IP: 217.70.183.195 Date: Wed, 5 Oct 2016 20:16:38 -0700 X-ASG-Orig-Subj: [##CM-E2-1004093872##][PATCH 01/15] ovsdb-idlc: Use ovsdb_datum_from_smap() instead of open-coding it. Message-Id: <1475723812-20932-1-git-send-email-blp@ovn.org> X-Mailer: git-send-email 2.1.3 X-Barracuda-Connect: UNKNOWN[192.168.24.2] X-Barracuda-Start-Time: 1475723814 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Cc: Ben Pfaff Subject: [ovs-dev] [PATCH 01/15] ovsdb-idlc: Use ovsdb_datum_from_smap() instead of open-coding it. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" There's no reason to have three copies of this code for every smap-type column. The code wasn't a perfect match for ovsdb_datum_from_smap(), so this commit also changes ovsdb_datum_from_smap() to better suit it. It only had one caller and the new design is adequate for that caller. Signed-off-by: Ben Pfaff --- lib/ovsdb-data.c | 21 +++++++++------------ lib/ovsdb-data.h | 4 ++-- ovsdb/ovsdb-idlc.in | 45 +++------------------------------------------ vswitchd/bridge.c | 1 + 4 files changed, 15 insertions(+), 56 deletions(-) diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 266a3e4..0dda73a 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc. +/* Copyright (c) 2009, 2010, 2011, 2012, 2014, 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. @@ -1542,27 +1542,24 @@ ovsdb_datum_to_bare(const struct ovsdb_datum *datum, } } -/* Initializes 'datum' as a string-to-string map whose contents are taken from - * 'smap'. Destroys 'smap'. */ +/* Initializes 'datum' as a string-to-string map whose contents are copied from + * 'smap', which is not modified. */ void -ovsdb_datum_from_smap(struct ovsdb_datum *datum, struct smap *smap) +ovsdb_datum_from_smap(struct ovsdb_datum *datum, const struct smap *smap) { - struct smap_node *node, *next; - size_t i; - datum->n = smap_count(smap); datum->keys = xmalloc(datum->n * sizeof *datum->keys); datum->values = xmalloc(datum->n * sizeof *datum->values); - i = 0; - SMAP_FOR_EACH_SAFE (node, next, smap) { - smap_steal(smap, node, - &datum->keys[i].string, &datum->values[i].string); + struct smap_node *node; + size_t i = 0; + SMAP_FOR_EACH (node, smap) { + datum->keys[i].string = xstrdup(node->key); + datum->values[i].string = xstrdup(node->value); i++; } ovs_assert(i == datum->n); - smap_destroy(smap); ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); } diff --git a/lib/ovsdb-data.h b/lib/ovsdb-data.h index 98633ef..ae2672e 100644 --- a/lib/ovsdb-data.h +++ b/lib/ovsdb-data.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010, 2011, 2012, 2015 Nicira, Inc. +/* Copyright (c) 2009, 2010, 2011, 2012, 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. @@ -178,7 +178,7 @@ void ovsdb_datum_to_string(const struct ovsdb_datum *, void ovsdb_datum_to_bare(const struct ovsdb_datum *, const struct ovsdb_type *, struct ds *); -void ovsdb_datum_from_smap(struct ovsdb_datum *, struct smap *); +void ovsdb_datum_from_smap(struct ovsdb_datum *, const struct smap *); /* Comparison. */ uint32_t ovsdb_datum_hash(const struct ovsdb_datum *, diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in index 0037b90..fb2241b 100755 --- a/ovsdb/ovsdb-idlc.in +++ b/ovsdb/ovsdb-idlc.in @@ -678,20 +678,7 @@ const struct ovsdb_datum * ovs_assert(inited); if (%(c)s) { - struct smap_node *node; - size_t i; - - datum.n = smap_count(%(c)s); - datum.keys = xmalloc(datum.n * sizeof *datum.keys); - datum.values = xmalloc(datum.n * sizeof *datum.values); - - i = 0; - SMAP_FOR_EACH (node, %(c)s) { - datum.keys[i].string = xstrdup(node->key); - datum.values[i].string = xstrdup(node->value); - i++; - } - ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); + ovsdb_datum_from_smap(&datum, %(c)s); } else { ovsdb_datum_init_empty(&datum); } @@ -932,20 +919,7 @@ void ovs_assert(inited); if (%(c)s) { - struct smap_node *node; - size_t i; - - datum.n = smap_count(%(c)s); - datum.keys = xmalloc(datum.n * sizeof *datum.keys); - datum.values = xmalloc(datum.n * sizeof *datum.values); - - i = 0; - SMAP_FOR_EACH (node, %(c)s) { - datum.keys[i].string = xstrdup(node->key); - datum.values[i].string = xstrdup(node->value); - i++; - } - ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); + ovsdb_datum_from_smap(&datum, %(c)s); } else { ovsdb_datum_init_empty(&datum); } @@ -1107,20 +1081,7 @@ void ovs_assert(inited); if (%(c)s) { - struct smap_node *node; - size_t i; - - datum.n = smap_count(%(c)s); - datum.keys = xmalloc(datum.n * sizeof *datum.keys); - datum.values = xmalloc(datum.n * sizeof *datum.values); - - i = 0; - SMAP_FOR_EACH (node, %(c)s) { - datum.keys[i].string = xstrdup(node->key); - datum.values[i].string = xstrdup(node->value); - i++; - } - ovsdb_datum_sort_unique(&datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); + ovsdb_datum_from_smap(&datum, %(c)s); } else { ovsdb_datum_init_empty(&datum); } diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c index 61cb966..4504217 100644 --- a/vswitchd/bridge.c +++ b/vswitchd/bridge.c @@ -2618,6 +2618,7 @@ run_system_stats(void) txn = ovsdb_idl_txn_create(idl); ovsdb_datum_from_smap(&datum, stats); + smap_destroy(stats); ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics, &datum); ovsdb_idl_txn_commit(txn);