1 #include <linux/slab.h>
2 #include <linux/string.h>
3 #include <linux/module.h>
5 #include <asm/uaccess.h>
8 * kstrdup - allocate space for and copy an existing string
10 * @s: the string to duplicate
11 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
13 char *kstrdup(const char *s, gfp_t gfp)
22 buf = kmalloc_track_caller(len, gfp);
27 EXPORT_SYMBOL(kstrdup);
30 * kmemdup - duplicate region of memory
32 * @src: memory region to duplicate
33 * @len: memory region length
34 * @gfp: GFP mask to use
36 void *kmemdup(const void *src, size_t len, gfp_t gfp)
40 p = kmalloc_track_caller(len, gfp);
45 EXPORT_SYMBOL(kmemdup);
48 * krealloc - reallocate memory. The contents will remain unchanged.
49 * @p: object to reallocate memory for.
50 * @new_size: how many bytes of memory are required.
51 * @flags: the type of memory to allocate.
53 * The contents of the object pointed to are preserved up to the
54 * lesser of the new and old sizes. If @p is %NULL, krealloc()
55 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
56 * %NULL pointer, the object pointed to is freed.
58 void *krealloc(const void *p, size_t new_size, gfp_t flags)
63 if (unlikely(!new_size)) {
72 ret = kmalloc_track_caller(new_size, flags);
74 memcpy(ret, p, min(new_size, ks));
79 EXPORT_SYMBOL(krealloc);
82 * strndup_user - duplicate an existing string from user space
84 * @s: The string to duplicate
85 * @n: Maximum number of bytes to copy, including the trailing NUL.
87 char *strndup_user(const char __user *s, long n)
92 length = strnlen_user(s, n);
95 return ERR_PTR(-EFAULT);
98 return ERR_PTR(-EINVAL);
100 p = kmalloc(length, GFP_KERNEL);
103 return ERR_PTR(-ENOMEM);
105 if (copy_from_user(p, s, length)) {
107 return ERR_PTR(-EFAULT);
110 p[length - 1] = '\0';
114 EXPORT_SYMBOL(strndup_user);