Gitweb:     
http://git.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=fd76bab2fa6d8f3ef6b326a4c6ae442fa21d30a4
Commit:     fd76bab2fa6d8f3ef6b326a4c6ae442fa21d30a4
Parent:     e3ebadd95cb621e2c7436f3d3646447ac9d5c16d
Author:     Pekka Enberg <[EMAIL PROTECTED]>
AuthorDate: Sun May 6 14:48:40 2007 -0700
Committer:  Linus Torvalds <[EMAIL PROTECTED]>
CommitDate: Mon May 7 12:12:50 2007 -0700

    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]>
---
 include/linux/slab.h |    3 ++-
 mm/slab.c            |   49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 mm/slob.c            |   35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 1ef822e..2f8f60f 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -72,8 +72,9 @@ static inline void *kmem_cache_alloc_node(struct kmem_cache 
*cachep,
  */
 void *__kmalloc(size_t, gfp_t);
 void *__kzalloc(size_t, gfp_t);
+void * __must_check krealloc(const void *, size_t, gfp_t);
 void kfree(const void *);
-unsigned int ksize(const void *);
+size_t ksize(const void *);
 
 /**
  * kcalloc - allocate memory for an array. The memory is set to zero.
diff --git a/mm/slab.c b/mm/slab.c
index 168bfe9..8b71a9c 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -3740,6 +3740,53 @@ 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.
  * @objp: The previously allocated object.
@@ -4481,7 +4528,7 @@ const struct seq_operations slabstats_op = {
  * allocated with either kmalloc() or kmem_cache_alloc(). The object
  * must not be freed during the duration of the call.
  */
-unsigned int ksize(const void *objp)
+size_t ksize(const void *objp)
 {
        if (unlikely(objp == NULL))
                return 0;
diff --git a/mm/slob.c b/mm/slob.c
index 5adc29c..03cce3d 100644
--- a/mm/slob.c
+++ b/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;
-
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to