mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Robert Love <rml@novell.com>
To: Rusty Russell <rusty@rustcorp.com.au>, Mr Morton <akpm@osdl.org>
Cc: linux-kernel@vger.kernel.org
Subject: [patch 1/2] kstrdup: implementation
Date: Thu, 21 Apr 2005 23:41:15 -0400	[thread overview]
Message-ID: <1114141276.6973.87.camel@jenny.boston.ximian.com> (raw)

Rusty and I's LCA kernel tutorial again brought up kstrdup().  Let's
close this never ending saga and provide a standard kernel
implementation.

As an example of the savings from such a patch, there are a handful of
existing strdup() implementations and what looks like 100s of open coded
strdup() uses.  Some of which are surely buggy or less optimal than our
version, and all of which bloat the kernel.

Andrew, patch is against 2.6.12-rc3.

Best,

	Robert Love


The world continually reimplements kstrdup().  Implement an optimal version
and export it to the world.

By: Robert Love and Rusty Russell.

Signed-off-by: Robert Love <rml@novell.com>

 include/linux/string.h |    1 +
 lib/string.c           |   20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff -urN linux-2.6.12-rc3/include/linux/string.h linux/include/linux/string.h
--- linux-2.6.12-rc3/include/linux/string.h	2005-03-02 02:38:07.000000000 -0500
+++ linux/include/linux/string.h	2005-04-21 21:23:04.000000000 -0400
@@ -17,6 +17,7 @@
 extern char * strsep(char **,const char *);
 extern __kernel_size_t strspn(const char *,const char *);
 extern __kernel_size_t strcspn(const char *,const char *);
+extern char * kstrdup(const char *,unsigned int __nocast);
 
 /*
  * Include machine specific inline routines
diff -urN linux-2.6.12-rc3/lib/string.c linux/lib/string.c
--- linux-2.6.12-rc3/lib/string.c	2005-03-02 02:38:25.000000000 -0500
+++ linux/lib/string.c	2005-04-21 21:31:11.000000000 -0400
@@ -22,6 +22,7 @@
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
+#include <linux/slab.h>
 #include <linux/module.h>
 
 #ifndef __HAVE_ARCH_STRNICMP
@@ -76,6 +77,25 @@
 EXPORT_SYMBOL(strcpy);
 #endif
 
+/*
+ * kstrdup - allocate space for and then copy an existing string
+ *
+ * @str: the string to duplicate
+ * @gfp: the GFP mask used to allocate the storage for the duplicated string
+ */
+char * kstrdup(const char *str, unsigned int __nocast flags)
+{
+	size_t len;
+	char *buf;
+
+	len = strlen(str) + 1;
+	buf = kmalloc(len, flags);
+	if (likely(buf))
+		memcpy(buf, str, len);
+	return buf;
+}
+EXPORT_SYMBOL(kstrdup);
+
 #ifndef __HAVE_ARCH_STRNCPY
 /**
  * strncpy - Copy a length-limited, %NUL-terminated string



             reply	other threads:[~2005-04-22  3:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-22  3:41 Robert Love [this message]
2005-04-22  3:51 ` Adrian Bunk
2005-04-22  3:58   ` Robert Love

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1114141276.6973.87.camel@jenny.boston.ximian.com \
    --to=rml@novell.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®