[ofa-general] [PATCH] libibmad: Use enum types for function parameters (WAS) Declare some enums as typedefs for cleaner function interfaces

Ira Weiny weiny2 at llnl.gov
Thu Feb 5 10:03:31 PST 2009


Sasha,

On Wed, 4 Feb 2009 10:30:54 -0800
Ira Weiny <weiny2 at llnl.gov> wrote:

> On Wed, 4 Feb 2009 20:27:25 +0200
> Sasha Khapyorsky <sashak at voltaire.com> wrote:
> 
> > On 11:20 Wed 04 Feb     , Jason Gunthorpe wrote:
> > > On Wed, Feb 04, 2009 at 08:14:21PM +0200, Sasha Khapyorsky wrote:
> > > 
> > > > I don't understand how enum typedefing makes things cleaner - actually
> > > > this will enforce me explicitly to verify an actual type in header
> > > > files. Sometimes typedefs could help with porting, but it is not the
> > > > case here.
> > > 
> > > Not typedefing per say, but passing an enum through an int is not that
> > > great. You don't need the typedefs to do this, just 'enum MAD_FIELDS'
> > > for instance will do.
> > 
> > Yes, that would be fine to do.
> 
> I will redo the patch with 'enum MAD_FIELDS'.
> 

Patch below,
Ira

>From 3a52d32d7c6964a8078402c3712a58d1e43975de Mon Sep 17 00:00:00 2001
From: weiny2 at llnl.gov <weiny2 at llnl.gov>
Date: Mon, 2 Feb 2009 10:21:18 -0800
Subject: [PATCH] Use enum types for function parameters


Signed-off-by: weiny2 at llnl.gov <weiny2 at llnl.gov>
---
 libibmad/include/infiniband/mad.h |   30 +++++++++++++++---------------
 libibmad/src/fields.c             |   22 +++++++++++-----------
 libibmad/src/resolve.c            |    6 +++---
 3 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/libibmad/include/infiniband/mad.h b/libibmad/include/infiniband/mad.h
