diff mbox

[PATCHv3,3/9] linux-generic: classification: add strong typing support

Message ID 1422737449-11540-4-git-send-email-bill.fischofer@linaro.org
State New
Headers show

Commit Message

Bill Fischofer Jan. 31, 2015, 8:50 p.m. UTC
Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>
---
 include/odp/api/classification.h                   | 44 ++++++++++++++++---
 .../include/odp/plat/classification_types.h        | 34 ++++++++++++---
 platform/linux-generic/odp_classification.c        | 51 +++++++++++++---------
 .../classification/odp_classification_basic.c      |  2 +-
 4 files changed, 99 insertions(+), 32 deletions(-)
diff mbox

Patch

diff --git a/include/odp/api/classification.h b/include/odp/api/classification.h
index 5c6636f..55e4ba0 100644
--- a/include/odp/api/classification.h
+++ b/include/odp/api/classification.h
@@ -172,11 +172,6 @@  int odp_cos_with_l3_qos(odp_pktio_t pktio_in,
  */
 
 /**
- * Macro for Invalid PMR.
- */
-#define    ODP_PMR_INVAL ((odp_pmr_t)~0)
-
-/**
  * Packet Matching Rule field enumeration
  * for fields that may be used to calculate
  * the PMR, if present in a packet.
@@ -391,6 +386,45 @@  int odp_pktio_pmr_match_set_cos(odp_pmr_set_t pmr_set_id, odp_pktio_t src_pktio,
 				odp_cos_t dst_cos);
 
 /**
+ * Get printable value for an odp_cos_t
+ *
+ * @param hdl  odp_cos_t handle to be printed
+ * @return     uint64_t value that can be used to print/display this
+ *             handle
+ *
+ * @note This routine is intended to be used for diagnostic purposes
+ * to enable applications to generate a printable value that represents
+ * an odp_cos_t handle.
+ */
+uint64_t odp_cos_t_print(odp_cos_t hdl);
+
+/**
+ * Get printable value for an odp_pmr_t
+ *
+ * @param hdl  odp_pmr_t handle to be printed
+ * @return     uint64_t value that can be used to print/display this
+ *             handle
+ *
+ * @note This routine is intended to be used for diagnostic purposes
+ * to enable applications to generate a printable value that represents
+ * an odp_pmr_t handle.
+ */
+uint64_t odp_pmr_t_print(odp_pmr_t hdl);
+
+/**
+ * Get printable value for an odp_pmr_set_t
+ *
+ * @param hdl  odp_pmr_set_t handle to be printed
+ * @return     uint64_t value that can be used to print/display this
+ *             handle
+ *
+ * @note This routine is intended to be used for diagnostic purposes
+ * to enable applications to generate a printable value that represents
+ * an odp_pmr_set_t handle.
+ */
+uint64_t odp_pmr_set_t_print(odp_pmr_set_t hdl);
+
+/**
  * @}
  */
 
diff --git a/platform/linux-generic/include/odp/plat/classification_types.h b/platform/linux-generic/include/odp/plat/classification_types.h
index 07d640e..ed000db 100644
--- a/platform/linux-generic/include/odp/plat/classification_types.h
+++ b/platform/linux-generic/include/odp/plat/classification_types.h
@@ -17,19 +17,43 @@ 
 extern "C" {
 #endif
 
+#include <odp/plat/strong_types.h>
+
 /** @addtogroup odp_classification
  *  @{
  */
 
-typedef uint32_t odp_cos_t;
-typedef uint32_t odp_flowsig_t;
+typedef odp_handle_t odp_cos_t;
+typedef odp_handle_t odp_flowsig_t;
 
-#define ODP_COS_INVALID    ((odp_cos_t)~0)
+#define ODP_COS_INVALID  _odp_cast_scalar(odp_cos_t, ~0)
 #define ODP_COS_NAME_LEN 32
 
 typedef uint16_t odp_cos_flow_set_t;
-typedef uint32_t odp_pmr_t;
-typedef uint32_t odp_pmr_set_t;
+
+typedef odp_handle_t odp_pmr_t;
+#define ODP_PMR_INVAL _odp_cast_scalar(odp_pmr_t, ~0)
+
+typedef odp_handle_t odp_pmr_set_t;
+#define ODP_PMR_SET_INVAL _odp_cast_scalar(odp_pmr_set_t, ~0)
+
+/** Get printable format of odp_cos_t */
+static inline uint64_t odp_cos_t_print(odp_cos_t hdl)
+{
+	return _odp_pri(hdl);
+}
+
+/** Get printable format of odp_pmr_t */
+static inline uint64_t odp_pmr_t_print(odp_pmr_t hdl)
+{
+	return _odp_pri(hdl);
+}
+
+/** Get printable format of odp_pmr_set_t */
+static inline uint64_t odp_pmr_set_t_print(odp_pmr_set_t hdl)
+{
+	return _odp_pri(hdl);
+}
 
 /**
  * @}
diff --git a/platform/linux-generic/odp_classification.c b/platform/linux-generic/odp_classification.c
index 78597ef..9459586 100644
--- a/platform/linux-generic/odp_classification.c
+++ b/platform/linux-generic/odp_classification.c
@@ -32,17 +32,17 @@  static pmr_tbl_t	*pmr_tbl;
 
 cos_t *get_cos_entry_internal(odp_cos_t cos_id)
 {
-	return &(cos_tbl->cos_entry[cos_id]);
+	return &(cos_tbl->cos_entry[_odp_typeval(cos_id)]);
 }
 
 pmr_set_t *get_pmr_set_entry_internal(odp_pmr_set_t pmr_set_id)
 {
-	return &(pmr_set_tbl->pmr_set[pmr_set_id]);
+	return &(pmr_set_tbl->pmr_set[_odp_typeval(pmr_set_id)]);
 }
 
 pmr_t *get_pmr_entry_internal(odp_pmr_t pmr_id)
 {
-	return &(pmr_tbl->pmr[pmr_id]);
+	return &(pmr_tbl->pmr[_odp_typeval(pmr_id)]);
 }
 
 int odp_classification_init_global(void)
@@ -68,7 +68,8 @@  int odp_classification_init_global(void)
 	memset(cos_tbl, 0, sizeof(cos_tbl_t));
 	for (i = 0; i < ODP_COS_MAX_ENTRY; i++) {
 		/* init locks */
-		cos_t *cos = get_cos_entry_internal(i);
+		cos_t *cos =
+			get_cos_entry_internal(_odp_cast_scalar(odp_cos_t, i));
 		LOCK_INIT(&cos->s.lock);
 	}
 
@@ -88,7 +89,8 @@  int odp_classification_init_global(void)
 	memset(pmr_tbl, 0, sizeof(pmr_tbl_t));
 	for (i = 0; i < ODP_PMR_MAX_ENTRY; i++) {
 		/* init locks */
-		pmr_t *pmr = get_pmr_entry_internal(i);
+		pmr_t *pmr =
+			get_pmr_entry_internal(_odp_cast_scalar(odp_pmr_t, i));
 		LOCK_INIT(&pmr->s.lock);
 	}
 
@@ -108,7 +110,9 @@  int odp_classification_init_global(void)
 	memset(pmr_set_tbl, 0, sizeof(pmr_set_tbl_t));
 	for (i = 0; i < ODP_PMRSET_MAX_ENTRY; i++) {
 		/* init locks */
-		pmr_set_t *pmr = get_pmr_set_entry_internal(i);
+		pmr_set_t *pmr =
+			get_pmr_set_entry_internal
+			(_odp_cast_scalar(odp_pmr_set_t, i));
 		LOCK_INIT(&pmr->s.pmr.s.lock);
 	}
 
@@ -142,7 +146,7 @@  odp_cos_t odp_cos_create(const char *name)
 			cos_tbl->cos_entry[i].s.headroom = 0;
 			cos_tbl->cos_entry[i].s.valid = 1;
 			UNLOCK(&cos_tbl->cos_entry[i].s.lock);
-			return (odp_cos_t)i;
+			return _odp_cast_scalar(odp_cos_t, i);
 		}
 		UNLOCK(&cos_tbl->cos_entry[i].s.lock);
 	}
