Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

phylib: introduce mdiobus_alloc_size()

Introduce function mdiobus_alloc_size() as an alternative to mdiobus_alloc().
Most callers of mdiobus_alloc() also allocate a private data structure, and
then manually point bus->priv to this object. mdiobus_alloc_size()
combines the two operations into one, which simplifies memory management.

The original mdiobus_alloc() now just calls mdiobus_alloc_size(0).

Signed-off-by: Timur Tabi <timur@freescale.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Timur Tabi and committed by
David S. Miller
eb8a54a7 1398eee0

+25 -6
+19 -5
drivers/net/phy/mdio_bus.c
··· 37 37 #include <asm/uaccess.h> 38 38 39 39 /** 40 - * mdiobus_alloc - allocate a mii_bus structure 40 + * mdiobus_alloc_size - allocate a mii_bus structure 41 41 * 42 42 * Description: called by a bus driver to allocate an mii_bus 43 43 * structure to fill in. 44 + * 45 + * 'size' is an an extra amount of memory to allocate for private storage. 46 + * If non-zero, then bus->priv is points to that memory. 44 47 */ 45 - struct mii_bus *mdiobus_alloc(void) 48 + struct mii_bus *mdiobus_alloc_size(size_t size) 46 49 { 47 50 struct mii_bus *bus; 51 + size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 52 + size_t alloc_size; 48 53 49 - bus = kzalloc(sizeof(*bus), GFP_KERNEL); 50 - if (bus != NULL) 54 + /* If we alloc extra space, it should be aligned */ 55 + if (size) 56 + alloc_size = aligned_size + size; 57 + else 58 + alloc_size = sizeof(*bus); 59 + 60 + bus = kzalloc(alloc_size, GFP_KERNEL); 61 + if (bus) { 51 62 bus->state = MDIOBUS_ALLOCATED; 63 + if (size) 64 + bus->priv = (void *)bus + aligned_size; 65 + } 52 66 53 67 return bus; 54 68 } 55 - EXPORT_SYMBOL(mdiobus_alloc); 69 + EXPORT_SYMBOL(mdiobus_alloc_size); 56 70 57 71 /** 58 72 * mdiobus_release - mii_bus device release callback
+6 -1
include/linux/phy.h
··· 129 129 }; 130 130 #define to_mii_bus(d) container_of(d, struct mii_bus, dev) 131 131 132 - struct mii_bus *mdiobus_alloc(void); 132 + struct mii_bus *mdiobus_alloc_size(size_t); 133 + static inline struct mii_bus *mdiobus_alloc(void) 134 + { 135 + return mdiobus_alloc_size(0); 136 + } 137 + 133 138 int mdiobus_register(struct mii_bus *bus); 134 139 void mdiobus_unregister(struct mii_bus *bus); 135 140 void mdiobus_free(struct mii_bus *bus);