[PATCH 02/18] base: driver.c: Coalesce error checking code

Andrey Smirnov andrew.smirnov at gmail.com
Tue Feb 16 17:29:03 PST 2016


Both dev_get_mem_region and dev_get_mem_region_by_name do exactly the
same thing with struct resource they obtain. Move that code into a
dedicated helper function.

Signed-off-by: Andrey Smirnov <andrew.smirnov at gmail.com>
---
 drivers/base/driver.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index a70fbb2..2ef7ca9 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -325,15 +325,18 @@ struct resource *dev_get_resource(struct device_d *dev, unsigned long type,
 	return ERR_PTR(-ENOENT);
 }
 
+static void *__start_or_err(const struct resource *res)
+{
+	return IS_ERR(res) ? ERR_CAST(res) : IOMEM(res->start);
+}
+
 void *dev_get_mem_region(struct device_d *dev, int num)
 {
 	struct resource *res;
 
 	res = dev_get_resource(dev, IORESOURCE_MEM, num);
-	if (IS_ERR(res))
-		return ERR_CAST(res);
 
-	return (void __force *)res->start;
+	return __start_or_err(res);
 }
 EXPORT_SYMBOL(dev_get_mem_region);
 
@@ -361,10 +364,8 @@ void *dev_get_mem_region_by_name(struct device_d *dev, const char *name)
 	struct resource *res;
 
 	res = dev_get_resource_by_name(dev, IORESOURCE_MEM, name);
-	if (IS_ERR(res))
-		return ERR_CAST(res);
 
-	return (void __force *)res->start;
+	return __start_or_err(res);
 }
 EXPORT_SYMBOL(dev_get_mem_region_by_name);
 
-- 
2.5.0




More information about the barebox mailing list