mm/util.c must #include <linux/sched.h>
[deliverable/linux.git] / mm / util.c
1 #include <linux/mm.h>
2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
5 #include <linux/err.h>
6 #include <linux/sched.h>
7 #include <asm/uaccess.h>
8
9 /**
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
13 */
14 char *kstrdup(const char *s, gfp_t gfp)
15 {
16 size_t len;
17 char *buf;
18
19 if (!s)
20 return NULL;
21
22 len = strlen(s) + 1;
23 buf = kmalloc_track_caller(len, gfp);
24 if (buf)
25 memcpy(buf, s, len);
26 return buf;
27 }
28 EXPORT_SYMBOL(kstrdup);
29
30 /**
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
35 */
36 char *kstrndup(const char *s, size_t max, gfp_t gfp)
37 {
38 size_t len;
39 char *buf;
40
41 if (!s)
42 return NULL;
43
44 len = strnlen(s, max);
45 buf = kmalloc_track_caller(len+1, gfp);
46 if (buf) {
47 memcpy(buf, s, len);
48 buf[len] = '\0';
49 }
50 return buf;
51 }
52 EXPORT_SYMBOL(kstrndup);
53
54 /**
55 * kmemdup - duplicate region of memory
56 *
57 * @src: memory region to duplicate
58 * @len: memory region length
59 * @gfp: GFP mask to use
60 */
61 void *kmemdup(const void *src, size_t len, gfp_t gfp)
62 {
63 void *p;
64
65 p = kmalloc_track_caller(len, gfp);
66 if (p)
67 memcpy(p, src, len);
68 return p;
69 }
70 EXPORT_SYMBOL(kmemdup);
71
72 /**
73 * krealloc - reallocate memory. The contents will remain unchanged.
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.
77 *
78 * The contents of the object pointed to are preserved up to the
79 * lesser of the new and old sizes. If @p is %NULL, krealloc()
80 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
81 * %NULL pointer, the object pointed to is freed.
82 */
83 void *krealloc(const void *p, size_t new_size, gfp_t flags)
84 {
85 void *ret;
86 size_t ks = 0;
87
88 if (unlikely(!new_size)) {
89 kfree(p);
90 return ZERO_SIZE_PTR;
91 }
92
93 if (p)
94 ks = ksize(p);
95
96 if (ks >= new_size)
97 return (void *)p;
98
99 ret = kmalloc_track_caller(new_size, flags);
100 if (ret && p) {
101 memcpy(ret, p, ks);
102 kfree(p);
103 }
104 return ret;
105 }
106 EXPORT_SYMBOL(krealloc);
107
108 /*
109 * strndup_user - duplicate an existing string from user space
110 * @s: The string to duplicate
111 * @n: Maximum number of bytes to copy, including the trailing NUL.
112 */
113 char *strndup_user(const char __user *s, long n)
114 {
115 char *p;
116 long length;
117
118 length = strnlen_user(s, n);
119
120 if (!length)
121 return ERR_PTR(-EFAULT);
122
123 if (length > n)
124 return ERR_PTR(-EINVAL);
125
126 p = kmalloc(length, GFP_KERNEL);
127
128 if (!p)
129 return ERR_PTR(-ENOMEM);
130
131 if (copy_from_user(p, s, length)) {
132 kfree(p);
133 return ERR_PTR(-EFAULT);
134 }
135
136 p[length - 1] = '\0';
137
138 return p;
139 }
140 EXPORT_SYMBOL(strndup_user);
141
142 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
143 void arch_pick_mmap_layout(struct mm_struct *mm)
144 {
145 mm->mmap_base = TASK_UNMAPPED_BASE;
146 mm->get_unmapped_area = arch_get_unmapped_area;
147 mm->unmap_area = arch_unmap_area;
148 }
149 #endif
This page took 0.05241 seconds and 6 git commands to generate.