LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 1/2] slab: introduce krealloc
@ 2007-02-22 12:40 Pekka J Enberg
  2007-02-22 15:20 ` Christoph Lameter
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pekka J Enberg @ 2007-02-22 12:40 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, mpm, clameter, jsipek

From: Pekka Enberg <penberg@cs.helsinki.fi>

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.

Cc: Josef Sipek <jsipek@fsl.cs.sunysb.edu>
Cc: Matt Mackall <mpm@selenic.com>
Cc: Christoph Lameter <clameter@sgi.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
---
 include/linux/slab.h |    1 +
 mm/slab.c            |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 mm/slob.c            |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)

Index: 2.6/include/linux/slab.h
===================================================================
--- 2.6.orig/include/linux/slab.h	2007-02-22 11:33:15.000000000 +0200
+++ 2.6/include/linux/slab.h	2007-02-22 11:33:39.000000000 +0200
@@ -72,6 +72,7 @@
  */
 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 *);
 
Index: 2.6/mm/slab.c
===================================================================
--- 2.6.orig/mm/slab.c	2007-02-22 11:33:15.000000000 +0200
+++ 2.6/mm/slab.c	2007-02-22 11:33:28.000000000 +0200
@@ -3737,6 +3737,53 @@
 #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.
Index: 2.6/mm/slob.c
===================================================================
--- 2.6.orig/mm/slob.c	2007-02-22 11:33:15.000000000 +0200
+++ 2.6/mm/slob.c	2007-02-22 11:33:28.000000000 +0200
@@ -190,6 +190,39 @@
 }
 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;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] slab: introduce krealloc
  2007-02-22 12:40 [PATCH 1/2] slab: introduce krealloc Pekka J Enberg
@ 2007-02-22 15:20 ` Christoph Lameter
  2007-02-22 15:21 ` Matt Mackall
  2007-02-22 18:19 ` Josef Sipek
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Lameter @ 2007-02-22 15:20 UTC (permalink / raw)
  To: Pekka J Enberg; +Cc: akpm, linux-kernel, mpm, jsipek

On Thu, 22 Feb 2007, Pekka J Enberg wrote:

> 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.

Acked-by: Christoph Lameter <clameter@sgi.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] slab: introduce krealloc
  2007-02-22 12:40 [PATCH 1/2] slab: introduce krealloc Pekka J Enberg
  2007-02-22 15:20 ` Christoph Lameter
@ 2007-02-22 15:21 ` Matt Mackall
  2007-02-22 18:19 ` Josef Sipek
  2 siblings, 0 replies; 4+ messages in thread
From: Matt Mackall @ 2007-02-22 15:21 UTC (permalink / raw)
  To: Pekka J Enberg; +Cc: akpm, linux-kernel, clameter, jsipek

On Thu, Feb 22, 2007 at 02:40:22PM +0200, Pekka J Enberg wrote:
> From: Pekka Enberg <penberg@cs.helsinki.fi>
> 
> 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.
> 
> Cc: Josef Sipek <jsipek@fsl.cs.sunysb.edu>
> Cc: Matt Mackall <mpm@selenic.com>
> Cc: Christoph Lameter <clameter@sgi.com>
> Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>

Acked-by: Matt Mackall <mpm@selenic.com>

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] slab: introduce krealloc
  2007-02-22 12:40 [PATCH 1/2] slab: introduce krealloc Pekka J Enberg
  2007-02-22 15:20 ` Christoph Lameter
  2007-02-22 15:21 ` Matt Mackall
@ 2007-02-22 18:19 ` Josef Sipek
  2 siblings, 0 replies; 4+ messages in thread
From: Josef Sipek @ 2007-02-22 18:19 UTC (permalink / raw)
  To: Pekka J Enberg; +Cc: akpm, linux-kernel, mpm, clameter

On Thu, Feb 22, 2007 at 02:40:22PM +0200, Pekka J Enberg wrote:
> From: Pekka Enberg <penberg@cs.helsinki.fi>
> 
> 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.

Acked-by: Josef 'Jeff' Sipek <jsipek@cs.sunysb.edu>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-02-22 18:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-22 12:40 [PATCH 1/2] slab: introduce krealloc Pekka J Enberg
2007-02-22 15:20 ` Christoph Lameter
2007-02-22 15:21 ` Matt Mackall
2007-02-22 18:19 ` Josef Sipek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).