added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / mm / util.c
blob8d4b90094ee9194cce6dc242b3a374d25c1fa53d
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 <linux/tracepoint.h>
8 #include <asm/uaccess.h>
10 /**
11 * kstrdup - allocate space for and copy an existing string
12 * @s: the string to duplicate
13 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
15 char *kstrdup(const char *s, gfp_t gfp)
17 size_t len;
18 char *buf;
20 if (!s)
21 return NULL;
23 len = strlen(s) + 1;
24 buf = kmalloc_track_caller(len, gfp);
25 if (buf)
26 memcpy(buf, s, len);
27 return buf;
29 EXPORT_SYMBOL(kstrdup);
31 /**
32 * kstrndup - allocate space for and copy an existing string
33 * @s: the string to duplicate
34 * @max: read at most @max chars from @s
35 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
37 char *kstrndup(const char *s, size_t max, gfp_t gfp)
39 size_t len;
40 char *buf;
42 if (!s)
43 return NULL;
45 len = strnlen(s, max);
46 buf = kmalloc_track_caller(len+1, gfp);
47 if (buf) {
48 memcpy(buf, s, len);
49 buf[len] = '\0';
51 return buf;
53 EXPORT_SYMBOL(kstrndup);
55 /**
56 * kmemdup - duplicate region of memory
58 * @src: memory region to duplicate
59 * @len: memory region length
60 * @gfp: GFP mask to use
62 void *kmemdup(const void *src, size_t len, gfp_t gfp)
64 void *p;
66 p = kmalloc_track_caller(len, gfp);
67 if (p)
68 memcpy(p, src, len);
69 return p;
71 EXPORT_SYMBOL(kmemdup);
73 /**
74 * __krealloc - like krealloc() but don't free @p.
75 * @p: object to reallocate memory for.
76 * @new_size: how many bytes of memory are required.
77 * @flags: the type of memory to allocate.
79 * This function is like krealloc() except it never frees the originally
80 * allocated buffer. Use this if you don't want to free the buffer immediately
81 * like, for example, with RCU.
83 void *__krealloc(const void *p, size_t new_size, gfp_t flags)
85 void *ret;
86 size_t ks = 0;
88 if (unlikely(!new_size))
89 return ZERO_SIZE_PTR;
91 if (p)
92 ks = ksize(p);
94 if (ks >= new_size)
95 return (void *)p;
97 ret = kmalloc_track_caller(new_size, flags);
98 if (ret && p)
99 memcpy(ret, p, ks);
101 return ret;
103 EXPORT_SYMBOL(__krealloc);
106 * krealloc - reallocate memory. The contents will remain unchanged.
107 * @p: object to reallocate memory for.
108 * @new_size: how many bytes of memory are required.
109 * @flags: the type of memory to allocate.
111 * The contents of the object pointed to are preserved up to the
112 * lesser of the new and old sizes. If @p is %NULL, krealloc()
113 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
114 * %NULL pointer, the object pointed to is freed.
116 void *krealloc(const void *p, size_t new_size, gfp_t flags)
118 void *ret;
120 if (unlikely(!new_size)) {
121 kfree(p);
122 return ZERO_SIZE_PTR;
125 ret = __krealloc(p, new_size, flags);
126 if (ret && p != ret)
127 kfree(p);
129 return ret;
131 EXPORT_SYMBOL(krealloc);
134 * kzfree - like kfree but zero memory
135 * @p: object to free memory of
137 * The memory of the object @p points to is zeroed before freed.
138 * If @p is %NULL, kzfree() does nothing.
140 void kzfree(const void *p)
142 size_t ks;
143 void *mem = (void *)p;
145 if (unlikely(ZERO_OR_NULL_PTR(mem)))
146 return;
147 ks = ksize(mem);
148 memset(mem, 0, ks);
149 kfree(mem);
151 EXPORT_SYMBOL(kzfree);
154 * strndup_user - duplicate an existing string from user space
155 * @s: The string to duplicate
156 * @n: Maximum number of bytes to copy, including the trailing NUL.
158 char *strndup_user(const char __user *s, long n)
160 char *p;
161 long length;
163 length = strnlen_user(s, n);
165 if (!length)
166 return ERR_PTR(-EFAULT);
168 if (length > n)
169 return ERR_PTR(-EINVAL);
171 p = kmalloc(length, GFP_KERNEL);
173 if (!p)
174 return ERR_PTR(-ENOMEM);
176 if (copy_from_user(p, s, length)) {
177 kfree(p);
178 return ERR_PTR(-EFAULT);
181 p[length - 1] = '\0';
183 return p;
185 EXPORT_SYMBOL(strndup_user);
187 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
188 void arch_pick_mmap_layout(struct mm_struct *mm)
190 mm->mmap_base = TASK_UNMAPPED_BASE;
191 mm->get_unmapped_area = arch_get_unmapped_area;
192 mm->unmap_area = arch_unmap_area;
194 #endif
196 int __attribute__((weak)) get_user_pages_fast(unsigned long start,
197 int nr_pages, int write, struct page **pages)
199 struct mm_struct *mm = current->mm;
200 int ret;
202 down_read(&mm->mmap_sem);
203 ret = get_user_pages(current, mm, start, nr_pages,
204 write, 0, pages, NULL);
205 up_read(&mm->mmap_sem);
207 return ret;
209 EXPORT_SYMBOL_GPL(get_user_pages_fast);
211 /* Tracepoints definitions. */
212 DEFINE_TRACE(kmalloc);
213 DEFINE_TRACE(kmem_cache_alloc);
214 DEFINE_TRACE(kmalloc_node);
215 DEFINE_TRACE(kmem_cache_alloc_node);
216 DEFINE_TRACE(kfree);
217 DEFINE_TRACE(kmem_cache_free);
219 EXPORT_TRACEPOINT_SYMBOL(kmalloc);
220 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
221 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
222 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
223 EXPORT_TRACEPOINT_SYMBOL(kfree);
224 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);