aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Lameter <clameter@sgi.com>2007-07-17 07:03:21 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-17 13:23:01 -0400
commitef2ad80c7d255ed0449eda947c2d700635b7e0f5 (patch)
treebc44916bdb25de29c8211566a4b5a1c041fa8ab6
parentd45f39cb06610ea456e1d689149b9becacda8b40 (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>
-rw-r--r--mm/slab.c46
-rw-r--r--mm/slob.c33
-rw-r--r--mm/slub.c37
-rw-r--r--mm/util.c34
4 files changed, 34 insertions, 116 deletions
diff --git a/mm/slab.c b/mm/slab.c
index a453383333fc..4bd8a53091b7 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -3726,52 +3726,6 @@ EXPORT_SYMBOL(__kmalloc);
3726#endif 3726#endif
3727 3727
3728/** 3728/**
3729 * krealloc - reallocate memory. The contents will remain unchanged.
3730 * @p: object to reallocate memory for.
3731 * @new_size: how many bytes of memory are required.
3732 * @flags: the type of memory to allocate.
3733 *
3734 * The contents of the object pointed to are preserved up to the
3735 * lesser of the new and old sizes. If @p is %NULL, krealloc()
3736 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
3737 * %NULL pointer, the object pointed to is freed.
3738 */
3739void *krealloc(const void *p, size_t new_size, gfp_t flags)
3740{
3741 struct kmem_cache *cache, *new_cache;
3742 void *ret;
3743
3744 if (unlikely(!p))
3745 return kmalloc_track_caller(new_size, flags);
3746
3747 if (unlikely(!new_size)) {
3748 kfree(p);
3749 return NULL;
3750 }
3751
3752 cache = virt_to_cache(p);
3753 new_cache = __find_general_cachep(new_size, flags);
3754
3755 /*
3756 * If new size fits in the current cache, bail out.
3757 */
3758 if (likely(cache == new_cache))
3759 return (void *)p;
3760
3761 /*
3762 * We are on the slow-path here so do not use __cache_alloc
3763 * because it bloats kernel text.
3764 */
3765 ret = kmalloc_track_caller(new_size, flags);
3766 if (ret) {
3767 memcpy(ret, p, min(new_size, ksize(p)));
3768 kfree(p);
3769 }
3770 return ret;
3771}
3772EXPORT_SYMBOL(krealloc);
3773
3774/**
3775 * kmem_cache_free - Deallocate an object 3729 * kmem_cache_free - Deallocate an object
3776 * @cachep: The cache the allocation was from. 3730 * @cachep: The cache the allocation was from.
3777 * @objp: The previously allocated object. 3731 * @objp: The previously allocated object.
diff --git a/mm/slob.c b/mm/slob.c
index b4899079d8b0..154e7bdf3544 100644
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -446,39 +446,6 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node)
446} 446}
447EXPORT_SYMBOL(__kmalloc_node); 447EXPORT_SYMBOL(__kmalloc_node);
448 448
449/**
450 * krealloc - reallocate memory. The contents will remain unchanged.
451 *
452 * @p: object to reallocate memory for.
453 * @new_size: how many bytes of memory are required.
454 * @flags: the type of memory to allocate.
455 *
456 * The contents of the object pointed to are preserved up to the
457 * lesser of the new and old sizes. If @p is %NULL, krealloc()
458 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
459 * %NULL pointer, the object pointed to is freed.
460 */
461void *krealloc(const void *p, size_t new_size, gfp_t flags)
462{
463 void *ret;
464
465 if (unlikely(!p))
466 return kmalloc_track_caller(new_size, flags);
467
468 if (unlikely(!new_size)) {
469 kfree(p);
470 return NULL;
471 }
472
473 ret = kmalloc_track_caller(new_size, flags);
474 if (ret) {
475 memcpy(ret, p, min(new_size, ksize(p)));
476 kfree(p);
477 }
478 return ret;
479}
480EXPORT_SYMBOL(krealloc);
481
482void kfree(const void *block) 449void kfree(const void *block)
483{ 450{
484 struct slob_page *sp; 451 struct slob_page *sp;
diff --git a/mm/slub.c b/mm/slub.c
index a18708821c1e..1b0a95d75dbb 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2467,43 +2467,6 @@ int kmem_cache_shrink(struct kmem_cache *s)
2467} 2467}
2468EXPORT_SYMBOL(kmem_cache_shrink); 2468EXPORT_SYMBOL(kmem_cache_shrink);
2469 2469
2470/**
2471 * krealloc - reallocate memory. The contents will remain unchanged.
2472 * @p: object to reallocate memory for.
2473 * @new_size: how many bytes of memory are required.
2474 * @flags: the type of memory to allocate.
2475 *
2476 * The contents of the object pointed to are preserved up to the
2477 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2478 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2479 * %NULL pointer, the object pointed to is freed.
2480 */
2481void *krealloc(const void *p, size_t new_size, gfp_t flags)
2482{
2483 void *ret;
2484 size_t ks;
2485
2486 if (unlikely(!p || p == ZERO_SIZE_PTR))
2487 return kmalloc(new_size, flags);
2488
2489 if (unlikely(!new_size)) {
2490 kfree(p);
2491 return ZERO_SIZE_PTR;
2492 }
2493
2494 ks = ksize(p);
2495 if (ks >= new_size)
2496 return (void *)p;
2497
2498 ret = kmalloc(new_size, flags);
2499 if (ret) {
2500 memcpy(ret, p, min(new_size, ks));
2501 kfree(p);
2502 }
2503 return ret;
2504}
2505EXPORT_SYMBOL(krealloc);
2506
2507/******************************************************************** 2470/********************************************************************
2508 * Basic setup of slabs 2471 * Basic setup of slabs
2509 *******************************************************************/ 2472 *******************************************************************/
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)
58} 58}
59EXPORT_SYMBOL(kmemdup); 59EXPORT_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 */
72void *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}
93EXPORT_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 *