index 9ff4a3e..33a233c 100644
--- a/libibmad/include/infiniband/mad.h
+++ b/libibmad/include/infiniband/mad.h
@@ -595,14 +595,14 @@ typedef struct ib_vendor_call {
 #define MAD_DEF_RETRIES		3
 #define MAD_DEF_TIMEOUT_MS	1000
 
-enum {
+enum MAD_DEST {
 	IB_DEST_LID,
 	IB_DEST_DRPATH,
 	IB_DEST_GUID,
 	IB_DEST_DRSLID,
 };
 
-enum {
+enum MAD_NODE_TYPE {
 	IB_NODE_CA = 1,
 	IB_NODE_SWITCH,
 	IB_NODE_ROUTER,
@@ -631,20 +631,20 @@ static inline int ib_portid_set(ib_portid_t * portid, int lid, int qp, int qkey)
 }
 
 /* fields.c */
-MAD_EXPORT uint32_t mad_get_field(void *buf, int base_offs, int field);
-MAD_EXPORT void mad_set_field(void *buf, int base_offs, int field,
+MAD_EXPORT uint32_t mad_get_field(void *buf, int base_offs, enum MAD_FIELDS field);
+MAD_EXPORT void mad_set_field(void *buf, int base_offs, enum MAD_FIELDS field,
 			      uint32_t val);
 /* field must be byte aligned */
-MAD_EXPORT uint64_t mad_get_field64(void *buf, int base_offs, int field);
-MAD_EXPORT void mad_set_field64(void *buf, int base_offs, int field,
+MAD_EXPORT uint64_t mad_get_field64(void *buf, int base_offs, enum MAD_FIELDS field);
+MAD_EXPORT void mad_set_field64(void *buf, int base_offs, enum MAD_FIELDS field,
 				uint64_t val);
-MAD_EXPORT void mad_set_array(void *buf, int base_offs, int field, void *val);
-MAD_EXPORT void mad_get_array(void *buf, int base_offs, int field, void *val);
-MAD_EXPORT void mad_decode_field(uint8_t * buf, int field, void *val);
-MAD_EXPORT void mad_encode_field(uint8_t * buf, int field, void *val);
-MAD_EXPORT int mad_print_field(int field, const char *name, void *val);
-MAD_EXPORT char *mad_dump_field(int field, char *buf, int bufsz, void *val);
-MAD_EXPORT char *mad_dump_val(int field, char *buf, int bufsz, void *val);
+MAD_EXPORT void mad_set_array(void *buf, int base_offs, enum MAD_FIELDS field, void *val);
+MAD_EXPORT void mad_get_array(void *buf, int base_offs, enum MAD_FIELDS field, void *val);
+MAD_EXPORT void mad_decode_field(uint8_t * buf, enum MAD_FIELDS field, void *val);
+MAD_EXPORT void mad_encode_field(uint8_t * buf, enum MAD_FIELDS field, void *val);
+MAD_EXPORT int mad_print_field(enum MAD_FIELDS field, const char *name, void *val);
+MAD_EXPORT char *mad_dump_field(enum MAD_FIELDS field, char *buf, int bufsz, void *val);
+MAD_EXPORT char *mad_dump_val(enum MAD_FIELDS field, char *buf, int bufsz, void *val);
 
 /* mad.c */
 MAD_EXPORT void *mad_encode(void *buf, ib_rpc_t * rpc, ib_dr_path_t * drpath,
@@ -729,7 +729,7 @@ MAD_EXPORT int ib_resolve_smlid(ib_portid_t * sm_id, int timeout);
 MAD_EXPORT int ib_resolve_guid(ib_portid_t * portid, uint64_t * guid,
 			       ib_portid_t * sm_id, int timeout);
 MAD_EXPORT int ib_resolve_portid_str(ib_portid_t * portid, char *addr_str,
-				     int dest_type, ib_portid_t * sm_id);
+				     enum MAD_DEST dest, ib_portid_t * sm_id);
 MAD_EXPORT int ib_resolve_self(ib_portid_t * portid, int *portnum,
 			       ibmad_gid_t * gid);
 
@@ -737,7 +737,7 @@ int ib_resolve_smlid_via(ib_portid_t * sm_id, int timeout, const void *srcport);
 int ib_resolve_guid_via(ib_portid_t * portid, uint64_t * guid,
 			ib_portid_t * sm_id, int timeout, const void *srcport);
 int ib_resolve_portid_str_via(ib_portid_t * portid, char *addr_str,
-			      int dest_type, ib_portid_t * sm_id,
+			      enum MAD_DEST dest, ib_portid_t * sm_id,
 			      const void *srcport);
 int ib_resolve_self_via(ib_portid_t * portid, int *portnum, ibmad_gid_t * gid,
 			const void *srcport);
diff --git a/libibmad/src/fields.c b/libibmad/src/fields.c
index d5a1eb4..588c57f 100644
--- a/libibmad/src/fields.c
+++ b/libibmad/src/fields.c
@@ -479,37 +479,37 @@ static void _get_array(void *buf, int base_offs, const ib_field_t * f,
 	memcpy(val, (uint8_t *) buf + base_offs + bitoffs / 8, f->bitlen / 8);
 }
 
-uint32_t mad_get_field(void *buf, int base_offs, int field)
+uint32_t mad_get_field(void *buf, int base_offs, enum MAD_FIELDS field)
 {
 	return _get_field(buf, base_offs, ib_mad_f + field);
 }
 
-void mad_set_field(void *buf, int base_offs, int field, uint32_t val)
+void mad_set_field(void *buf, int base_offs, enum MAD_FIELDS field, uint32_t val)
 {
 	_set_field(buf, base_offs, ib_mad_f + field, val);
 }
 
-uint64_t mad_get_field64(void *buf, int base_offs, int field)
+uint64_t mad_get_field64(void *buf, int base_offs, enum MAD_FIELDS field)
 {
 	return _get_field64(buf, base_offs, ib_mad_f + field);
 }
 
-void mad_set_field64(void *buf, int base_offs, int field, uint64_t val)
+void mad_set_field64(void *buf, int base_offs, enum MAD_FIELDS field, uint64_t val)
 {
 	_set_field64(buf, base_offs, ib_mad_f + field, val);
 }
 
-void mad_set_array(void *buf, int base_offs, int field, void *val)
+void mad_set_array(void *buf, int base_offs, enum MAD_FIELDS field, void *val)
 {
 	_set_array(buf, base_offs, ib_mad_f + field, val);
 }
 
-void mad_get_array(void *buf, int base_offs, int field, void *val)
+void mad_get_array(void *buf, int base_offs, enum MAD_FIELDS field, void *val)
 {
 	_get_array(buf, base_offs, ib_mad_f + field, val);
 }
 
-void mad_decode_field(uint8_t * buf, int field, void *val)
+void mad_decode_field(uint8_t * buf, enum MAD_FIELDS field, void *val)
 {
 	const ib_field_t *f = ib_mad_f + field;
 
@@ -528,7 +528,7 @@ void mad_decode_field(uint8_t * buf, int field, void *val)
 	_get_array(buf, 0, f, val);
 }
 
-void mad_encode_field(uint8_t * buf, int field, void *val)
+void mad_encode_field(uint8_t * buf, enum MAD_FIELDS field, void *val)
 {
 	const ib_field_t *f = ib_mad_f + field;
 
@@ -602,21 +602,21 @@ static int _mad_print_field(const ib_field_t * f, const char *name, void *val,
 			 valsz ? valsz : ALIGN(f->bitlen, 8) / 8);
 }
 
-int mad_print_field(int field, const char *name, void *val)
+int mad_print_field(enum MAD_FIELDS field, const char *name, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
 		return -1;
 	return _mad_print_field(ib_mad_f + field, name, val, 0);
 }
 
-char *mad_dump_field(int field, char *buf, int bufsz, void *val)
+char *mad_dump_field(enum MAD_FIELDS field, char *buf, int bufsz, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
 		return 0;
 	return _mad_dump_field(ib_mad_f + field, 0, buf, bufsz, val);
 }
 
-char *mad_dump_val(int field, char *buf, int bufsz, void *val)
+char *mad_dump_val(enum MAD_FIELDS field, char *buf, int bufsz, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
 		return 0;
diff --git a/libibmad/src/resolve.c b/libibmad/src/resolve.c
index b62360b..553949d 100644
--- a/libibmad/src/resolve.c
+++ b/libibmad/src/resolve.c
@@ -92,7 +92,7 @@ int ib_resolve_guid_via(ib_portid_t * portid, uint64_t * guid,
 }
 
 int ib_resolve_portid_str_via(ib_portid_t * portid, char *addr_str,
-			      int dest_type, ib_portid_t * sm_id,
+			      enum MAD_DEST dest_type, ib_portid_t * sm_id,
 			      const void *srcport)
 {
 	uint64_t guid;
@@ -142,8 +142,8 @@ int ib_resolve_portid_str_via(ib_portid_t * portid, char *addr_str,
 	return -1;
 }
 
-int ib_resolve_portid_str(ib_portid_t * portid, char *addr_str, int dest_type,
-			  ib_portid_t * sm_id)
+int ib_resolve_portid_str(ib_portid_t * portid, char *addr_str,
+			enum MAD_DEST dest_type, ib_portid_t * sm_id)
 {
 	return ib_resolve_portid_str_via(portid, addr_str, dest_type,
 					 sm_id, NULL);
-- 
1.5.4.5




More information about the general mailing list