Skip to contentSkip to conversation

staging: comedi: cleanup comedi_recognize()

PATCH In review Dan Carpenter 3 participants 12 messages May 24, 2012 – Jun 6, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: comedi: cleanup comedi_recognize()

This function is more complicated than it needs to be because of the consts. It's not worth saving them because we drop the consts anyway when we return (void *)name_ptr.

drivers/staging/comedi/drivers.c +5 -5
index 1c3d638..a8f576d 100644
@@ -304,14 +304,13 @@ static int postconfig(struct comedi_device *dev)
  * that register their supported board names */
 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
 {
-	unsigned i;
-	const char *const *name_ptr = driv->board_name;
+	char **name_ptr = (char **)driv->board_name;
+	int i;
+
 	for (i = 0; i < driv->num_names; i++) {
 		if (strcmp(*name_ptr, name) = 0)
-			return (void *)name_ptr;
-		name_ptr -		    (const char *const *)((const char *)name_ptr +
-					  driv->offset);
+			return name_ptr;
+		name_ptr = (char **)((void *)name_ptr + driv->offset);
 	}
 
 	return NULL;
#1 lore raw
Quoted 19 lines · Dan Carpenter

This function is more complicated than it needs to be because of the consts. It's not worth saving them because we drop the consts anyway when we return (void *)name_ptr.

drivers/staging/comedi/drivers.c +3 -2
index 1c3d638..a8f576d 100644
@@ -304,14 +304,13 @@ static int postconfig(struct comedi_device *dev)
   * that register their supported board names */
  static void *comedi_recognize(struct comedi_driver *driv, const char *name)
  {
-	unsigned i;
-	const char *const *name_ptr = driv->board_name;
+	char **name_ptr = (char **)driv->board_name;
+	int i;
+

drivers/staging/comedi/drivers.c static int postconfig(struct comedi_device *dev) · line 316

Quoted 10 lines
drivers/staging/comedi/drivers.c +2 -3
  	for (i = 0; i<  driv->num_names; i++) {
  		if (strcmp(*name_ptr, name) = 0)
-			return (void *)name_ptr;
-		name_ptr > -		    (const char *const *)((const char *)name_ptr +
-					  driv->offset);
+			return name_ptr;
+		name_ptr = (char **)((void *)name_ptr + driv->offset);
  	}
 
  	return NULL;

You could simplify it further by keeping one of the consts:

static void *comedi_recognize(struct comedi_driver *driv, const char *name)

{
	unsigned i;
	const char **name_ptr = driv->board_name;
	for (i = 0; i < driv->num_names; i++) {
		if (strcmp(*name_ptr, name) = 0)
			return name_ptr;
		name_ptr = (void *)name_ptr + driv->offset;
	}
	return NULL;
}
Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#2 lore raw
In reply to Dan Carpenter
Quoted 34 lines · Ian Abbott
Quoted 19 lines · Dan Carpenter

This function is more complicated than it needs to be because of the consts. It's not worth saving them because we drop the consts anyway when we return (void *)name_ptr.

drivers/staging/comedi/drivers.c +3 -2
index 1c3d638..a8f576d 100644
@@ -304,14 +304,13 @@ static int postconfig(struct comedi_device *dev)
  * that register their supported board names */
 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
 {
-	unsigned i;
-	const char *const *name_ptr = driv->board_name;
+	char **name_ptr = (char **)driv->board_name;
+	int i;
+

drivers/staging/comedi/drivers.c static int postconfig(struct comedi_device *dev) · line 316

Quoted 10 lines
drivers/staging/comedi/drivers.c +2 -3
 	for (i = 0; i<  driv->num_names; i++) {
 		if (strcmp(*name_ptr, name) = 0)
-			return (void *)name_ptr;
-		name_ptr > >-		    (const char *const *)((const char *)name_ptr +
-					  driv->offset);
+			return name_ptr;
+		name_ptr = (char **)((void *)name_ptr + driv->offset);
 	}
 
 	return NULL;

You could simplify it further by keeping one of the consts:

static void *comedi_recognize(struct comedi_driver *driv, const char *name)

Quoted 10 lines
{
	unsigned i;
	const char **name_ptr = driv->board_name;
	for (i = 0; i < driv->num_names; i++) {
		if (strcmp(*name_ptr, name) = 0)
			return name_ptr;
		name_ptr = (void *)name_ptr + driv->offset;
	}
	return NULL;
}

Sure. I'll resend.

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 46 lines · Dan Carpenter
Quoted 34 lines · Ian Abbott
Quoted 19 lines · Dan Carpenter

This function is more complicated than it needs to be because of the consts. It's not worth saving them because we drop the consts anyway when we return (void *)name_ptr.

drivers/staging/comedi/drivers.c +3 -2
index 1c3d638..a8f576d 100644
@@ -304,14 +304,13 @@ static int postconfig(struct comedi_device *dev)
   * that register their supported board names */
  static void *comedi_recognize(struct comedi_driver *driv, const char *name)
  {
-	unsigned i;
-	const char *const *name_ptr = driv->board_name;
+	char **name_ptr = (char **)driv->board_name;
+	int i;
+

drivers/staging/comedi/drivers.c static int postconfig(struct comedi_device *dev) · line 316

Quoted 10 lines
drivers/staging/comedi/drivers.c +2 -3
  	for (i = 0; i<   driv->num_names; i++) {
  		if (strcmp(*name_ptr, name) = 0)
-			return (void *)name_ptr;
-		name_ptr >>> -		    (const char *const *)((const char *)name_ptr +
-					  driv->offset);
+			return name_ptr;
+		name_ptr = (char **)((void *)name_ptr + driv->offset);
  	}
 
  	return NULL;

You could simplify it further by keeping one of the consts:

static void *comedi_recognize(struct comedi_driver *driv, const char *name)

Quoted 10 lines
{
	unsigned i;
	const char **name_ptr = driv->board_name;
	for (i = 0; i<  driv->num_names; i++) {
		if (strcmp(*name_ptr, name) = 0)
			return name_ptr;
		name_ptr = (void *)name_ptr + driv->offset;
	}
	return NULL;
}
Quoted 1 line

Sure. I'll resend.

Actually, my version gives a warning about a dropped qualifier, so the initializer of name_ptr would need a cast after all. You could keep your version but remove the casts from (void *) when converting to the other pointer types.

static void *comedi_recognize(struct comedi_driver *driv, const char *name)

{
	unsigned i;
	char **name_ptr = (char **)driv->board_name;
	for (i = 0; i < driv->num_names; i++) {
		if (strcmp(*name_ptr, name) = 0)
			return name_ptr;
		name_ptr = (void *)name_ptr + driv->offset;
	}

	return NULL;
}
Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#4 lore raw
In reply to Dan Carpenter

Am 24.05.2012 12:57, schrieb Ian Abbott:

Quoted 36 lines · Dan Carpenter
Quoted 21 lines

This function is more complicated than it needs to be because of the consts. It's not worth saving them because we drop the consts anyway when we return (void *)name_ptr.

drivers/staging/comedi/drivers.c +3 -2
index 1c3d638..a8f576d 100644
@@ -304,14 +304,13 @@ static int postconfig(struct comedi_device *dev)
   * that register their supported board names */
  static void *comedi_recognize(struct comedi_driver *driv, const char
 *name)
  {
-    unsigned i;
-    const char *const *name_ptr = driv->board_name;
+    char **name_ptr = (char **)driv->board_name;
+    int i;
+

drivers/staging/comedi/drivers.c static int postconfig(struct comedi_device *dev) · line 316

Quoted 10 lines
drivers/staging/comedi/drivers.c +2 -3
      for (i = 0; i<  driv->num_names; i++) {
          if (strcmp(*name_ptr, name) = 0)
-            return (void *)name_ptr;
-        name_ptr >> -            (const char *const *)((const char *)name_ptr +
-                      driv->offset);
+            return name_ptr;
+        name_ptr = (char **)((void *)name_ptr + driv->offset);
      }
 
      return NULL;

You could simplify it further by keeping one of the consts:

static void *comedi_recognize(struct comedi_driver *driv, const char *name)

Quoted 11 lines
{
    unsigned i;
    const char **name_ptr = driv->board_name;
    for (i = 0; i < driv->num_names; i++) {
        if (strcmp(*name_ptr, name) = 0)
            return name_ptr;
        name_ptr = (void *)name_ptr + driv->offset;
    }
    return NULL;
}

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

A look at the structure also shows that this is a linked list, why using offset instead of driv->next ?

re,
wh

#5 lore raw
In reply to Dan Carpenter
Quoted 5 lines · walter harms

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

Quoted 3 lines

A look at the structure also shows that this is a linked list, why using offset instead of driv->next ?

It's a different list.

regards,
dan carpenter

#6 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter
Quoted 5 lines · walter harms

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#7 lore raw
In reply to Dan Carpenter

Am 06.06.2012 11:28, schrieb Ian Abbott:

Quoted 8 lines · Dan Carpenter
Quoted 7 lines · walter harms
Quoted 5 lines

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

Quoted 9 lines
Quoted 2 lines

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

I do not like it. It is confusing.
Are these struct comedi_driver *driv of different size every time ? or why is it not possible to return simply driv[i] ?

re,
wh

#8 lore raw
In reply to Dan Carpenter
Quoted 14 lines · walter harms

Am 06.06.2012 11:28, schrieb Ian Abbott:

Quoted 11 lines · Dan Carpenter
Quoted 9 lines · walter harms
Quoted 5 lines

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

Quoted 8 lines
Quoted 6 lines

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

I do not like it. It is confusing.

Yep.

Quoted 1 line

Are these struct comedi_driver *driv of different size every time ?

Nope.

regards,
dan carpenter

#9 lore raw
In reply to Dan Carpenter
Quoted 22 lines · Dan Carpenter
Quoted 21 lines · walter harms

Am 06.06.2012 11:28, schrieb Ian Abbott:

Quoted 17 lines · Dan Carpenter
Quoted 9 lines · walter harms
Quoted 5 lines

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

Quoted 3 lines
Quoted 1 line

I do not like it. It is confusing.

Yep.

But at least the confusion is concentrated in a single place! From the individual drivers' point of view it's fairly simple (as long as it puts the 'const char pointer to board name' member at the top of its private data structure).

Quoted 3 lines
Quoted 1 line

Are these struct comedi_driver *driv of different size every time ?

Nope.

It's the private board information data structures for each driver that are different sizes, but the size is passed in the comedi comedi_driver 'offset' member along with the 'board_name' and 'num_names' members. Not all drivers set those members and just use the 'driver_name' to match any supported device, but I digress.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#10 lore raw
In reply to Dan Carpenter

Am 06.06.2012 13:20, schrieb Ian Abbott:

Quoted 23 lines · Dan Carpenter
Quoted 22 lines · walter harms
Quoted 21 lines

Am 06.06.2012 11:28, schrieb Ian Abbott:

Quoted 17 lines · Dan Carpenter
Quoted 9 lines · walter harms
Quoted 5 lines

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

I do not like it. It is confusing.

Yep.

But at least the confusion is concentrated in a single place! From the individual drivers' point of view it's fairly simple (as long as it puts the 'const char pointer to board name' member at the top of its private data structure).

Sometimes you can not write "clean" code but a few lines of comments are then a must.

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

Are these struct comedi_driver *driv of different size every time ?

Nope.

It's the private board information data structures for each driver that are different sizes, but the size is passed in the comedi comedi_driver 'offset' member along with the 'board_name' and 'num_names' members. Not all drivers set those members and just use the 'driver_name' to match any supported device, but I digress.

If i can believe the comments in the structure offset is the distance to the next name /* offset in bytes from one board name pointer to the next */ and the name is not the first entry it is the second.
That means any program assuming that this code returns a (struct comedi_driver *) has to do some correction or it will be off-bye-one sizeof(struct comedi_driver *). Please correct me if i am wrong:

I have no clue how dev->board_ptr is used, if it works it is fine with me.

re,
wh

#11 lore raw
In reply to Dan Carpenter
Quoted 48 lines · walter harms

Am 06.06.2012 13:20, schrieb Ian Abbott:

Quoted 23 lines · Dan Carpenter
Quoted 22 lines · walter harms
Quoted 21 lines

Am 06.06.2012 11:28, schrieb Ian Abbott:

Quoted 17 lines · Dan Carpenter
Quoted 9 lines · walter harms
Quoted 5 lines

Hi all,
the patch is fine with me but i have a few basic questions:

Why the (void *) at all ? it returns a name what is a const char *.

We're really returning a pointer to a private struct, it's just that the first element on the struct always has to be a pointer to char *.

To be pedantic, it's really returning a pointer to some member of type 'const char *' within a private struct. To make life easier for themselves, those drivers make that the first member of the private struct so a void pointer to the member is also a void pointer to the struct.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

I do not like it. It is confusing.

Yep.

But at least the confusion is concentrated in a single place! From the individual drivers' point of view it's fairly simple (as long as it puts the 'const char pointer to board name' member at the top of its private data structure).

Sometimes you can not write "clean" code but a few lines of comments are then a must.

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

Are these struct comedi_driver *driv of different size every time ?

Nope.

It's the private board information data structures for each driver that are different sizes, but the size is passed in the comedi comedi_driver 'offset' member along with the 'board_name' and 'num_names' members. Not all drivers set those members and just use the 'driver_name' to match any supported device, but I digress.

Quoted 3 lines

If i can believe the comments in the structure offset is the distance to the next name /* offset in bytes from one board name pointer to the next */ and the name is not the first entry it is the second.

This board name pointer is in a driver-specific, board information struct. All of them make it the first member of the driver-specific struct for convenience, although they don't need to - they could use offsetof() or container_of() to convert the pointer to the member to a pointer to the containing struct. The comedi core does not care about the driver-specific struct but needs to be told how to access the board names in an initialized array of these structs.

Quoted 5 lines

That means any program assuming that this code returns a (struct comedi_driver *) has to do some correction or it will be off-bye-one sizeof(struct comedi_driver *). Please correct me if i am wrong:

I have no clue how dev->board_ptr is used, if it works it is fine with me.

If the low-level driver initializes 'board_name', 'offset' and 'num_names' to something non-zero in its 'struct comedi_driver' then 'dev->board_ptr' will point to one of the board name pointers in initialized array of driver-specific, board information structs when the driver's attach() hook is called. If 'num_names' is 0, 'dev->board_ptr' will be NULL when the attach() hook is called. In either case, the low-level driver is free to change the value of 'dev->board_ptr' for its own purposes, although generally it will be pointed to some element of its array of board information structs.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-