[PATCHv2 3/8] shared/utils: Add helpers for bitfield fast uid generation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Andrei Emeltchenko <andrei.emeltchenko@xxxxxxxxx>

For small unique id generation we may use bitfield since it is very fast
and memory efficient. If we need to generate id from 1 to 64 helper
functions are added to utils.
---
 src/shared/util.c | 24 ++++++++++++++++++++++++
 src/shared/util.h |  3 +++
 2 files changed, 27 insertions(+)

diff --git a/src/shared/util.c b/src/shared/util.c
index 74ffb08..484031f 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -106,3 +106,27 @@ unsigned char util_get_dt(const char *parent, const char *name)
 
 	return DT_UNKNOWN;
 }
+
+/* Helpers for bitfield operations */
+/* Find unique id from 1 to max */
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max)
+{
+	uint64_t temp = *bitmap;
+	uint64_t id = 1;
+
+	while (temp >>= 1)
+		id++;
+
+	if (id > max)
+		return 0;
+
+	*bitmap |= 0x01l << id;
+
+	return id;
+}
+
+/* Clear id bit in bitmap */
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id)
+{
+	*bitmap &= ~(0x01l << id);
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 8437662..8f28c24 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -93,6 +93,9 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
 
 unsigned char util_get_dt(const char *parent, const char *name);
 
+uint8_t util_get_bitmap64_uid(uint64_t *bitmap, uint8_t max);
+void util_clear_bitmap64(uint64_t *bitmap, uint8_t id);
+
 static inline void bswap_128(const void *src, void *dst)
 {
 	const uint8_t *s = src;
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux