nfs: fix sparse warnings
[linux-2.6/kmemtrace.git] / mm / util.c
blob8f18683825bcd0e3b95b4fe1754befb3e0b0268c
1 #include <linux/slab.h>
2 #include <linux/string.h>
3 #include <linux/module.h>
4 #include <linux/err.h>
5 #include <asm/uaccess.h>
7 /**
8 * kstrdup - allocate space for and copy an existing string
9 * @s: the string to duplicate
10 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
12 char *kstrdup(const char *s, gfp_t gfp)
14 size_t len;
15 char *buf;
17 if (!s)
18 return NULL;
20 len = strlen(s) + 1;
21 buf = kmalloc_track_caller(len, gfp);
22 if (buf)
23 memcpy(buf, s, len);
24 return buf;
26 EXPORT_SYMBOL(kstrdup);
28 /**
29 * kstrndup - allocate space for and copy an existing string
30 * @s: the string to duplicate
31 * @max: read at most @max chars from @s
32 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
34 char *kstrndup(const char *s, size_t max, gfp_t gfp)
36 size_t len;
37 char *buf;
39 if (!s)
40 return NULL;
42 len = strnlen(s, max);
43 buf = kmalloc_track_caller(len+1, gfp);
44 if (buf) {
45 memcpy(buf, s, len);
46 buf[len] = '\0';
48 return buf;
50 EXPORT_SYMBOL(kstrndup);
52 /**
53 * kmemdup - duplicate region of memory
55 * @src: memory region to duplicate
56 * @len: memory region length
57 * @gfp: GFP mask to use
59 void *kmemdup(const void *src, size_t len, gfp_t gfp)
61 void *p;
63 p = kmalloc_track_caller(len, gfp);
64 if (p)
65 memcpy(p, src, len);
66 return p;
68 EXPORT_SYMBOL(kmemdup);
70 /**
71 * krealloc - reallocate memory. The contents will remain unchanged.
72 * @p: object to reallocate memory for.
73 * @new_size: how many bytes of memory are required.
74 * @flags: the type of memory to allocate.
76 * The contents of the object pointed to are preserved up to the
77 * lesser of the new and old sizes. If @p is %NULL, krealloc()
78 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
79 * %NULL pointer, the object pointed to is freed.
81 void *krealloc(const void *p, size_t new_size, gfp_t flags)
83 void *ret;
84 size_t ks = 0;
86 if (unlikely(!new_size)) {
87 kfree(p);
88 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);
100 kfree(p);
102 return ret;
104 EXPORT_SYMBOL(krealloc);
107 * strndup_user - duplicate an existing string from user space
108 * @s: The string to duplicate
109 * @n: Maximum number of bytes to copy, including the trailing NUL.
111 char *strndup_user(const char __user *s, long n)
113 char *p;
114 long length;
116 length = strnlen_user(s, n);
118 if (!length)
119 return ERR_PTR(-EFAULT);
121 if (length > n)
122 return ERR_PTR(-EINVAL);
124 p = kmalloc(length, GFP_KERNEL);
126 if (!p)
127 return ERR_PTR(-ENOMEM);
129 if (copy_from_user(p, s, length)) {
130 kfree(p);
131 return ERR_PTR(-EFAULT);
134 p[length - 1] = '\0';
136 return p;
138 EXPORT_SYMBOL(strndup_user);