From: Ben Pfaff Date: Sat, 28 Feb 2009 00:47:47 +0000 (-0800) Subject: New function port_array_count(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f580f2cd4e335fa89e3e665288153aa191b4f55;p=openvswitch New function port_array_count(). --- diff --git a/lib/port-array.c b/lib/port-array.c index 5d3eb8c2..23ba2b34 100644 --- a/lib/port-array.c +++ b/lib/port-array.c @@ -172,3 +172,29 @@ port_array_next(const struct port_array *pa, unsigned int *idxp) ++*idxp; return next(pa, idxp); } + +/* Returns the number of non-null elements of 'pa'. */ +unsigned int +port_array_count(const struct port_array *pa) +{ + unsigned int l1_idx, l2_idx, l3_idx; + unsigned int count; + + count = 0; + for (l1_idx = 0; l1_idx < PORT_ARRAY_L1_SIZE; l1_idx++) { + struct port_array_l2 *l2 = pa->l1[l1_idx]; + if (l2 != &l2_sentinel) { + for (l2_idx = 0; l2_idx < PORT_ARRAY_L2_SIZE; l2_idx++) { + struct port_array_l3 *l3 = l2->l2[l2_idx]; + if (l3 != &l3_sentinel) { + for (l3_idx = 0; l3_idx < PORT_ARRAY_L3_SIZE; l3_idx++) { + if (l3->l3[l3_idx]) { + count++; + } + } + } + } + } + } + return count; +} diff --git a/lib/port-array.h b/lib/port-array.h index 01b3c100..1522fccb 100644 --- a/lib/port-array.h +++ b/lib/port-array.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -103,5 +103,6 @@ void port_array_clear(struct port_array *); void port_array_set(struct port_array *, uint16_t idx, void *); 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 *); #endif /* port-array.h */