@@ -162,12 +166,13 @@  odp_pmr_set_t alloc_pmr_set(pmr_t **pmr)
 			*pmr = (pmr_t *)&pmr_set_tbl->pmr_set[i];
 			odp_atomic_init_u32(&pmr_set_tbl->pmr_set[i]
 					    .s.pmr.s.count, 0);
-			return (odp_pmr_set_t)i; /* return as locked */
+			/* return as locked */
+			return _odp_cast_scalar(odp_pmr_set_t, i);
 		}
 		UNLOCK(&pmr_set_tbl->pmr_set[i].s.pmr.s.lock);
 	}
 	ODP_ERR("ODP_PMRSET_MAX_ENTRY reached");
-	return ODP_PMR_INVAL;
+	return ODP_PMR_SET_INVAL;
 }
 
 odp_pmr_t alloc_pmr(pmr_t **pmr)
@@ -181,7 +186,8 @@  odp_pmr_t alloc_pmr(pmr_t **pmr)
 			odp_atomic_init_u32(&pmr_tbl->pmr[i].s.count, 0);
 			pmr_tbl->pmr[i].s.num_pmr = 0;
 			*pmr = &pmr_tbl->pmr[i];
-			return (odp_pmr_t)i; /* return as locked */
+			/* return as locked */
+			return _odp_cast_scalar(odp_pmr_t, i);
 		}
 		UNLOCK(&pmr_tbl->pmr[i].s.lock);
 	}
