Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 1 | #include <linux/slab.h> |
| 2 | #include <linux/string.h> |
| 3 | #include <linux/module.h> |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 4 | #include <linux/err.h> |
| 5 | #include <asm/uaccess.h> |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 6 | |
| 7 | /** |
Pekka Enberg | 40c07ae | 2006-03-25 03:06:43 -0800 | [diff] [blame^] | 8 | * __kzalloc - allocate memory. The memory is set to zero. |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 9 | * @size: how many bytes of memory are required. |
| 10 | * @flags: the type of memory to allocate. |
| 11 | */ |
Pekka Enberg | 40c07ae | 2006-03-25 03:06:43 -0800 | [diff] [blame^] | 12 | void *__kzalloc(size_t size, gfp_t flags) |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 13 | { |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 14 | void *ret = ____kmalloc(size, flags); |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 15 | if (ret) |
| 16 | memset(ret, 0, size); |
| 17 | return ret; |
| 18 | } |
Pekka Enberg | 40c07ae | 2006-03-25 03:06:43 -0800 | [diff] [blame^] | 19 | EXPORT_SYMBOL(__kzalloc); |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * kstrdup - allocate space for and copy an existing string |
| 23 | * |
| 24 | * @s: the string to duplicate |
| 25 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
| 26 | */ |
| 27 | char *kstrdup(const char *s, gfp_t gfp) |
| 28 | { |
| 29 | size_t len; |
| 30 | char *buf; |
| 31 | |
| 32 | if (!s) |
| 33 | return NULL; |
| 34 | |
| 35 | len = strlen(s) + 1; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 36 | buf = ____kmalloc(len, gfp); |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 37 | if (buf) |
| 38 | memcpy(buf, s, len); |
| 39 | return buf; |
| 40 | } |
| 41 | EXPORT_SYMBOL(kstrdup); |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * strndup_user - duplicate an existing string from user space |
| 45 | * |
| 46 | * @s: The string to duplicate |
| 47 | * @n: Maximum number of bytes to copy, including the trailing NUL. |
| 48 | */ |
| 49 | char *strndup_user(const char __user *s, long n) |
| 50 | { |
| 51 | char *p; |
| 52 | long length; |
| 53 | |
| 54 | length = strnlen_user(s, n); |
| 55 | |
| 56 | if (!length) |
| 57 | return ERR_PTR(-EFAULT); |
| 58 | |
| 59 | if (length > n) |
| 60 | return ERR_PTR(-EINVAL); |
| 61 | |
| 62 | p = kmalloc(length, GFP_KERNEL); |
| 63 | |
| 64 | if (!p) |
| 65 | return ERR_PTR(-ENOMEM); |
| 66 | |
| 67 | if (copy_from_user(p, s, length)) { |
| 68 | kfree(p); |
| 69 | return ERR_PTR(-EFAULT); |
| 70 | } |
| 71 | |
| 72 | p[length - 1] = '\0'; |
| 73 | |
| 74 | return p; |
| 75 | } |
| 76 | EXPORT_SYMBOL(strndup_user); |