]> diplodocus.org Git - nmh/commitdiff
sbr/vector.c: Only allocate bvector storage for set bits.
authorRalph Corderoy <ralph@inputplus.co.uk>
Fri, 28 Apr 2017 11:45:26 +0000 (12:45 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Fri, 28 Apr 2017 11:45:26 +0000 (12:45 +0100)
Now that a pointer to all the bit storage doesn't escape vector.c,
there's no need to allocate storage, that's initialised to 0, just to
then clear a bit in it.  Only extend the bits's storage for set bits.
Remove the unused initial size parameter from bvector_create() as it
simplifies the implementation.

h/mh.h
sbr/folder_read.c
sbr/folder_realloc.c
sbr/vector.c
uip/sortm.c

diff --git a/h/mh.h b/h/mh.h
index 1edf482..0a62f58 100644 (file)
--- a/h/mh.h
+++ b/h/mh.h
@@ -192,7 +192,7 @@ extern struct swit anoyes[];        /* standard yes/no switches */
  */
 typedef struct bvector *bvector_t;
 
-bvector_t bvector_create (size_t /* initial size in bits, can be 0 */);
+bvector_t bvector_create (void);
 void bvector_copy (bvector_t, bvector_t);
 void bvector_free (bvector_t);
 void bvector_clear (bvector_t, size_t);
@@ -329,7 +329,7 @@ struct msgs {
 #define make_seq_private(mp,seqnum) \
         bvector_set (mp->attrstats, FFATTRSLOT + seqnum)
 #define make_all_public(mp) \
-        mp->attrstats = bvector_create(0); bvector_clear_all (mp->attrstats)
+        mp->attrstats = bvector_create(); bvector_clear_all (mp->attrstats)
 
 /*
  * macros for folder attributes
index e742153..e157dd2 100644 (file)
@@ -136,7 +136,7 @@ folder_read (char *name, int lockflag)
     mp->num_msgstats = MSGSTATNUM (mp->lowoff, mp->hghoff);
     mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp));
     for (i = 0, v = mp->msgstats; i < mp->num_msgstats; ++i, ++v) {
-       *v = bvector_create (0);
+       *v = bvector_create ();
     }
 
     mp->msgattrs = svector_create (0);
index fbef854..2084e0e 100644 (file)
@@ -58,7 +58,7 @@ folder_realloc (struct msgs *mp, int lo, int hi)
            mh_xrealloc (mp->msgstats, MSGSTATSIZE(mp));
        for (i = old_size, v = &mp->msgstats[old_size]; i < new_size;
             ++i, ++v) {
-           *v = bvector_create (0);
+           *v = bvector_create ();
        }
     } else {
        /*
@@ -72,7 +72,7 @@ folder_realloc (struct msgs *mp, int lo, int hi)
        mp->num_msgstats = MSGSTATNUM (lo, hi);
        tmpstats = mh_xmalloc (MSGSTATSIZE(mp));
        for (i = 0, t = tmpstats; i < mp->num_msgstats; ++i, ++t) {
-           *t = bvector_create (0);
+           *t = bvector_create ();
        }
 
        /* then copy messages status array with shift */
index b7103b7..e376065 100644 (file)
@@ -60,25 +60,16 @@ struct bvector {
 static void bvector_resize (bvector_t vec, size_t newsize);
 
 bvector_t
-bvector_create (size_t init_size) {
+bvector_create (void) {
     bvector_t vec;
-    size_t bytes;
 
     /* See "wider than unsigned long" comment above. */
     assert (sizeof *vec->bits <= sizeof 1ul);
 
     NEW(vec);
-
-    if (init_size <= BVEC_INIT_SIZE) {
-        vec->bits = vec->tiny;
-        vec->maxsize = BVEC_INIT_SIZE;
-        memset(vec->tiny, 0, sizeof vec->tiny);
-        return vec;
-    }
-
-    bytes = BVEC_BYTES(init_size);
-    vec->bits = mh_xcalloc (1, bytes);
-    vec->maxsize = bytes * CHAR_BIT;
+    vec->bits = vec->tiny;
+    vec->maxsize = BVEC_INIT_SIZE;
+    memset(vec->tiny, 0, sizeof vec->tiny);
 
     return vec;
 }
@@ -103,13 +94,8 @@ bvector_free (bvector_t vec) {
 
 void
 bvector_clear (bvector_t vec, size_t n) {
-    size_t word = BVEC_WORD(n);
-    size_t offset = BVEC_OFFSET(n);
-
-    if (n >= vec->maxsize)
-        bvector_resize (vec, n);
-
-    vec->bits[word] &= ~(1ul << offset);
+    if (n < vec->maxsize)
+        vec->bits[BVEC_WORD(n)] &= ~(1ul << BVEC_OFFSET(n));
 }
 
 
@@ -131,12 +117,10 @@ bvector_set (bvector_t vec, size_t n) {
 
 unsigned int
 bvector_at (bvector_t vec, size_t i) {
-    size_t word = BVEC_WORD(i);
-    size_t offset = BVEC_OFFSET(i);
+    if (i < vec->maxsize)
+        return !!(vec->bits[BVEC_WORD(i)] & (1ul << BVEC_OFFSET(i)));
 
-    return i < vec->maxsize
-        ?  (vec->bits[word] & (1ul << offset) ? 1 : 0)
-        :  0;
+    return 0;
 }
 
 static void
index f07e786..4f5602f 100644 (file)
@@ -540,7 +540,7 @@ static void
 rename_msgs (struct msgs *mp, struct smsg **mlist)
 {
     int i, j, old, new;
-    bvector_t tmpset = bvector_create (0);
+    bvector_t tmpset = bvector_create ();
     char f1[BUFSIZ], tmpfil[BUFSIZ];
     char newbuf[PATH_MAX + 1];
     struct smsg *sp;