1 #include <linux/slab.h>
2 #include <linux/string.h>
3 #include <linux/module.h>
5 #include <asm/uaccess.h>
8 * __kzalloc - allocate memory. The memory is set to zero.
9 * @size: how many bytes of memory are required.
10 * @flags: the type of memory to allocate.
12 void *__kzalloc(size_t size, gfp_t flags)
14 void *ret = kmalloc_track_caller(size, flags);
19 EXPORT_SYMBOL(__kzalloc);
22 * kstrdup - allocate space for and copy an existing string
24 * @s: the string to duplicate
25 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
27 char *kstrdup(const char *s, gfp_t gfp)
36 buf = kmalloc_track_caller(len, gfp);
41 EXPORT_SYMBOL(kstrdup);
44 * kmemdup - duplicate region of memory
46 * @src: memory region to duplicate
47 * @len: memory region length
48 * @gfp: GFP mask to use
50 void *kmemdup(const void *src, size_t len, gfp_t gfp)
54 p = kmalloc_track_caller(len, gfp);
59 EXPORT_SYMBOL(kmemdup);
62 * krealloc - reallocate memory. The contents will remain unchanged.
63 * @p: object to reallocate memory for.
64 * @new_size: how many bytes of memory are required.
65 * @flags: the type of memory to allocate.
67 * The contents of the object pointed to are preserved up to the
68 * lesser of the new and old sizes. If @p is %NULL, krealloc()
69 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
70 * %NULL pointer, the object pointed to is freed.
72 void *krealloc(const void *p, size_t new_size, gfp_t flags)
77 if (unlikely(!new_size)) {
86 ret = kmalloc_track_caller(new_size, flags);
88 memcpy(ret, p, min(new_size, ks));
93 EXPORT_SYMBOL(krealloc);
96 * strndup_user - duplicate an existing string from user space
98 * @s: The string to duplicate
99 * @n: Maximum number of bytes to copy, including the trailing NUL.
101 char *strndup_user(const char __user *s, long n)
106 length = strnlen_user(s, n);
109 return ERR_PTR(-EFAULT);
112 return ERR_PTR(-EINVAL);
114 p = kmalloc(length, GFP_KERNEL);
117 return ERR_PTR(-ENOMEM);
119 if (copy_from_user(p, s, length)) {
121 return ERR_PTR(-EFAULT);
124 p[length - 1] = '\0';
128 EXPORT_SYMBOL(strndup_user);