mirror: Use 'struct ref_count' for refcounting.
authorJoe Stringer <joestringer@nicira.com>
Fri, 7 Mar 2014 01:20:25 +0000 (17:20 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 11 Mar 2014 22:59:04 +0000 (15:59 -0700)
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
ofproto/ofproto-dpif-mirror.c

index 99172dd..fcc7758 100644 (file)
@@ -36,7 +36,7 @@ struct mbridge {
     bool need_revalidate;
     bool has_mirrors;
 
-    int ref_cnt;
+    struct ovs_refcount ref_cnt;
 };
 
 struct mbundle {
@@ -82,7 +82,7 @@ mbridge_create(void)
     struct mbridge *mbridge;
 
     mbridge = xzalloc(sizeof *mbridge);
-    mbridge->ref_cnt = 1;
+    ovs_refcount_init(&mbridge->ref_cnt);
 
     hmap_init(&mbridge->mbundles);
     return mbridge;
@@ -93,8 +93,7 @@ mbridge_ref(const struct mbridge *mbridge_)
 {
     struct mbridge *mbridge = CONST_CAST(struct mbridge *, mbridge_);
     if (mbridge) {
-        ovs_assert(mbridge->ref_cnt > 0);
-        mbridge->ref_cnt++;
+        ovs_refcount_ref(&mbridge->ref_cnt);
     }
     return mbridge;
 }
@@ -109,23 +108,20 @@ mbridge_unref(struct mbridge *mbridge)
         return;
     }
 
-    ovs_assert(mbridge->ref_cnt > 0);
-    if (--mbridge->ref_cnt) {
-        return;
-    }
+    if (ovs_refcount_unref(&mbridge->ref_cnt) == 1) {
+        for (i = 0; i < MAX_MIRRORS; i++) {
+            if (mbridge->mirrors[i]) {
+                mirror_destroy(mbridge, mbridge->mirrors[i]->aux);
+            }
+        }
 
-    for (i = 0; i < MAX_MIRRORS; i++) {
-        if (mbridge->mirrors[i]) {
-            mirror_destroy(mbridge, mbridge->mirrors[i]->aux);
+        HMAP_FOR_EACH_SAFE (mbundle, next, hmap_node, &mbridge->mbundles) {
+            mbridge_unregister_bundle(mbridge, mbundle->ofbundle);
         }
-    }
 
-    HMAP_FOR_EACH_SAFE (mbundle, next, hmap_node, &mbridge->mbundles) {
-        mbridge_unregister_bundle(mbridge, mbundle->ofbundle);
+        hmap_destroy(&mbridge->mbundles);
+        free(mbridge);
     }
-
-    hmap_destroy(&mbridge->mbundles);
-    free(mbridge);
 }
 
 bool