[ovs-dev] [QoS2 1/3] port-array: Add port_array_delete() function.

Ben Pfaff blp at nicira.com
Tue Jun 8 00:05:59 UTC 2010


port_array_delete(pa, idx) is equivalent to port_array_set(pa, idx, NULL),
but it never allocates memory and avoids conditionals.
---
 lib/port-array.c        |   13 ++++++++++++-
 lib/port-array.h        |    3 ++-
 ofproto/ofproto-sflow.c |    2 +-
 ofproto/ofproto.c       |    2 +-
 ofproto/pinsched.c      |    2 +-
 5 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/port-array.c b/lib/port-array.c
index e8f35be..be4c0dc 100644
--- a/lib/port-array.c
+++ b/lib/port-array.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 Nicira Networks.
+ * Copyright (c) 2008, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -95,6 +95,17 @@ port_array_set(struct port_array *pa, uint16_t idx, void *p)
     l3->l3[PORT_ARRAY_L3(idx)] = p;
 }
 
+/* Sets 'pa' element numbered 'idx' to NULL. */
+void
+port_array_delete(struct port_array *pa, uint16_t idx)
+{
+    unsigned int l1_idx = PORT_ARRAY_L1(idx);
+    unsigned int l2_idx = PORT_ARRAY_L2(idx);
+    unsigned int l3_idx = PORT_ARRAY_L3(idx);
+
+    pa->l1[l1_idx]->l2[l2_idx]->l3[l3_idx] = NULL;
+}
+
 static void *
 next(const struct port_array *pa, unsigned int *idxp)
 {
diff --git a/lib/port-array.h b/lib/port-array.h
index 36410bf..15fa41d 100644
--- a/lib/port-array.h
+++ b/lib/port-array.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -84,6 +84,7 @@ void port_array_init(struct port_array *);
 void port_array_destroy(struct port_array *);
 void port_array_clear(struct port_array *);
 void port_array_set(struct port_array *, uint16_t idx, void *);
+void port_array_delete(struct port_array *, uint16_t idx);
 void *port_array_first(const struct port_array *, unsigned int *);
 void *port_array_next(const struct port_array *, unsigned int *);
 unsigned int port_array_count(const struct port_array *);
diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c
index 37c1bb7..0c6cc49 100644
--- a/ofproto/ofproto-sflow.c
+++ b/ofproto/ofproto-sflow.c
@@ -354,7 +354,7 @@ ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t odp_port)
         }
         netdev_close(osp->netdev);
         free(osp);
-        port_array_set(&os->ports, odp_port, NULL);
+        port_array_delete(&os->ports, odp_port);
     }
 }
 
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index b3d820a..3ed20bf 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -1468,7 +1468,7 @@ ofport_remove(struct ofproto *p, struct ofport *ofport)
     uint16_t odp_port = ofp_port_to_odp_port(ofport->opp.port_no);
 
     netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
-    port_array_set(&p->ports, odp_port, NULL);
+    port_array_delete(&p->ports, odp_port);
     shash_delete(&p->port_by_name,
                  shash_find(&p->port_by_name, (char *) ofport->opp.name));
     if (p->sflow) {
diff --git a/ofproto/pinsched.c b/ofproto/pinsched.c
index 6af2bb6..d749ee4 100644
--- a/ofproto/pinsched.c
+++ b/ofproto/pinsched.c
@@ -70,7 +70,7 @@ dequeue_packet(struct pinsched *ps, struct ovs_queue *q,
     struct ofpbuf *packet = queue_pop_head(q);
     if (!q->n) {
         free(q);
-        port_array_set(&ps->queues, port_no, NULL);
+        port_array_delete(&ps->queues, port_no);
     }
     ps->n_queued--;
     return packet;
-- 
1.7.1





More information about the dev mailing list