2 * Copyright (c) 2008 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "port-array.h"
21 static struct port_array_l2 l2_sentinel;
22 static struct port_array_l3 l3_sentinel;
25 /* Initializes 'pa' as an empty port_array. */
27 port_array_init(struct port_array *pa)
32 for (i = 0; i < PORT_ARRAY_L2_SIZE; i++) {
33 l2_sentinel.l2[i] = &l3_sentinel;
36 for (i = 0; i < PORT_ARRAY_L1_SIZE; i++) {
37 pa->l1[i] = &l2_sentinel;
41 /* Frees all the memory allocated for 'pa'. It is the client's responsibility
42 * to free memory that 'pa' elements point to. */
44 port_array_destroy(struct port_array *pa)
48 for (l1_idx = 0; l1_idx < PORT_ARRAY_L1_SIZE; l1_idx++) {
49 struct port_array_l2 *l2 = pa->l1[l1_idx];
51 if (l2 != &l2_sentinel) {
54 for (l2_idx = 0; l2_idx < PORT_ARRAY_L2_SIZE; l2_idx++) {
55 struct port_array_l3 *l3 = l2->l2[l2_idx];
56 if (l3 != &l3_sentinel) {
65 /* Clears all elements of 'pa' to null pointers. */
67 port_array_clear(struct port_array *pa)
69 port_array_destroy(pa);
73 /* Sets 'pa' element numbered 'idx' to 'p'. */
75 port_array_set(struct port_array *pa, uint16_t idx, void *p)
77 struct port_array_l2 **l2p, *l2;
78 struct port_array_l3 **l3p, *l3;
80 /* Traverse level 1. */
81 l2p = &pa->l1[PORT_ARRAY_L1(idx)];
82 if (*l2p == &l2_sentinel) {
83 *l2p = xmemdup(&l2_sentinel, sizeof l2_sentinel);
87 /* Traverse level 2. */
88 l3p = &l2->l2[PORT_ARRAY_L2(idx)];
89 if (*l3p == &l3_sentinel) {
90 *l3p = xmemdup(&l3_sentinel, sizeof l3_sentinel);
95 l3->l3[PORT_ARRAY_L3(idx)] = p;
99 next(const struct port_array *pa, unsigned int *idxp)
101 unsigned int idx = *idxp;
103 /* Using shift-right directly here, instead of PORT_ARRAY_L1(idx), ensures
104 * that with an initially too-big value of '*idxp' we will skip the outer
105 * loop and return NULL. */
106 unsigned int l1_idx = idx >> PORT_ARRAY_L1_SHIFT;
107 unsigned int l2_idx = PORT_ARRAY_L2(idx);
108 unsigned int l3_idx = PORT_ARRAY_L3(idx);
109 while (l1_idx < PORT_ARRAY_L1_SIZE) {
110 struct port_array_l2 *l2 = pa->l1[l1_idx];
111 if (l2 != &l2_sentinel) {
112 while (l2_idx < PORT_ARRAY_L2_SIZE) {
113 struct port_array_l3 *l3 = l2->l2[l2_idx];
114 if (l3 != &l3_sentinel) {
115 while (l3_idx < PORT_ARRAY_L3_SIZE) {
116 void *p = l3->l3[l3_idx];
118 *idxp = ((l1_idx << PORT_ARRAY_L1_SHIFT)
119 | (l2_idx << PORT_ARRAY_L2_SHIFT)
120 | (l3_idx << PORT_ARRAY_L3_SHIFT));
134 *idxp = PORT_ARRAY_SIZE;
138 /* Returns the value of the lowest-numbered non-empty element of 'pa', and sets
139 * '*idxp' to that element's index. If 'pa' is entirely empty, returns a null
140 * pointer and sets '*idxp' to 65536. */
142 port_array_first(const struct port_array *pa, unsigned int *idxp)
145 return next(pa, idxp);
148 /* Returns the value of the lowest-numbered non-empty element of 'pa' greater
149 * than the initial value of '*idxp', and sets '*idxp' to that element's index.
150 * If 'pa' contains no non-empty elements with indexes greater than the initial
151 * value of '*idxp', returns a null pointer and sets '*idxp' to 65536. */
153 port_array_next(const struct port_array *pa, unsigned int *idxp)
156 return next(pa, idxp);
159 /* Returns the number of non-null elements of 'pa'. */
161 port_array_count(const struct port_array *pa)
163 unsigned int l1_idx, l2_idx, l3_idx;
167 for (l1_idx = 0; l1_idx < PORT_ARRAY_L1_SIZE; l1_idx++) {
168 struct port_array_l2 *l2 = pa->l1[l1_idx];
169 if (l2 != &l2_sentinel) {
170 for (l2_idx = 0; l2_idx < PORT_ARRAY_L2_SIZE; l2_idx++) {
171 struct port_array_l3 *l3 = l2->l2[l2_idx];
172 if (l3 != &l3_sentinel) {
173 for (l3_idx = 0; l3_idx < PORT_ARRAY_L3_SIZE; l3_idx++) {
174 if (l3->l3[l3_idx]) {