All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pekka J Enberg <penberg@cs.helsinki.fi>
To: akpm@linux-foundation.org
Cc: clameter@sgi.com, linux-kernel@vger.kernel.org,
	jsipek@fsl.cs.sunysb.edu, unionfs@filesystems.org,
	bunk@stusta.de, hooanon05@yahoo.co.jp
Subject: [PATCH 1/3] slab: introduce krealloc
Date: Wed, 21 Feb 2007 10:06:20 +0200 (EET)	[thread overview]
Message-ID: <Pine.LNX.4.64.0702211003290.8172@sbz-30.cs.Helsinki.FI> (raw)

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

Introduce krealloc() for reallocating memory while preserving contents.

Cc: Christoph Lameter <clameter@sgi.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
---
 include/linux/slab.h |    1 +
 mm/util.c            |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Index: 2.6/include/linux/slab.h
===================================================================
--- 2.6.orig/include/linux/slab.h	2007-02-21 09:05:08.000000000 +0200
+++ 2.6/include/linux/slab.h	2007-02-21 09:05:47.000000000 +0200
@@ -72,6 +72,7 @@
  */
 void *__kmalloc(size_t, gfp_t);
 void *__kzalloc(size_t, gfp_t);
+void *krealloc(const void *, size_t, gfp_t);
 void kfree(const void *);
 unsigned int ksize(const void *);
 
Index: 2.6/mm/util.c
===================================================================
--- 2.6.orig/mm/util.c	2007-02-21 09:05:49.000000000 +0200
+++ 2.6/mm/util.c	2007-02-21 09:34:32.000000000 +0200
@@ -18,6 +18,40 @@
 }
 EXPORT_SYMBOL(__kzalloc);
 
+/**
+ * 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)
+		return NULL;
+
+	memcpy(ret, p, min(new_size, ksize(p)));
+	kfree(p);
+	return ret;
+}
+EXPORT_SYMBOL(krealloc);
+
 /*
  * kstrdup - allocate space for and copy an existing string
  *

             reply	other threads:[~2007-02-21  8:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-21  8:06 Pekka J Enberg [this message]
2007-02-21  9:24 ` [PATCH 1/3] slab: introduce krealloc Arjan van de Ven
2007-02-21  9:33   ` Pekka Enberg
2007-02-21  9:36     ` Arjan van de Ven
2007-02-21 17:51     ` Christoph Lameter
2007-02-21 17:53 ` Christoph Lameter
2007-02-21 18:21   ` Pekka Enberg
2007-02-21 18:27     ` Christoph Lameter
2007-02-21 18:37       ` Pekka Enberg
2007-02-21 18:48         ` Christoph Lameter
2007-02-21 19:19           ` Pekka Enberg
2007-02-21 19:22             ` Christoph Lameter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0702211003290.8172@sbz-30.cs.Helsinki.FI \
    --to=penberg@cs.helsinki.fi \
    --cc=akpm@linux-foundation.org \
    --cc=bunk@stusta.de \
    --cc=clameter@sgi.com \
    --cc=hooanon05@yahoo.co.jp \
    --cc=jsipek@fsl.cs.sunysb.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=unionfs@filesystems.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.