mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-29 19:21:31 -04:00
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 <[email protected]> Acked-by: Pekka Enberg <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
This commit is contained in:
parent
d45f39cb06
commit
ef2ad80c7d
4 changed files with 34 additions and 116 deletions
46
mm/slab.c
46
mm/slab.c
|
|
@ -3725,52 +3725,6 @@ void *__kmalloc(size_t size, gfp_t flags)
|
|||
EXPORT_SYMBOL(__kmalloc);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
struct kmem_cache *cache, *new_cache;
|
||||
void *ret;
|
||||
|
||||
if (unlikely(!p))
|
||||
return kmalloc_track_caller(new_size, flags);
|
||||
|
||||
if (unlikely(!new_size)) {
|
||||
kfree(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cache = virt_to_cache(p);
|
||||
new_cache = __find_general_cachep(new_size, flags);
|
||||
|
||||
/*
|
||||
* If new size fits in the current cache, bail out.
|
||||
*/
|
||||
if (likely(cache == new_cache))
|
||||
return (void *)p;
|
||||
|
||||
/*
|
||||
* We are on the slow-path here so do not use __cache_alloc
|
||||
* because it bloats kernel text.
|
||||
*/
|
||||
ret = kmalloc_track_caller(new_size, flags);
|
||||
if (ret) {
|
||||
memcpy(ret, p, min(new_size, ksize(p)));
|
||||
kfree(p);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(krealloc);
|
||||
|
||||
/**
|
||||
* kmem_cache_free - Deallocate an object
|
||||
* @cachep: The cache the allocation was from.
|
||||
|
|
|
|||
Loading…
Reference in a new issue