<p>Harald Welte has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/12787">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">bitvec: Add bitvec_bytes_used() function<br><br>This new bitvec API function returns the number of bytes used in a given<br>bit-vector.<br><br>Change-Id: Id4bd7f7543f5b0f4f6f876e283bd065039c37646<br>---<br>M include/osmocom/core/bitvec.h<br>M tests/bitvec/bitvec_test.c<br>M tests/bitvec/bitvec_test.ok<br>3 files changed, 37 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/87/12787/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h</span><br><span>index c9bab39..da2d4e4 100644</span><br><span>--- a/include/osmocom/core/bitvec.h</span><br><span>+++ b/include/osmocom/core/bitvec.h</span><br><span>@@ -84,4 +84,13 @@</span><br><span>                        unsigned int array_len, bool dry_run,</span><br><span>                        unsigned int num_bits);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Return the number of bytes used within the bit vector */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline unsigned int bitvec_used_bytes(const struct bitvec *bv)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     unsigned int bytes = bv->cur_bit/8;</span><br><span style="color: hsl(120, 100%, 40%);">+        if (bv->cur_bit%8)</span><br><span style="color: hsl(120, 100%, 40%);">+         bytes++;</span><br><span style="color: hsl(120, 100%, 40%);">+      return bytes;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! @} */</span><br><span>diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c</span><br><span>index d0bc30c..c8795db 100644</span><br><span>--- a/tests/bitvec/bitvec_test.c</span><br><span>+++ b/tests/bitvec/bitvec_test.c</span><br><span>@@ -181,6 +181,29 @@</span><br><span>  test_array_item(17, &b, n, array, n * 3);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void test_used_bytes()</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct bitvec b;</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t d[32];</span><br><span style="color: hsl(120, 100%, 40%);">+        unsigned int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     b.data = d;</span><br><span style="color: hsl(120, 100%, 40%);">+   b.data_len = sizeof(d);</span><br><span style="color: hsl(120, 100%, 40%);">+       bitvec_zero(&b);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        OSMO_ASSERT(bitvec_used_bytes(&b) == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        for (i = 0; i < 8; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+          bitvec_set_bit(&b, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+            OSMO_ASSERT(bitvec_used_bytes(&b) == 1);</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   for (i = 8; i < 16; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+         bitvec_set_bit(&b, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+            OSMO_ASSERT(bitvec_used_bytes(&b) == 2);</span><br><span style="color: hsl(120, 100%, 40%);">+  }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int main(int argc, char **argv)</span><br><span> {</span><br><span>       struct bitvec bv;</span><br><span>@@ -286,6 +309,9 @@</span><br><span>      bitvec_zero(&bv);</span><br><span>        test_bitvec_rl_curbit(&bv, 1, 64, 0);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ printf("\nbitvec bytes used.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+   test_used_bytes();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>         printf("\nbitvec ok.\n");</span><br><span>  return 0;</span><br><span> }</span><br><span>diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok</span><br><span>index 6281973..a48912d 100644</span><br><span>--- a/tests/bitvec/bitvec_test.ok</span><br><span>+++ b/tests/bitvec/bitvec_test.ok</span><br><span>@@ -168,4 +168,6 @@</span><br><span> ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ </span><br><span> bitvec_runlength....</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+bitvec bytes used.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> bitvec ok.</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/12787">change 12787</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/12787"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id4bd7f7543f5b0f4f6f876e283bd065039c37646 </div>
<div style="display:none"> Gerrit-Change-Number: 12787 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Harald Welte <laforge@gnumonks.org> </div>