Skip to contentSkip to conversation

Implement devm_kcalloc

RFC PATCH In review Brandon Philips 2 participants 2 messages Aug 2, 2007 – Aug 3, 2007

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

RFC PATCH 4/5 Implement devm_kcalloc

devm_kcalloc is a simple wrapper around devm_kzalloc for arrays. This is needed because kcalloc is often used in network devices.

---
 drivers/base/devres.c  |   16 ++++++++++++++++
 include/linux/device.h |    1 +
 2 files changed, 17 insertions(+)
drivers/base/devres.c +16 -0
Index: linux-2.6/drivers/base/devres.c
===================================================================
@@ -630,6 +630,22 @@ void * devm_kzalloc(struct device *dev, 
 EXPORT_SYMBOL_GPL(devm_kzalloc);
 
 /**
+ * devm_kcalloc - resource-managed kcalloc
+ * @dev: Device to allocate memory for
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate.
+ */
+inline void * devm_kcalloc(struct device * dev, size_t n, size_t size,
+			   gfp_t flags)
+{
+        if (n != 0 && size > ULONG_MAX / n)
+                return NULL;
+        return devm_kzalloc(dev, n * size, flags);
+}
+EXPORT_SYMBOL_GPL(devm_kcalloc);
+
+/**
  * devm_kfree - Resource-managed kfree
  * @dev: Device this memory belongs to
  * @p: Memory to free
include/linux/device.h +1 -0
Index: linux-2.6/include/linux/device.h
===================================================================
@@ -402,6 +402,7 @@ extern int devres_release_group(struct d
 
 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
+extern void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t flags);
 extern void devm_kfree(struct device *dev, void *p);
 
 struct device {
#1 lore raw
Quoted 7 lines · Brandon Philips

/**
+ * devm_kcalloc - resource-managed kcalloc
+ * @dev: Device to allocate memory for
+ * @n: number of elements.
+ * @size: element size.
+ * @flags: the type of memory to allocate.
+ */

drivers/base/devres.c void * devm_kzalloc(struct device *dev, · line 646

Quoted 8 lines
drivers/base/devres.c +8 -0
+inline void * devm_kcalloc(struct device * dev, size_t n, size_t size,
+			   gfp_t flags)
+{
+        if (n != 0 && size > ULONG_MAX / n)
+                return NULL;
+        return devm_kzalloc(dev, n * size, flags);
+}
+EXPORT_SYMBOL_GPL(devm_kcalloc);

Please drop inline. It's meaningless.

Other than that, Acked-by: Tejun Heo <[email protected]>

Signature · 1 line
tejun