* [PATCH 2.6] 1/7 create kstrdup library function
@ 2005-02-01 3:28 pmarques
2005-02-01 16:44 ` Pekka Enberg
0 siblings, 1 reply; 6+ messages in thread
From: pmarques @ 2005-02-01 3:28 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel
[-- Attachment #1: Type: text/plain, Size: 348 bytes --]
This patch creates the kstrdup library function so that it doesn't have to be
reimplemented (or even EXPORT'ed) by every user that needs it.
Signed-off-by: Paulo Marques <pmarques@grupopie.com>
--
Paulo Marques - www.grupopie.com
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch1 --]
[-- Type: text/x-diff; name="patch1", Size: 1233 bytes --]
diff -buprN -X dontdiff vanilla-2.6.11-rc2-bk9/include/linux/string.h linux-2.6.11-rc2-bk9/include/linux/string.h
--- vanilla-2.6.11-rc2-bk9/include/linux/string.h 2004-12-24 21:34:31.000000000 +0000
+++ linux-2.6.11-rc2-bk9/include/linux/string.h 2005-01-31 19:31:12.000000000 +0000
@@ -88,6 +88,8 @@ extern int memcmp(const void *,const voi
extern void * memchr(const void *,int,__kernel_size_t);
#endif
+extern char *kstrdup(const char *s, int gfp);
+
#ifdef __cplusplus
}
#endif
diff -buprN -X dontdiff vanilla-2.6.11-rc2-bk9/lib/string.c linux-2.6.11-rc2-bk9/lib/string.c
--- vanilla-2.6.11-rc2-bk9/lib/string.c 2005-01-31 20:05:37.000000000 +0000
+++ linux-2.6.11-rc2-bk9/lib/string.c 2005-01-31 20:00:31.000000000 +0000
@@ -599,3 +599,23 @@ void *memchr(const void *s, int c, size_
}
EXPORT_SYMBOL(memchr);
#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)
+{
+ int len;
+ char *buf;
+
+ if (!s) return NULL;
+
+ len = strlen(s) + 1;
+ buf = kmalloc(len, gfp);
+ if (buf)
+ memcpy(buf, s, len);
+ return buf;
+}
+
+EXPORT_SYMBOL(kstrdup);
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2.6] 1/7 create kstrdup library function
2005-02-01 3:28 [PATCH 2.6] 1/7 create kstrdup library function pmarques
@ 2005-02-01 16:44 ` Pekka Enberg
2005-02-01 17:00 ` Paulo Marques
0 siblings, 1 reply; 6+ messages in thread
From: Pekka Enberg @ 2005-02-01 16:44 UTC (permalink / raw)
To: pmarques@grupopie.com; +Cc: Andrew Morton, linux-kernel
Hi,
On Tue, 1 Feb 2005 03:28:21 +0000, pmarques@grupopie.com
<pmarques@grupopie.com> wrote:
>
> This patch creates the kstrdup library function so that it doesn't have to be
> reimplemented (or even EXPORT'ed) by every user that needs it.
>
> Signed-off-by: Paulo Marques <pmarques@grupopie.com>
>
> diff -buprN -X dontdiff vanilla-2.6.11-rc2-bk9/lib/string.c linux-2.6.11-rc2-bk9/lib/string.c
> --- vanilla-2.6.11-rc2-bk9/lib/string.c 2005-01-31 20:05:37.000000000 +0000
> +++ linux-2.6.11-rc2-bk9/lib/string.c 2005-01-31 20:00:31.000000000 +0000
> @@ -599,3 +599,23 @@ void *memchr(const void *s, int c, size_
> }
> EXPORT_SYMBOL(memchr);
> #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)
> +{
> + int len;
> + char *buf;
> +
> + if (!s) return NULL;
> +
> + len = strlen(s) + 1;
> + buf = kmalloc(len, gfp);
> + if (buf)
> + memcpy(buf, s, len);
> + return buf;
> +}
> +
> +EXPORT_SYMBOL(kstrdup);
kstrdup() is a special-case _memory allocator_ (not so much a string
operation) so I think it should go into mm/slab.c where we currently
have kcalloc().
P.S. Please inline patches to your email as per
Documentation/SubmittingPatches. I, for one, have trouble with
attachments.
Pekka
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2.6] 1/7 create kstrdup library function
2005-02-01 16:44 ` Pekka Enberg
@ 2005-02-01 17:00 ` Paulo Marques
2005-02-02 7:23 ` Pekka Enberg
0 siblings, 1 reply; 6+ messages in thread
From: Paulo Marques @ 2005-02-01 17:00 UTC (permalink / raw)
To: Pekka Enberg; +Cc: Andrew Morton, linux-kernel
Pekka Enberg wrote:
> [...]
>
> kstrdup() is a special-case _memory allocator_ (not so much a string
> operation) so I think it should go into mm/slab.c where we currently
> have kcalloc().
I was following Rusty Russell's approach. Also, I believe this is more
intuitive because the standard libc strdup function is declared in string.h.
However, I really don't have strong feelings either way, so if the
majority agrees that this should be in mm/slab, its fine by me.
> P.S. Please inline patches to your email as per
> Documentation/SubmittingPatches. I, for one, have trouble with
> attachments.
Unfortunately my email client messes up inline patches and wordwraps /
mangles white space, so I resort to attaching them until I have time to
look into fixing that :(
--
Paulo Marques - www.grupopie.com
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2.6] 1/7 create kstrdup library function
2005-02-01 17:00 ` Paulo Marques
@ 2005-02-02 7:23 ` Pekka Enberg
2005-02-02 12:17 ` Paulo Marques
0 siblings, 1 reply; 6+ messages in thread
From: Pekka Enberg @ 2005-02-02 7:23 UTC (permalink / raw)
To: Paulo Marques; +Cc: Andrew Morton, linux-kernel, penberg
At some point in time, I wrote:
> > kstrdup() is a special-case _memory allocator_ (not so much a string
> > operation) so I think it should go into mm/slab.c where we currently
> > have kcalloc().
On Tue, 01 Feb 2005 17:00:17 +0000, Paulo Marques <pmarques@grupopie.com> wrote:
> I was following Rusty Russell's approach. Also, I believe this is more
> intuitive because the standard libc strdup function is declared in string.h.
>
> However, I really don't have strong feelings either way, so if the
> majority agrees that this should be in mm/slab, its fine by me.
Intuitive, perhaps, but I think it's wrong. I don't like it because it
makes string operations depend on slab. Furthermore, kstrdup() is not
a string operation. It is about memory allocation, really, just like
kcalloc().
One possible way to clean this up would be to extract the
standard-like allocators (kmalloc, kcalloc, and kstrdup) from
mm/slab.c and move them into a separate file.
Pekka
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2.6] 1/7 create kstrdup library function
2005-02-02 7:23 ` Pekka Enberg
@ 2005-02-02 12:17 ` Paulo Marques
2005-02-02 12:29 ` Pekka J Enberg
0 siblings, 1 reply; 6+ messages in thread
From: Paulo Marques @ 2005-02-02 12:17 UTC (permalink / raw)
To: Pekka Enberg; +Cc: Andrew Morton, linux-kernel, penberg
Pekka Enberg wrote:
> At some point in time, I wrote:
>
>>>kstrdup() is a special-case _memory allocator_ (not so much a string
>>>operation) so I think it should go into mm/slab.c where we currently
>>>have kcalloc().
>
>
> On Tue, 01 Feb 2005 17:00:17 +0000, Paulo Marques <pmarques@grupopie.com> wrote:
>
>>I was following Rusty Russell's approach. Also, I believe this is more
>>intuitive because the standard libc strdup function is declared in string.h.
>>
>>However, I really don't have strong feelings either way, so if the
>>majority agrees that this should be in mm/slab, its fine by me.
>
>
> Intuitive, perhaps, but I think it's wrong. I don't like it because it
> makes string operations depend on slab. Furthermore, kstrdup() is not
> a string operation. It is about memory allocation, really, just like
> kcalloc().
I agree with the "is like kcalloc" argument in the sense that it does an
allocation + something else. But in this case the "something else" is in
fact a string operation, so this just seem to be in the middle.
> One possible way to clean this up would be to extract the
> standard-like allocators (kmalloc, kcalloc, and kstrdup) from
> mm/slab.c and move them into a separate file.
I don't like this approach. From a quick grep you get 4972 kmalloc's in
.c files in the kernel tree and only 35 kstrdup's. Moving kstrdup around
is still just 7 patches, kmalloc is a much bigger problem.
Anyway, as I said before, if more people believe that moving kstrdup
into mm/slab.c is the way to go, then its fine by me. The problem I was
trying to solve was having several versions of strdup-like functions all
around the kernel, and this problem gets fixed either way. Right now,
the poll goes something like this:
string.c: me, Rusty Russell
slab.c: Pekka Enberg
I think we need more votes :)
--
Paulo Marques - www.grupopie.com
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: 1/7 create kstrdup library function
2005-02-02 12:17 ` Paulo Marques
@ 2005-02-02 12:29 ` Pekka J Enberg
0 siblings, 0 replies; 6+ messages in thread
From: Pekka J Enberg @ 2005-02-02 12:29 UTC (permalink / raw)
To: Paulo Marques; +Cc: Pekka Enberg, Andrew Morton, linux-kernel, rusty
Paulo Marques writes:
> I agree with the "is like kcalloc" argument in the sense that it does an
> allocation + something else. But in this case the "something else" is in
> fact a string operation, so this just seem to be in the middle.
Sure, but now you're forcing all users of <string.h> to depend on the slab
as well. Conceptually, kstrdup() is an initializing memory allocator, not a
string operation, which is why I think it should go into slab.c.
Paulo Marques writes:
> I don't like this approach. From a quick grep you get 4972 kmalloc's in .c
> files in the kernel tree and only 35 kstrdup's. Moving kstrdup around is
> still just 7 patches, kmalloc is a much bigger problem.
Hmm? Yes, moving the declaration from slab.h to some other header is
painful. I only talked about moving the definition from slab.c.
Paulo Marques writes:
> Anyway, as I said before, if more people believe that moving kstrdup into
> mm/slab.c is the way to go, then its fine by me. The problem I was trying
> to solve was having several versions of strdup-like functions all around
> the kernel, and this problem gets fixed either way. Right now, the poll
> goes something like this:
>
> string.c: me, Rusty Russell
> slab.c: Pekka Enberg
>
> I think we need more votes :)
Could we also get Rusty's confirmation on this?
Pekka
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-02-02 12:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-02-01 3:28 [PATCH 2.6] 1/7 create kstrdup library function pmarques
2005-02-01 16:44 ` Pekka Enberg
2005-02-01 17:00 ` Paulo Marques
2005-02-02 7:23 ` Pekka Enberg
2005-02-02 12:17 ` Paulo Marques
2005-02-02 12:29 ` Pekka J Enberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox