Staging: epl: clean up demo_main.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / util.c
blob7c122e49f769ccea98ae105746e0238acc1652ce
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>
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
14 char *kstrdup(const char *s, gfp_t gfp)
16 size_t len;
17 char *buf;
19 if (!s)
20 return NULL;
22 len = strlen(s) + 1;
23 buf = kmalloc_track_caller(len, gfp);
24 if (buf)
25 memcpy(buf, s, len);
26 return buf;
28 EXPORT_SYMBOL(kstrdup);
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
36 char *kstrndup(const char *s, size_t max, gfp_t gfp)
38 size_t len;
39 char *buf;
41 if (!s)
42 return NULL;
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';
50 return buf;
52 EXPORT_SYMBOL(kstrndup);
54 /**
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)
63 void *p;
65 p = kmalloc_track_caller(len, gfp);
66 if (p)
67 memcpy(p, src, len);
68 return p;
70 EXPORT_SYMBOL(kmemdup);
72 /**
73 * memdup_user - duplicate memory region from user space
75 * @src: source address in user space
76 * @len: number of bytes to copy
78 * Returns an ERR_PTR() on failure.
80 void *memdup_user(const void __user *src, size_t len)
82 void *p;
85 * Always use GFP_KERNEL, since copy_from_user() can sleep and
86 * cause pagefault, which makes it pointless to use GFP_NOFS
87 * or GFP_ATOMIC.
89 p = kmalloc_track_caller(len, GFP_KERNEL);
90 if (!p)
91 return ERR_PTR(-ENOMEM);
93 if (copy_from_user(p, src, len)) {
94 kfree(p);
95 return ERR_PTR(-EFAULT);
98 return p;
100 EXPORT_SYMBOL(memdup_user);
103 * __krealloc - like krealloc() but don't free @p.
104 * @p: object to reallocate memory for.
105 * @new_size: how many bytes of memory are required.
106 * @flags: the type of memory to allocate.
108 * This function is like krealloc() except it never frees the originally
109 * allocated buffer. Use this if you don't want to free the buffer immediately
110 * like, for example, with RCU.
112 void *__krealloc(const void *p, size_t new_size, gfp_t flags)
114 void *ret;
115 size_t ks = 0;
117 if (unlikely(!new_size))
118 return ZERO_SIZE_PTR;
120 if (p)
121 ks = ksize(p);
123 if (ks >= new_size)
124 return (void *)p;
126 ret = kmalloc_track_caller(new_size, flags);
127 if (ret && p)
128 memcpy(ret, p, ks);
130 return ret;
132 EXPORT_SYMBOL(__krealloc);
135 * krealloc - reallocate memory. The contents will remain unchanged.
136 * @p: object to reallocate memory for.
137 * @new_size: how many bytes of memory are required.
138 * @flags: the type of memory to allocate.
140 * The contents of the object pointed to are preserved up to the
141 * lesser of the new and old sizes. If @p is %NULL, krealloc()
142 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
143 * %NULL pointer, the object pointed to is freed.
145 void *krealloc(const void *p, size_t new_size, gfp_t flags)
147 void *ret;
149 if (unlikely(!new_size)) {
150 kfree(p);
151 return ZERO_SIZE_PTR;
154 ret = __krealloc(p, new_size, flags);
155 if (ret && p != ret)
156 kfree(p);
158 return ret;
160 EXPORT_SYMBOL(krealloc);
163 * kzfree - like kfree but zero memory
164 * @p: object to free memory of
166 * The memory of the object @p points to is zeroed before freed.
167 * If @p is %NULL, kzfree() does nothing.
169 void kzfree(const void *p)
171 size_t ks;
172 void *mem = (void *)p;
174 if (unlikely(ZERO_OR_NULL_PTR(mem)))
175 return;
176 ks = ksize(mem);
177 memset(mem, 0, ks);
178 kfree(mem);
180 EXPORT_SYMBOL(kzfree);
183 * strndup_user - duplicate an existing string from user space
184 * @s: The string to duplicate
185 * @n: Maximum number of bytes to copy, including the trailing NUL.
187 char *strndup_user(const char __user *s, long n)
189 char *p;
190 long length;
192 length = strnlen_user(s, n);
194 if (!length)
195 return ERR_PTR(-EFAULT);
197 if (length > n)
198 return ERR_PTR(-EINVAL);
200 p = kmalloc(length, GFP_KERNEL);
202 if (!p)
203 return ERR_PTR(-ENOMEM);
205 if (copy_from_user(p, s, length)) {
206 kfree(p);
207 return ERR_PTR(-EFAULT);
210 p[length - 1] = '\0';
212 return p;
214 EXPORT_SYMBOL(strndup_user);
216 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
217 void arch_pick_mmap_layout(struct mm_struct *mm)
219 mm->mmap_base = TASK_UNMAPPED_BASE;
220 mm->get_unmapped_area = arch_get_unmapped_area;
221 mm->unmap_area = arch_unmap_area;
223 #endif
225 int __attribute__((weak)) get_user_pages_fast(unsigned long start,
226 int nr_pages, int write, struct page **pages)
228 struct mm_struct *mm = current->mm;
229 int ret;
231 down_read(&mm->mmap_sem);
232 ret = get_user_pages(current, mm, start, nr_pages,
233 write, 0, pages, NULL);
234 up_read(&mm->mmap_sem);
236 return ret;
238 EXPORT_SYMBOL_GPL(get_user_pages_fast);