2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
6 #include <asm/uaccess.h>
9 * 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 * kstrndup - allocate space for and copy an existing string
31 * @s: the string to duplicate
32 * @max: read at most @max chars from @s
33 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
35 char *kstrndup(const char *s
, size_t max
, gfp_t gfp
)
43 len
= strnlen(s
, max
);
44 buf
= kmalloc_track_caller(len
+1, gfp
);
51 EXPORT_SYMBOL(kstrndup
);
54 * kmemdup - duplicate region of memory
56 * @src: memory region to duplicate
57 * @len: memory region length
58 * @gfp: GFP mask to use
60 void *kmemdup(const void *src
, size_t len
, gfp_t gfp
)
64 p
= kmalloc_track_caller(len
, gfp
);
69 EXPORT_SYMBOL(kmemdup
);
72 * krealloc - reallocate memory. The contents will remain unchanged.
73 * @p: object to reallocate memory for.
74 * @new_size: how many bytes of memory are required.
75 * @flags: the type of memory to allocate.
77 * The contents of the object pointed to are preserved up to the
78 * lesser of the new and old sizes. If @p is %NULL, krealloc()
79 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
80 * %NULL pointer, the object pointed to is freed.
82 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
87 if (unlikely(!new_size
)) {
98 ret
= kmalloc_track_caller(new_size
, flags
);
105 EXPORT_SYMBOL(krealloc
);
108 * strndup_user - duplicate an existing string from user space
109 * @s: The string to duplicate
110 * @n: Maximum number of bytes to copy, including the trailing NUL.
112 char *strndup_user(const char __user
*s
, long n
)
117 length
= strnlen_user(s
, n
);
120 return ERR_PTR(-EFAULT
);
123 return ERR_PTR(-EINVAL
);
125 p
= kmalloc(length
, GFP_KERNEL
);
128 return ERR_PTR(-ENOMEM
);
130 if (copy_from_user(p
, s
, length
)) {
132 return ERR_PTR(-EFAULT
);
135 p
[length
- 1] = '\0';
139 EXPORT_SYMBOL(strndup_user
);
141 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
142 void arch_pick_mmap_layout(struct mm_struct
*mm
)
144 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
145 mm
->get_unmapped_area
= arch_get_unmapped_area
;
146 mm
->unmap_area
= arch_unmap_area
;