bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bitset: let freeing functions accept NULL


From: Akim Demaille
Subject: bitset: let freeing functions accept NULL
Date: Mon, 21 Oct 2019 16:54:29 +0200

I installed this.  Apparently I was drunk when I wrote the example
in the documentation.

bitsetv does not need these changes, it is already NULL-proof.


commit ac7fd66617c1a3ace838b8660e70930c4182c1e0
Author: Akim Demaille <address@hidden>
Date:   Mon Oct 21 16:47:00 2019 +0200

    bitset: let freeing functions accept NULL
    
    * lib/bitset.c (bitset_free, bitset_obstack_free): Do nothing if
    given NULL.
    * lib/bitset.h: Document that.
    * doc/bitset.texi: Fix the example, and demonstrate bitset_free.

diff --git a/ChangeLog b/ChangeLog
index cd6281271..56102ad45 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-10-21  Akim Demaille  <address@hidden>
+
+       bitset: let freeing functions accept NULL.
+       * lib/bitset.c (bitset_free, bitset_obstack_free): Do nothing if
+       given NULL.
+       * lib/bitset.h: Document that.
+       * doc/bitset.texi: Fix the example, and demonstrate bitset_free.
+
 2019-10-15  Paul Eggert  <address@hidden>
 
        inttypes: use more-robust test for int range
diff --git a/doc/bitset.texi b/doc/bitset.texi
index d624c0952..b9e540777 100644
--- a/doc/bitset.texi
+++ b/doc/bitset.texi
@@ -48,9 +48,9 @@ Prefer fastest at memory expense.
 enum { nbits = 32 };
 
 bitset bs0 = bitset_create (nbits, BITSET_FIXED);
-bitset_set (bs1, 1);
-bitset_set (bs1, 3);
-bitset_set (bs1, 5);
+bitset_set (bs0, 1);
+bitset_set (bs0, 3);
+bitset_set (bs0, 5);
 
 bitset bs1 = bitset_create (nbits, BITSET_FIXED);
 bitset_set (bs1, 0);
@@ -58,6 +58,10 @@ bitset_set (bs1, 2);
 bitset_set (bs1, 4);
 
 bitset bs = bitset_create (nbits, BITSET_FIXED);
-bitset_or (bs, b1, b2);
+bitset_or (bs, bs0, bs1);
 ASSERT (bitset_count (bs) == 6);
+
+bitset_free (bs);
+bitset_free (bs1);
+bitset_free (bs0);
 @end smallexample
diff --git a/lib/bitset.c b/lib/bitset.c
index 7ea592e13..c3fe923bf 100644
--- a/lib/bitset.c
+++ b/lib/bitset.c
@@ -168,8 +168,11 @@ bitset_create (bitset_bindex n_bits, unsigned attr)
 void
 bitset_free (bitset bset)
 {
-  BITSET_FREE_ (bset);
-  free (bset);
+  if (bset)
+    {
+      BITSET_FREE_ (bset);
+      free (bset);
+    }
 }
 
 
@@ -177,7 +180,8 @@ bitset_free (bitset bset)
 void
 bitset_obstack_free (bitset bset)
 {
-  BITSET_FREE_ (bset);
+  if (bset)
+    BITSET_FREE_ (bset);
 }
 
 
diff --git a/lib/bitset.h b/lib/bitset.h
index e5bb015b8..ea04916ea 100644
--- a/lib/bitset.h
+++ b/lib/bitset.h
@@ -109,7 +109,7 @@ enum bitset_type bitset_type_choose (bitset_bindex, 
bitset_attrs);
 /* Create a bitset of desired type and size.  The bitset is zeroed.  */
 bitset bitset_alloc (bitset_bindex, enum bitset_type);
 
-/* Free bitset.  */
+/* Free bitset.  Do nothing if NULL.  */
 void bitset_free (bitset);
 
 /* Create a bitset of desired type and size using an obstack.  The
@@ -117,7 +117,7 @@ void bitset_free (bitset);
 bitset bitset_obstack_alloc (struct obstack *bobstack,
                              bitset_bindex, enum bitset_type);
 
-/* Free bitset allocated on obstack.  */
+/* Free bitset allocated on obstack.  Do nothing if NULL.  */
 void bitset_obstack_free (bitset);
 
 /* Create a bitset of desired size and attributes.  The bitset is zeroed.  */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]