From 08c2028943a2cf55daf137729d1f8225e8bd8d57 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 19 Mar 2013 17:11:44 +0100 Subject: [PATCH] Bitarray: Added bit_array_xrealloc() As suggested by Martina Balintova and inspired by her code. Also renamed bit-count.c to bit-array.c. --- ucw/Makefile | 2 +- ucw/{bit-count.c => bit-array.c} | 15 ++++++++++++++- ucw/bitarray.h | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) rename ucw/{bit-count.c => bit-array.c} (67%) diff --git a/ucw/Makefile b/ucw/Makefile index d4a055ce..6c47e712 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -21,7 +21,7 @@ LIBUCW_MODS= \ wildmatch regex \ prime primetable random \ time-stamp time-timer time-conf \ - bit-ffs bit-fls bit-count \ + bit-ffs bit-fls bit-array \ url \ mainloop main-block main-rec \ proctitle exitstatus runcmd \ diff --git a/ucw/bit-count.c b/ucw/bit-array.c similarity index 67% rename from ucw/bit-count.c rename to ucw/bit-array.c index a84a6b7c..584d752e 100644 --- a/ucw/bit-count.c +++ b/ucw/bit-array.c @@ -1,7 +1,8 @@ /* - * UCW Library -- Counting bits in bitarray + * UCW Library -- Support routines for bitarray * * (c) 2012 Pavel Charvat + * (c) 2013 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -20,6 +21,18 @@ uns bit_array_count_bits(bitarray_t a, uns n) return m; } +bitarray_t bit_array_xrealloc(bitarray_t a, uns old_n, uns new_n) +{ + uns old_bytes = BIT_ARRAY_BYTES(old_n); + uns new_bytes = BIT_ARRAY_BYTES(new_n); + if (old_bytes == new_bytes) + return a; + a = xrealloc(a, new_bytes); + if (old_bytes < new_bytes) + bzero(a + old_bytes, new_bytes - old_bytes); + return a; +} + #ifdef TEST #include diff --git a/ucw/bitarray.h b/ucw/bitarray.h index e2994c64..daf8fa01 100644 --- a/ucw/bitarray.h +++ b/ucw/bitarray.h @@ -25,6 +25,8 @@ bit_array_xmalloc(uns n) return xmalloc(BIT_ARRAY_BYTES(n)); } +bitarray_t bit_array_xrealloc(bitarray_t a, uns old_n, uns new_n); + static inline bitarray_t bit_array_xmalloc_zero(uns n) { -- 2.39.5