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 | { |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 14 | void *ret = kmalloc_track_caller(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; |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 36 | buf = kmalloc_track_caller(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 | |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 43 | /** |
| 44 | * kmemdup - duplicate region of memory |
| 45 | * |
| 46 | * @src: memory region to duplicate |
| 47 | * @len: memory region length |
| 48 | * @gfp: GFP mask to use |
| 49 | */ |
| 50 | void *kmemdup(const void *src, size_t len, gfp_t gfp) |
| 51 | { |
| 52 | void *p; |
| 53 | |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 54 | p = kmalloc_track_caller(len, gfp); |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 55 | if (p) |
| 56 | memcpy(p, src, len); |
| 57 | return p; |
| 58 | } |
| 59 | EXPORT_SYMBOL(kmemdup); |
| 60 | |
Christoph Lameter | ef2ad80 | 2007-07-17 04:03:21 -0700 | [diff] [blame^] | 61 | /** |
| 62 | * krealloc - reallocate memory. The contents will remain unchanged. |
| 63 | * @p: object to reallocate memory for. |
| 64 | * @new_size: how many bytes of memory are required. |
| 65 | * @flags: the type of memory to allocate. |
| 66 | * |
| 67 | * The contents of the object pointed to are preserved up to the |
| 68 | * lesser of the new and old sizes. If @p is %NULL, krealloc() |
| 69 | * behaves exactly like kmalloc(). If @size is 0 and @p is not a |
| 70 | * %NULL pointer, the object pointed to is freed. |
| 71 | */ |
| 72 | void *krealloc(const void *p, size_t new_size, gfp_t flags) |
| 73 | { |
| 74 | void *ret; |
| 75 | size_t ks; |
| 76 | |
| 77 | if (unlikely(!new_size)) { |
| 78 | kfree(p); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | ks = ksize(p); |
| 83 | if (ks >= new_size) |
| 84 | return (void *)p; |
| 85 | |
| 86 | ret = kmalloc_track_caller(new_size, flags); |
| 87 | if (ret) { |
| 88 | memcpy(ret, p, min(new_size, ks)); |
| 89 | kfree(p); |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | EXPORT_SYMBOL(krealloc); |
| 94 | |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 95 | /* |
| 96 | * strndup_user - duplicate an existing string from user space |
| 97 | * |
| 98 | * @s: The string to duplicate |
| 99 | * @n: Maximum number of bytes to copy, including the trailing NUL. |
| 100 | */ |
| 101 | char *strndup_user(const char __user *s, long n) |
| 102 | { |
| 103 | char *p; |
| 104 | long length; |
| 105 | |
| 106 | length = strnlen_user(s, n); |
| 107 | |
| 108 | if (!length) |
| 109 | return ERR_PTR(-EFAULT); |
| 110 | |
| 111 | if (length > n) |
| 112 | return ERR_PTR(-EINVAL); |
| 113 | |
| 114 | p = kmalloc(length, GFP_KERNEL); |
| 115 | |
| 116 | if (!p) |
| 117 | return ERR_PTR(-ENOMEM); |
| 118 | |
| 119 | if (copy_from_user(p, s, length)) { |
| 120 | kfree(p); |
| 121 | return ERR_PTR(-EFAULT); |
| 122 | } |
| 123 | |
| 124 | p[length - 1] = '\0'; |
| 125 | |
| 126 | return p; |
| 127 | } |
| 128 | EXPORT_SYMBOL(strndup_user); |