diff options
author | Pekka Enberg <penberg@cs.helsinki.fi> | 2007-05-06 17:48:40 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-05-07 15:12:50 -0400 |
commit | fd76bab2fa6d8f3ef6b326a4c6ae442fa21d30a4 (patch) | |
tree | 66f310ab9d7cdadfb79486700f1e01df7923ec14 /mm/slob.c | |
parent | e3ebadd95cb621e2c7436f3d3646447ac9d5c16d (diff) |
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.
[akpm@linux-foundation.org: fix warnings]
Acked-by: Josef Sipek <jsipek@fsl.cs.sunysb.edu>
Acked-by: Matt Mackall <mpm@selenic.com>
Acked-by: Christoph Lameter <clameter@sgi.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/slob.c')
-rw-r--r-- | mm/slob.c | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -190,6 +190,39 @@ void *__kmalloc(size_t size, gfp_t gfp) | |||
190 | } | 190 | } |
191 | EXPORT_SYMBOL(__kmalloc); | 191 | EXPORT_SYMBOL(__kmalloc); |
192 | 192 | ||
193 | /** | ||
194 | * krealloc - reallocate memory. The contents will remain unchanged. | ||
195 | * | ||
196 | * @p: object to reallocate memory for. | ||
197 | * @new_size: how many bytes of memory are required. | ||
198 | * @flags: the type of memory to allocate. | ||
199 | * | ||
200 | * The contents of the object pointed to are preserved up to the | ||
201 | * lesser of the new and old sizes. If @p is %NULL, krealloc() | ||
202 | * behaves exactly like kmalloc(). If @size is 0 and @p is not a | ||
203 | * %NULL pointer, the object pointed to is freed. | ||
204 | */ | ||
205 | void *krealloc(const void *p, size_t new_size, gfp_t flags) | ||
206 | { | ||
207 | void *ret; | ||
208 | |||
209 | if (unlikely(!p)) | ||
210 | return kmalloc_track_caller(new_size, flags); | ||
211 | |||
212 | if (unlikely(!new_size)) { | ||
213 | kfree(p); | ||
214 | return NULL; | ||
215 | } | ||
216 | |||
217 | ret = kmalloc_track_caller(new_size, flags); | ||
218 | if (ret) { | ||
219 | memcpy(ret, p, min(new_size, ksize(p))); | ||
220 | kfree(p); | ||
221 | } | ||
222 | return ret; | ||
223 | } | ||
224 | EXPORT_SYMBOL(krealloc); | ||
225 | |||
193 | void kfree(const void *block) | 226 | void kfree(const void *block) |
194 | { | 227 | { |
195 | bigblock_t *bb, **last = &bigblocks; | 228 | bigblock_t *bb, **last = &bigblocks; |
@@ -219,7 +252,7 @@ void kfree(const void *block) | |||
219 | 252 | ||
220 | EXPORT_SYMBOL(kfree); | 253 | EXPORT_SYMBOL(kfree); |
221 | 254 | ||
222 | unsigned int ksize(const void *block) | 255 | size_t ksize(const void *block) |
223 | { | 256 | { |
224 | bigblock_t *bb; | 257 | bigblock_t *bb; |
225 | unsigned long flags; | 258 | unsigned long flags; |