updated kstrdup
Subsystems (3)
Rusty,
Doing some hacking, and I realized I needed a kstrdup(). Refusing to reinvent it yet again, I went looking for your kstrdup patch. But the patch was marked BROKEN and - I being stubborn, and trying the patch anyway - it did not apply cleanly to the current kernel.
So I updated it for 2.6.0-test11 and (because I am anal) added some comments.
Patch is attached.
Rob Love
drivers/md/dm-ioctl-v1.c | 18 ++++-------------- drivers/md/dm-ioctl-v4.c | 18 ++++-------------- include/linux/string.h | 2 ++ lib/string.c | 18 +++++++++++++++++- 4 files changed, 27 insertions(+), 29 deletions(-)
drivers/md/dm-ioctl-v1.c +4 -14
@@ -14,6 +14,7 @@#include <linux/wait.h> #include <linux/slab.h> #include <linux/devfs_fs_kernel.h> +#include <linux/string.h> #include <asm/uaccess.h>
@@ -115,17 +116,6 @@return NULL ; } -/*----------------------------------------------------------------- -* Inserting, removing and renaming a device. -*---------------------------------------------------------------*/ -static inline char *kstrdup (const char *str) -{ -char *r =kmalloc (strlen (str) +1 ,GFP_KERNEL ); -if (r) -strcpy (r, str); -return r; -} -static struct hash_cell *alloc_cell (const char *name,const char *uuid,struct mapped_device *md) {
@@ -135,7 +125,7 @@if (!hc)return NULL ; - hc->name =kstrdup (name); + hc->name =kstrdup (name,GFP_KERNEL );if (!hc->name) {kfree (hc);return NULL ;
@@ -145,7 +135,7 @@ hc->uuid =NULL ; else { - hc->uuid =kstrdup (uuid); + hc->uuid =kstrdup (uuid,GFP_KERNEL );if (!hc->uuid) {kfree (hc->name);kfree (hc);
@@ -264,7 +254,7 @@/* * duplicate new. */ - new_name =kstrdup (new); + new_name =kstrdup (new,GFP_KERNEL );if (!new_name)return -ENOMEM ;
drivers/md/dm-ioctl-v4.c +4 -14
@@ -14,6 +14,7 @@#include <linux/slab.h> #include <linux/devfs_fs_kernel.h> #include <linux/dm-ioctl.h> +#include <linux/string.h> #include <asm/uaccess.h>
@@ -116,17 +117,6 @@return NULL ; } -/*----------------------------------------------------------------- -* Inserting, removing and renaming a device. -*---------------------------------------------------------------*/ -static inline char *kstrdup (const char *str) -{ -char *r =kmalloc (strlen (str) +1 ,GFP_KERNEL ); -if (r) -strcpy (r, str); -return r; -} -static struct hash_cell *alloc_cell (const char *name,const char *uuid,struct mapped_device *md) {
@@ -136,7 +126,7 @@if (!hc)return NULL ; - hc->name =kstrdup (name); + hc->name =kstrdup (name,GFP_KERNEL );if (!hc->name) {kfree (hc);return NULL ;
@@ -146,7 +136,7 @@ hc->uuid =NULL ; else { - hc->uuid =kstrdup (uuid); + hc->uuid =kstrdup (uuid,GFP_KERNEL );if (!hc->uuid) {kfree (hc->name);kfree (hc);
@@ -268,7 +258,7 @@/* * duplicate new. */ - new_name =kstrdup (new); + new_name =kstrdup (new,GFP_KERNEL );if (!new_name)return -ENOMEM ;
include/linux/string.h +2 -0
@@ -84,6 +84,8 @@extern void *memchr (const void *,int ,__kernel_size_t );#endif +extern char *kstrdup (const char *s,int gfp); + #ifdef __cplusplus }#endif
lib/string.c +17 -1
@@ -23,6 +23,7 @@#include <linux/string.h> #include <linux/ctype.h> #include <linux/module.h> +#include <linux/slab.h> #ifndef __HAVE_ARCH_STRNICMP/**
@@ -584,5 +585,20 @@ }return NULL ; } -#endif + +/* +* kstrdup - allocate space for and copy an existing string +* +* @s: the string to duplicate +* @gfp: the GFP mask used in the kmalloc() call when allocating memory +*/ +char *kstrdup (const char *s,int gfp) +{ +char *buf =kmalloc (strlen (s)+1 , gfp); +if (buf) +strcpy (buf, s); +return buf; +} + +EXPORT_SYMBOL (kstrdup);