diff options
author | Christoph Lameter <clameter@sgi.com> | 2007-07-17 07:03:21 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-17 13:23:01 -0400 |
commit | ef2ad80c7d255ed0449eda947c2d700635b7e0f5 (patch) | |
tree | bc44916bdb25de29c8211566a4b5a1c041fa8ab6 /mm/util.c | |
parent | d45f39cb06610ea456e1d689149b9becacda8b40 (diff) |
Slab allocators: consolidate code for krealloc in mm/util.c
The size of a kmalloc object is readily available via ksize(). ksize is
provided by all allocators and thus we can implement krealloc in a generic
way.
Implement krealloc in mm/util.c and drop slab specific implementations of
krealloc.
Signed-off-by: Christoph Lameter <clameter@sgi.com>
Acked-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/util.c')
-rw-r--r-- | mm/util.c | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -58,6 +58,40 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp) | |||
58 | } | 58 | } |
59 | EXPORT_SYMBOL(kmemdup); | 59 | EXPORT_SYMBOL(kmemdup); |
60 | 60 | ||
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 | |||
61 | /* | 95 | /* |
62 | * strndup_user - duplicate an existing string from user space | 96 | * strndup_user - duplicate an existing string from user space |
63 | * | 97 | * |