Change in libosmocore[master]: bitvec: Add bitvec_bytes_used() function

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Sun Feb 3 11:07:45 UTC 2019


Harald Welte has uploaded this change for review. ( https://gerrit.osmocom.org/12787


Change subject: bitvec: Add bitvec_bytes_used() function
......................................................................

bitvec: Add bitvec_bytes_used() function

This new bitvec API function returns the number of bytes used in a given
bit-vector.

Change-Id: Id4bd7f7543f5b0f4f6f876e283bd065039c37646
---
M include/osmocom/core/bitvec.h
M tests/bitvec/bitvec_test.c
M tests/bitvec/bitvec_test.ok
3 files changed, 37 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/87/12787/1

diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index c9bab39..da2d4e4 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -84,4 +84,13 @@
 			      unsigned int array_len, bool dry_run,
 			      unsigned int num_bits);
 
+/*! Return the number of bytes used within the bit vector */
+static inline unsigned int bitvec_used_bytes(const struct bitvec *bv)
+{
+	unsigned int bytes = bv->cur_bit/8;
+	if (bv->cur_bit%8)
+		bytes++;
+	return bytes;
+}
+
 /*! @} */
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index d0bc30c..c8795db 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -181,6 +181,29 @@
 	test_array_item(17, &b, n, array, n * 3);
 }
 
+static void test_used_bytes()
+{
+	struct bitvec b;
+	uint8_t d[32];
+	unsigned int i;
+
+	b.data = d;
+	b.data_len = sizeof(d);
+	bitvec_zero(&b);
+
+	OSMO_ASSERT(bitvec_used_bytes(&b) == 0);
+
+	for (i = 0; i < 8; i++) {
+		bitvec_set_bit(&b, 1);
+		OSMO_ASSERT(bitvec_used_bytes(&b) == 1);
+	}
+
+	for (i = 8; i < 16; i++) {
+		bitvec_set_bit(&b, 1);
+		OSMO_ASSERT(bitvec_used_bytes(&b) == 2);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct bitvec bv;
@@ -286,6 +309,9 @@
 	bitvec_zero(&bv);
 	test_bitvec_rl_curbit(&bv, 1, 64, 0);
 
+	printf("\nbitvec bytes used.\n");
+	test_used_bytes();
+
 	printf("\nbitvec ok.\n");
 	return 0;
 }
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index 6281973..a48912d 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -168,4 +168,6 @@
 ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ ........ 
 bitvec_runlength....
 
+bitvec bytes used.
+
 bitvec ok.

-- 
To view, visit https://gerrit.osmocom.org/12787
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id4bd7f7543f5b0f4f6f876e283bd065039c37646
Gerrit-Change-Number: 12787
Gerrit-PatchSet: 1
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190203/f1eacd6c/attachment.htm>


More information about the gerrit-log mailing list