mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-28 18:51:56 -04:00
slab: introduce krealloc
This introduce krealloc() that reallocates memory while keeping the contents unchanged. The allocator avoids reallocation if the new size fits the currently used cache. I also added a simple non-optimized version for mm/slob.c for compatibility. [[email protected]: fix warnings] Acked-by: Josef Sipek <[email protected]> Acked-by: Matt Mackall <[email protected]> Acked-by: Christoph Lameter <[email protected]> Signed-off-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
e3ebadd95c
commit
fd76bab2fa
3 changed files with 84 additions and 3 deletions
35
mm/slob.c
35
mm/slob.c
|
|
@ -190,6 +190,39 @@ void *__kmalloc(size_t size, gfp_t gfp)
|
|||
}
|
||||
EXPORT_SYMBOL(__kmalloc);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
if (unlikely(!p))
|
||||
return kmalloc_track_caller(new_size, flags);
|
||||
|
||||
if (unlikely(!new_size)) {
|
||||
kfree(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = kmalloc_track_caller(new_size, flags);
|
||||
if (ret) {
|
||||
memcpy(ret, p, min(new_size, ksize(p)));
|
||||
kfree(p);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL(krealloc);
|
||||
|
||||
void kfree(const void *block)
|
||||
{
|
||||
bigblock_t *bb, **last = &bigblocks;
|
||||
|
|
@ -219,7 +252,7 @@ void kfree(const void *block)
|
|||
|
||||
EXPORT_SYMBOL(kfree);
|
||||
|
||||
unsigned int ksize(const void *block)
|
||||
size_t ksize(const void *block)
|
||||
{
|
||||
bigblock_t *bb;
|
||||
unsigned long flags;
|
||||
|
|
|
|||
Loading…
Reference in a new issue