diff mbox series

[1/1] usb: xhci: avoid type conversion of void *

Message ID 20200929200301.66459-1-xypron.glpk@gmx.de
State Accepted
Commit 3fade88686e71c9acee4cbeb3ae9706bbc845608
Delegated to: Marek Vasut
Headers show
Series [1/1] usb: xhci: avoid type conversion of void * | expand

Commit Message

Heinrich Schuchardt Sept. 29, 2020, 8:03 p.m. UTC
void * can be assigned to any pointer variable. Avoid unnecessary
conversions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 drivers/usb/host/xhci-mem.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

--
2.28.0

Comments

Marek Vasut Sept. 29, 2020, 8:20 p.m. UTC | #1
On 9/29/20 10:03 PM, Heinrich Schuchardt wrote:
> void * can be assigned to any pointer variable. Avoid unnecessary
> conversions.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  drivers/usb/host/xhci-mem.c | 21 +++++++++------------
>  1 file changed, 9 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
> index 1da0524aa0..4c303ae705 100644
> --- a/drivers/usb/host/xhci-mem.c
> +++ b/drivers/usb/host/xhci-mem.c
> @@ -280,10 +280,10 @@ static struct xhci_segment *xhci_segment_alloc(void)
>  {
>  	struct xhci_segment *seg;
> 
> -	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
> +	seg = malloc(sizeof(struct xhci_segment));

As far as I remember, the compiler used to complain about assignment
from a different type without a cast ?

btw. you might also do a patch which does malloc(sizeof(*seg)); while at it.
Heinrich Schuchardt Sept. 30, 2020, 11:43 p.m. UTC | #2
On 9/29/20 10:20 PM, Marek Vasut wrote:
> On 9/29/20 10:03 PM, Heinrich Schuchardt wrote:
>> void * can be assigned to any pointer variable. Avoid unnecessary
>> conversions.
>>
>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>> ---
>>  drivers/usb/host/xhci-mem.c | 21 +++++++++------------
>>  1 file changed, 9 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
>> index 1da0524aa0..4c303ae705 100644
>> --- a/drivers/usb/host/xhci-mem.c
>> +++ b/drivers/usb/host/xhci-mem.c
>> @@ -280,10 +280,10 @@ static struct xhci_segment *xhci_segment_alloc(void)
>>  {
>>  	struct xhci_segment *seg;
>>
>> -	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
>> +	seg = malloc(sizeof(struct xhci_segment));
>
> As far as I remember, the compiler used to complain about assignment
> from a different type without a cast ?

Under the ANSI C standard, the cast is redundant.

void * is an incomplete type. You can assign it to any pointer and any
pointer can be assigned to it, both without casting.

For other data types you are right. GCC will not accept an assignment
without conversion.

In C++ you need to cast to assign void * to other types.

> btw. you might also do a patch which does malloc(sizeof(*seg)); while at it.

Sure if you prefer that I will respin the patch.

Best regards

Heinrich
Marek Vasut Oct. 1, 2020, 8:31 a.m. UTC | #3
On 10/1/20 1:43 AM, Heinrich Schuchardt wrote:
> On 9/29/20 10:20 PM, Marek Vasut wrote:
>> On 9/29/20 10:03 PM, Heinrich Schuchardt wrote:
>>> void * can be assigned to any pointer variable. Avoid unnecessary
>>> conversions.
>>>
>>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>>> ---
>>>  drivers/usb/host/xhci-mem.c | 21 +++++++++------------
>>>  1 file changed, 9 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
>>> index 1da0524aa0..4c303ae705 100644
>>> --- a/drivers/usb/host/xhci-mem.c
>>> +++ b/drivers/usb/host/xhci-mem.c
>>> @@ -280,10 +280,10 @@ static struct xhci_segment *xhci_segment_alloc(void)
>>>  {
>>>  	struct xhci_segment *seg;
>>>
>>> -	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
>>> +	seg = malloc(sizeof(struct xhci_segment));
>>
>> As far as I remember, the compiler used to complain about assignment
>> from a different type without a cast ?
> 
> Under the ANSI C standard, the cast is redundant.

U-Boot isn't compiled with -ansi to my knowledge.

> void * is an incomplete type. You can assign it to any pointer and any
> pointer can be assigned to it, both without casting.
> 
> For other data types you are right. GCC will not accept an assignment
> without conversion.
> 
> In C++ you need to cast to assign void * to other types.
> 
>> btw. you might also do a patch which does malloc(sizeof(*seg)); while at it.
> 
> Sure if you prefer that I will respin the patch.

Separate patch is fine. I'll queue this one into next shortly.
Bin Meng Oct. 13, 2020, 2:41 p.m. UTC | #4
On Wed, Sep 30, 2020 at 4:03 AM Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> void * can be assigned to any pointer variable. Avoid unnecessary
> conversions.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  drivers/usb/host/xhci-mem.c | 21 +++++++++------------
>  1 file changed, 9 insertions(+), 12 deletions(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
diff mbox series

Patch

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 1da0524aa0..4c303ae705 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -280,10 +280,10 @@  static struct xhci_segment *xhci_segment_alloc(void)
 {
 	struct xhci_segment *seg;

-	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
+	seg = malloc(sizeof(struct xhci_segment));
 	BUG_ON(!seg);

-	seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE);
+	seg->trbs = xhci_malloc(SEGMENT_SIZE);

 	seg->next = NULL;

@@ -310,7 +310,7 @@  struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
 	struct xhci_ring *ring;
 	struct xhci_segment *prev;

-	ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring));
+	ring = malloc(sizeof(struct xhci_ring));
 	BUG_ON(!ring);

 	if (num_segs == 0)
@@ -426,8 +426,7 @@  static struct xhci_container_ctx
 {
 	struct xhci_container_ctx *ctx;

-	ctx = (struct xhci_container_ctx *)
-		malloc(sizeof(struct xhci_container_ctx));
+	ctx = malloc(sizeof(struct xhci_container_ctx));
 	BUG_ON(!ctx);

 	BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
@@ -437,7 +436,7 @@  static struct xhci_container_ctx
 	if (type == XHCI_CTX_TYPE_INPUT)
 		ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));

-	ctx->bytes = (u8 *)xhci_malloc(ctx->size);
+	ctx->bytes = xhci_malloc(ctx->size);

 	return ctx;
 }
@@ -459,8 +458,7 @@  int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
 		return -EEXIST;
 	}

-	ctrl->devs[slot_id] = (struct xhci_virt_device *)
-					malloc(sizeof(struct xhci_virt_device));
+	ctrl->devs[slot_id] = malloc(sizeof(struct xhci_virt_device));

 	if (!ctrl->devs[slot_id]) {
 		puts("Failed to allocate virtual device\n");
@@ -519,8 +517,7 @@  int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
 	struct xhci_segment *seg;

 	/* DCBAA initialization */
-	ctrl->dcbaa = (struct xhci_device_context_array *)
-			xhci_malloc(sizeof(struct xhci_device_context_array));
+	ctrl->dcbaa = xhci_malloc(sizeof(struct xhci_device_context_array));
 	if (ctrl->dcbaa == NULL) {
 		puts("unable to allocate DCBA\n");
 		return -ENOMEM;
@@ -556,8 +553,8 @@  int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,

 	/* Event ring does not maintain link TRB */
 	ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
-	ctrl->erst.entries = (struct xhci_erst_entry *)
-		xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
+	ctrl->erst.entries = xhci_malloc(sizeof(struct xhci_erst_entry) *
+					 ERST_NUM_SEGS);

 	ctrl->erst.num_entries = ERST_NUM_SEGS;