diff options
author | Christoph Lameter <clameter@sgi.com> | 2007-07-17 13:03:21 +0200 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-17 19:23:01 +0200 |
commit | ef2ad80c7d255ed0449eda947c2d700635b7e0f5 (patch) | |
tree | bc44916bdb25de29c8211566a4b5a1c041fa8ab6 /mm/util.c | |
parent | SLUB Debug: fix initial object debug state of NUMA bootstrap objects (diff) | |
download | linux-ef2ad80c7d255ed0449eda947c2d700635b7e0f5.tar.xz linux-ef2ad80c7d255ed0449eda947c2d700635b7e0f5.zip |
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 '')
-rw-r--r-- | mm/util.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/mm/util.c b/mm/util.c index ace2aea69f1a..18396ea63ee6 100644 --- a/mm/util.c +++ b/mm/util.c @@ -58,6 +58,40 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp) } EXPORT_SYMBOL(kmemdup); +/** + * krealloc - reallocate memory. The contents will remain unchanged. + * @p: object to reallocate memory for. + * @new_size: how many bytes of memory are required. + * @flags: the type of memory to allocate. + * + * The contents of the object pointed to are preserved up to the + * lesser of the new and old sizes. If @p is %NULL, krealloc() + * behaves exactly like kmalloc(). If @size is 0 and @p is not a + * %NULL pointer, the object pointed to is freed. + */ +void *krealloc(const void *p, size_t new_size, gfp_t flags) +{ + void *ret; + size_t ks; + + if (unlikely(!new_size)) { + kfree(p); + return NULL; + } + + ks = ksize(p); + if (ks >= new_size) + return (void *)p; + + ret = kmalloc_track_caller(new_size, flags); + if (ret) { + memcpy(ret, p, min(new_size, ks)); + kfree(p); + } + return ret; +} +EXPORT_SYMBOL(krealloc); + /* * strndup_user - duplicate an existing string from user space * |