@@ -192,30 +198,33 @@  odp_pmr_t alloc_pmr(pmr_t **pmr)
 
 cos_t *get_cos_entry(odp_cos_t cos_id)
 {
-	if (cos_id >= ODP_COS_MAX_ENTRY || cos_id == ODP_COS_INVALID)
+	if (_odp_typeval(cos_id) >= ODP_COS_MAX_ENTRY ||
+	    cos_id == ODP_COS_INVALID)
 		return NULL;
-	if (cos_tbl->cos_entry[cos_id].s.valid == 0)
+	if (cos_tbl->cos_entry[_odp_typeval(cos_id)].s.valid == 0)
 		return NULL;
-	return &(cos_tbl->cos_entry[cos_id]);
+	return &(cos_tbl->cos_entry[_odp_typeval(cos_id)]);
 }
 
 
 pmr_set_t *get_pmr_set_entry(odp_pmr_set_t pmr_set_id)
 {
-	if (pmr_set_id >= ODP_PMRSET_MAX_ENTRY || pmr_set_id == ODP_PMR_INVAL)
+	if (_odp_typeval(pmr_set_id) >= ODP_PMRSET_MAX_ENTRY ||
+	    pmr_set_id == ODP_PMR_SET_INVAL)
 		return NULL;
-	if (pmr_set_tbl->pmr_set[pmr_set_id].s.pmr.s.valid == 0)
+	if (pmr_set_tbl->pmr_set[_odp_typeval(pmr_set_id)].s.pmr.s.valid == 0)
 		return NULL;
-	return &(pmr_set_tbl->pmr_set[pmr_set_id]);
+	return &(pmr_set_tbl->pmr_set[_odp_typeval(pmr_set_id)]);
 }
 
 pmr_t *get_pmr_entry(odp_pmr_t pmr_id)
 {
-	if (pmr_id >= ODP_PMR_MAX_ENTRY || pmr_id == ODP_PMR_INVAL)
+	if (_odp_typeval(pmr_id) >= ODP_PMR_MAX_ENTRY ||
+	    pmr_id == ODP_PMR_INVAL)
 		return NULL;
-	if (pmr_tbl->pmr[pmr_id].s.valid == 0)
+	if (pmr_tbl->pmr[_odp_typeval(pmr_id)].s.valid == 0)
 		return NULL;
-	return &(pmr_tbl->pmr[pmr_id]);
+	return &(pmr_tbl->pmr[_odp_typeval(pmr_id)]);
 }
 
 int odp_cos_destroy(odp_cos_t cos_id)
@@ -544,7 +553,7 @@  int odp_pmr_match_set_create(int num_terms, odp_pmr_match_t *terms,
 {
 	pmr_t *pmr;
 	int i;
-	uint32_t id;
+	odp_pmr_set_t id;
 	int val_sz;
 	int count = 0;
 
@@ -555,7 +564,7 @@  int odp_pmr_match_set_create(int num_terms, odp_pmr_match_t *terms,
 
 	id = alloc_pmr_set(&pmr);
 	/*if alloc_pmr_set is successful it returns with the acquired lock*/
-	if (id == ODP_PMR_INVAL) {
+	if (id == ODP_PMR_SET_INVAL) {
 		*pmr_set_id = id;
 		return -1;
 	}
diff --git a/test/validation/classification/odp_classification_basic.c b/test/validation/classification/odp_classification_basic.c
index 844b66e..ac2fe8e 100644
--- a/test/validation/classification/odp_classification_basic.c
+++ b/test/validation/classification/odp_classification_basic.c
@@ -146,7 +146,7 @@  static void classification_pmr_match_set_destroy(void)
 	uint16_t mask = 0xffff;
 	int i;
 
-	retval = odp_pmr_match_set_destroy(ODP_PMR_INVAL);
+	retval = odp_pmr_match_set_destroy(ODP_PMR_SET_INVAL);
 	CU_ASSERT(retval < 0);
 
 	for (i = 0; i < PMR_SET_NUM; i++) {