From: Ben Pfaff Date: Mon, 29 Dec 2008 21:06:56 +0000 (-0800) Subject: New functions port_array_destroy(), port_array_clear(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78a1b9f49dced47d0c1430f6dfeb711ae8d4fc42;p=openvswitch New functions port_array_destroy(), port_array_clear(). --- diff --git a/lib/port-array.c b/lib/port-array.c index 31e9a647..5d3eb8c2 100644 --- a/lib/port-array.c +++ b/lib/port-array.c @@ -33,6 +33,7 @@ #include #include "port-array.h" +#include static struct port_array_l2 l2_sentinel; static struct port_array_l3 l3_sentinel; @@ -54,6 +55,38 @@ port_array_init(struct port_array *pa) } } +/* Frees all the memory allocated for 'pa'. It is the client's responsibility + * to free memory that 'pa' elements point to. */ +void +port_array_destroy(struct port_array *pa) +{ + unsigned int l1_idx; + + 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) { + unsigned int l2_idx; + + 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) { + free(l3); + } + } + free(l2); + } + } +} + +/* Clears all elements of 'pa' to null pointers. */ +void +port_array_clear(struct port_array *pa) +{ + port_array_destroy(pa); + port_array_init(pa); +} + /* Sets 'pa' element numbered 'idx' to 'p'. */ void port_array_set(struct port_array *pa, uint16_t idx, void *p) diff --git a/lib/port-array.h b/lib/port-array.h index 478a073c..01b3c100 100644 --- a/lib/port-array.h +++ b/lib/port-array.h @@ -99,6 +99,7 @@ port_array_get(const struct port_array *pa, uint16_t idx) 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_first(const struct port_array *, unsigned int *); void *port_array_next(const struct port_array *, unsigned int *);