From ac7fd66617c1a3ace838b8660e70930c4182c1e0 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 21 Oct 2019 16:47:00 +0200 Subject: [PATCH] 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. --- ChangeLog | 8 ++++++++ doc/bitset.texi | 12 ++++++++---- lib/bitset.c | 10 +++++++--- lib/bitset.h | 4 ++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index cd62812712..56102ad45b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2019-10-21 Akim Demaille + + 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 inttypes: use more-robust test for int range diff --git a/doc/bitset.texi b/doc/bitset.texi index d624c0952a..b9e5407778 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 7ea592e134..c3fe923bfa 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 e5bb015b88..ea04916ea4 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. */ -- 2.39.5