[PATCH] libosmocore[master]: Add and use macros to set/get/clear/toggle bit flags

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/.

Max gerrit-no-reply at lists.osmocom.org
Mon Jun 19 13:37:49 UTC 2017


Hello Vadim Yanitskiy, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/2857

to look at the new patch set (#3).

Add and use macros to set/get/clear/toggle bit flags

We extensively use bit masks and flags but do not have any convenience
wrappers for it which decrease code readability. Add macros which can
set/get, clear and toggle bits with corresponding doxygen annotations
and use them throughout the code to make sure they are covered by
existing tests.

Change-Id: Ie9d38b837ce84649c2975dbe8b889fe3a769885f
---
M include/osmocom/core/utils.h
M src/bits.c
M src/fsm.c
M src/gb/gprs_bssgp_vty.c
M src/gb/gprs_ns_vty.c
5 files changed, 19 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/57/2857/3

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 63a73ab..3b1838e 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -20,6 +20,15 @@
 /*! \brief Make a value_string entry from an enum value name */
 #define OSMO_VALUE_STRING(x) { x, #x }
 
+/*! \brief Set flag bit in x */
+#define OSMO_BIT_SET(x, flag) ((x) |= 1 << (flag))
+/*! \brief Get flag bit in x */
+#define OSMO_BIT_GET(x, flag) ((x) & (1 << (flag)))
+/*! \brief Clear flag bit in x */
+#define OSMO_BIT_CLEAR(x, flag) ((x) &= ~(1 << (flag)))
+/*! \brief Toggle flag bit in x */
+#define OSMO_BIT_TOGGLE(x, flag) ((x) ^= (1 << (flag)))
+
 #include <stdint.h>
 #include <stdio.h>
 
diff --git a/src/bits.c b/src/bits.c
index 0c77b27..37c98f0 100644
--- a/src/bits.c
+++ b/src/bits.c
@@ -23,6 +23,7 @@
 #include <stdint.h>
 
 #include <osmocom/core/bits.h>
+#include <osmocom/core/utils.h>
 
 /*! \addtogroup bits
  *  @{
@@ -193,9 +194,9 @@
 		op = out_ofs + i;
 		bn = lsb_mode ? (op&7) : (7-(op&7));
 		if (in[in_ofs+i])
-			out[op>>3] |= 1 << bn;
+			OSMO_BIT_SET(out[op>>3], bn);
 		else
-			out[op>>3] &= ~(1 << bn);
+			OSMO_BIT_CLEAR(out[op>>3], bn);
 	}
 	return ((out_ofs + num_bits - 1) >> 3) + 1;
 }
diff --git a/src/fsm.c b/src/fsm.c
index 5e74482..333f9e9 100644
--- a/src/fsm.c
+++ b/src/fsm.c
@@ -355,7 +355,7 @@
 	const struct osmo_fsm_state *st = &fsm->states[fi->state];
 
 	/* validate if new_state is a valid state */
-	if (!(st->out_state_mask & (1 << new_state))) {
+	if (!OSMO_BIT_GET(st->out_state_mask, new_state)) {
 		LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
 			    "transition to state %s not permitted!\n",
 			    osmo_fsm_state_name(fsm, new_state));
@@ -423,12 +423,12 @@
 	LOGPFSMSRC(fi, file, line,
 		   "Received Event %s\n", osmo_fsm_event_name(fsm, event));
 
-	if (((1 << event) & fsm->allstate_event_mask) && fsm->allstate_action) {
+	if (OSMO_BIT_GET(fsm->allstate_event_mask, event) && fsm->allstate_action) {
 		fsm->allstate_action(fi, event, data);
 		return 0;
 	}
 
-	if (!((1 << event) & fs->in_event_mask)) {
+	if (!OSMO_BIT_GET(fs->in_event_mask, event)) {
 		LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
 			    "Event %s not permitted\n",
 			    osmo_fsm_event_name(fsm, event));
diff --git a/src/gb/gprs_bssgp_vty.c b/src/gb/gprs_bssgp_vty.c
index f3f354c..7501b00 100644
--- a/src/gb/gprs_bssgp_vty.c
+++ b/src/gb/gprs_bssgp_vty.c
@@ -34,7 +34,7 @@
 #include <osmocom/gprs/gprs_ns.h>
 #include <osmocom/gprs/gprs_bssgp.h>
 #include <osmocom/gprs/gprs_bssgp_bss.h>
-
+#include <osmocom/core/utils.h>
 #include <osmocom/vty/vty.h>
 #include <osmocom/vty/command.h>
 #include <osmocom/vty/logging.h>
@@ -47,7 +47,7 @@
 				struct bssgp_bvc_ctx *bctx)
 {
 	if (bctx) {
-		target->filter_map |= (1 << LOG_FLT_GB_BVC);
+		OSMO_BIT_SET(target->filter_map, LOG_FLT_GB_BVC);
 		target->filter_data[LOG_FLT_GB_BVC] = bctx;
 	} else if (target->filter_data[LOG_FLT_GB_BVC]) {
 		target->filter_map = ~(1 << LOG_FLT_GB_BVC);
diff --git a/src/gb/gprs_ns_vty.c b/src/gb/gprs_ns_vty.c
index bdccb3f..e8d7a1b 100644
--- a/src/gb/gprs_ns_vty.c
+++ b/src/gb/gprs_ns_vty.c
@@ -34,7 +34,7 @@
 #include <osmocom/core/rate_ctr.h>
 #include <osmocom/gprs/gprs_ns.h>
 #include <osmocom/gprs/gprs_bssgp.h>
-
+#include <osmocom/core/utils.h>
 #include <osmocom/vty/vty.h>
 #include <osmocom/vty/command.h>
 #include <osmocom/vty/logging.h>
@@ -62,7 +62,7 @@
 				struct gprs_nsvc *nsvc)
 {
 	if (nsvc) {
-		target->filter_map |= (1 << LOG_FLT_GB_NSVC);
+		OSMO_BIT_SET(target->filter_map, LOG_FLT_GB_NSVC);
 		target->filter_data[LOG_FLT_GB_NSVC] = nsvc;
 	} else if (target->filter_data[LOG_FLT_GB_NSVC]) {
 		target->filter_map = ~(1 << LOG_FLT_GB_NSVC);

-- 
To view, visit https://gerrit.osmocom.org/2857
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Ie9d38b837ce84649c2975dbe8b889fe3a769885f
Gerrit-PatchSet: 3
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: fixeria <axilirator at gmail.com>



More information about the gerrit-log mailing list