ChangeSet 1.1587.12.42, 2004/04/30 14:16:10-07:00, eike-hotplug@sf-tec.de [PATCH] PCI Hotplug: Remove type magic from kmalloc This patch removes the cast of kmalloc's results to the target pointer type. Also it fixes kmalloc to use sizeof(*foo) instead of sizeof(type_of_foo) as suggested by Matthew Wilcox. Also removes a few useless checks if a pointer is NULL before calling kfree: kfree checks this itself. drivers/pci/hotplug/cpqphp_pci.c | 34 ++++++++++++++++++------------- drivers/pci/hotplug/shpchp_ctrl.c | 41 +++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 36 deletions(-) diff -Nru a/drivers/pci/hotplug/cpqphp_pci.c b/drivers/pci/hotplug/cpqphp_pci.c --- a/drivers/pci/hotplug/cpqphp_pci.c Mon May 17 17:01:36 2004 +++ b/drivers/pci/hotplug/cpqphp_pci.c Mon May 17 17:01:36 2004 @@ -738,7 +738,7 @@ pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus); pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte); - bus_node =(struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL); if (!bus_node) return -ENOMEM; @@ -753,7 +753,7 @@ pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length); if ((b_base <= b_length) && (save_command & 0x01)) { - io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + io_node = kmalloc(sizeof(*io_node), GFP_KERNEL); if (!io_node) return -ENOMEM; @@ -769,7 +769,7 @@ pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length); if ((w_base <= w_length) && (save_command & 0x02)) { - mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL); if (!mem_node) return -ENOMEM; @@ -785,7 +785,7 @@ pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length); if ((w_base <= w_length) && (save_command & 0x02)) { - p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL); if (!p_mem_node) return -ENOMEM; @@ -813,7 +813,8 @@ temp_register = base & 0xFFFFFFFE; temp_register = (~temp_register) + 1; - io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + io_node = kmalloc(sizeof(*io_node), + GFP_KERNEL); if (!io_node) return -ENOMEM; @@ -830,7 +831,8 @@ temp_register = base & 0xFFFFFFF0; temp_register = (~temp_register) + 1; - p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + p_mem_node = kmalloc(sizeof(*p_mem_node), + GFP_KERNEL); if (!p_mem_node) return -ENOMEM; @@ -846,7 +848,8 @@ temp_register = base & 0xFFFFFFF0; temp_register = (~temp_register) + 1; - mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + mem_node = kmalloc(sizeof(*mem_node), + GFP_KERNEL); if (!mem_node) return -ENOMEM; @@ -878,7 +881,8 @@ temp_register = base & 0xFFFFFFFE; temp_register = (~temp_register) + 1; - io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + io_node = kmalloc(sizeof(*io_node), + GFP_KERNEL); if (!io_node) return -ENOMEM; @@ -894,7 +898,8 @@ temp_register = base & 0xFFFFFFF0; temp_register = (~temp_register) + 1; - p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + p_mem_node = kmalloc(sizeof(*p_mem_node), + GFP_KERNEL); if (!p_mem_node) return -ENOMEM; @@ -910,7 +915,8 @@ temp_register = base & 0xFFFFFFF0; temp_register = (~temp_register) + 1; - mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + mem_node = kmalloc(sizeof(*mem_node), + GFP_KERNEL); if (!mem_node) return -ENOMEM; @@ -1293,7 +1299,7 @@ temp_dword = io_base + io_length; if ((io_base) && (temp_dword < 0x10000)) { - io_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + io_node = kmalloc(sizeof(*io_node), GFP_KERNEL); if (!io_node) return -ENOMEM; @@ -1315,7 +1321,7 @@ // If we've got a valid memory base, use it temp_dword = mem_base + mem_length; if ((mem_base) && (temp_dword < 0x10000)) { - mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL); if (!mem_node) return -ENOMEM; @@ -1339,7 +1345,7 @@ // the base + length isn't greater than 0xFFFF temp_dword = pre_mem_base + pre_mem_length; if ((pre_mem_base) && (temp_dword < 0x10000)) { - p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL); if (!p_mem_node) return -ENOMEM; @@ -1363,7 +1369,7 @@ // The second condition is to ignore bus numbers on // populated slots that don't have PCI-PCI bridges if (secondary_bus && (secondary_bus != primary_bus)) { - bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + bus_node = kmalloc(sizeof(*bus_mode), GFP_KERNEL); if (!bus_node) return -ENOMEM; diff -Nru a/drivers/pci/hotplug/shpchp_ctrl.c b/drivers/pci/hotplug/shpchp_ctrl.c --- a/drivers/pci/hotplug/shpchp_ctrl.c Mon May 17 17:01:36 2004 +++ b/drivers/pci/hotplug/shpchp_ctrl.c Mon May 17 17:01:36 2004 @@ -396,7 +396,7 @@ /* This one isn't an aligned length, so we'll make a new entry * and split it up. */ - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -530,7 +530,7 @@ if ((node->length - (temp_dword - node->base)) < size) continue; - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -549,7 +549,7 @@ if (node->length > size) { /* This one is longer than we need so we'll make a new entry and split it up */ - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -630,7 +630,7 @@ if ((max->length - (temp_dword - max->base)) < size) continue; - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -648,7 +648,7 @@ if ((max->base + max->length) & (size - 1)) { /* This one isn't end aligned properly at the top so we'll make a new entry and split it up */ - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -669,7 +669,8 @@ for ( i = 0; max_size[i] > size; i++) { if (max->length > max_size[i]) { - split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), + GFP_KERNEL); if (!split_node) break; /* return (NULL); */ split_node->base = max->base + max_size[i]; @@ -744,7 +745,7 @@ if ((node->length - (temp_dword - node->base)) < size) continue; - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -764,7 +765,7 @@ dbg("%s: too big\n", __FUNCTION__); /* this one is longer than we need so we'll make a new entry and split it up */ - split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + split_node = kmalloc(sizeof(*split_node), GFP_KERNEL); if (!split_node) return(NULL); @@ -882,7 +883,7 @@ struct pci_func *new_slot; struct pci_func *next; - new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL); + new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL); if (new_slot == NULL) { return(new_slot); @@ -1856,7 +1857,7 @@ struct hotplug_slot_info *info; int result; - info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL); + info = kmalloc(sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM; @@ -2504,20 +2505,16 @@ /* Make copies of the nodes we are going to pass down so that * if there is a problem,we can just use these to free resources */ - hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); - hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); - hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); - hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL); + hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL); + hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL); + hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL); + hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL); if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) { - if (hold_bus_node) - kfree(hold_bus_node); - if (hold_IO_node) - kfree(hold_IO_node); - if (hold_mem_node) - kfree(hold_mem_node); - if (hold_p_mem_node) - kfree(hold_p_mem_node); + kfree(hold_bus_node); + kfree(hold_IO_node); + kfree(hold_mem_node); + kfree(hold_p_mem_node); return(1); }