2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <asm/uaccess.h>
10 * kstrdup - allocate space for and copy an existing string
11 * @s: the string to duplicate
12 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
14 char *kstrdup(const char *s
, gfp_t gfp
)
23 buf
= kmalloc_track_caller(len
, gfp
);
28 EXPORT_SYMBOL(kstrdup
);
31 * kstrndup - allocate space for and copy an existing string
32 * @s: the string to duplicate
33 * @max: read at most @max chars from @s
34 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
36 char *kstrndup(const char *s
, size_t max
, gfp_t gfp
)
44 len
= strnlen(s
, max
);
45 buf
= kmalloc_track_caller(len
+1, gfp
);
52 EXPORT_SYMBOL(kstrndup
);
55 * kmemdup - duplicate region of memory
57 * @src: memory region to duplicate
58 * @len: memory region length
59 * @gfp: GFP mask to use
61 void *kmemdup(const void *src
, size_t len
, gfp_t gfp
)
65 p
= kmalloc_track_caller(len
, gfp
);
70 EXPORT_SYMBOL(kmemdup
);
73 * __krealloc - like krealloc() but don't free @p.
74 * @p: object to reallocate memory for.
75 * @new_size: how many bytes of memory are required.
76 * @flags: the type of memory to allocate.
78 * This function is like krealloc() except it never frees the originally
79 * allocated buffer. Use this if you don't want to free the buffer immediately
80 * like, for example, with RCU.
82 void *__krealloc(const void *p
, size_t new_size
, gfp_t flags
)
87 if (unlikely(!new_size
))
96 ret
= kmalloc_track_caller(new_size
, flags
);
102 EXPORT_SYMBOL(__krealloc
);
105 * krealloc - reallocate memory. The contents will remain unchanged.
106 * @p: object to reallocate memory for.
107 * @new_size: how many bytes of memory are required.
108 * @flags: the type of memory to allocate.
110 * The contents of the object pointed to are preserved up to the
111 * lesser of the new and old sizes. If @p is %NULL, krealloc()
112 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
113 * %NULL pointer, the object pointed to is freed.
115 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
119 if (unlikely(!new_size
)) {
121 return ZERO_SIZE_PTR
;
124 ret
= __krealloc(p
, new_size
, flags
);
130 EXPORT_SYMBOL(krealloc
);
133 * strndup_user - duplicate an existing string from user space
134 * @s: The string to duplicate
135 * @n: Maximum number of bytes to copy, including the trailing NUL.
137 char *strndup_user(const char __user
*s
, long n
)
142 length
= strnlen_user(s
, n
);
145 return ERR_PTR(-EFAULT
);
148 return ERR_PTR(-EINVAL
);
150 p
= kmalloc(length
, GFP_KERNEL
);
153 return ERR_PTR(-ENOMEM
);
155 if (copy_from_user(p
, s
, length
)) {
157 return ERR_PTR(-EFAULT
);
160 p
[length
- 1] = '\0';
164 EXPORT_SYMBOL(strndup_user
);
166 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
167 void arch_pick_mmap_layout(struct mm_struct
*mm
)
169 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
170 mm
->get_unmapped_area
= arch_get_unmapped_area
;
171 mm
->unmap_area
= arch_unmap_area
;