blob: 8ee64fed2bb56db97792b7fa4a7d8dc665123979 [file] [log] [blame]
Matt Mackall10cef602006-01-08 01:01:45 -08001/*
2 * SLOB Allocator: Simple List Of Blocks
3 *
4 * Matt Mackall <mpm@selenic.com> 12/30/03
5 *
6 * How SLOB works:
7 *
8 * The core of SLOB is a traditional K&R style heap allocator, with
9 * support for returning aligned objects. The granularity of this
Nick Piggin95b35122007-07-15 23:38:07 -070010 * allocator is 4 bytes on 32-bit and 8 bytes on 64-bit, though it
11 * could be as low as 2 if the compiler alignment requirements allow.
12 *
13 * The slob heap is a linked list of pages from __get_free_page, and
14 * within each page, there is a singly-linked list of free blocks (slob_t).
15 * The heap is grown on demand and allocation from the heap is currently
16 * first-fit.
Matt Mackall10cef602006-01-08 01:01:45 -080017 *
18 * Above this is an implementation of kmalloc/kfree. Blocks returned
Nick Piggin95b35122007-07-15 23:38:07 -070019 * from kmalloc are 4-byte aligned and prepended with a 4-byte header.
Matt Mackall10cef602006-01-08 01:01:45 -080020 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
21 * __get_free_pages directly so that it can return page-aligned blocks
22 * and keeps a linked list of such pages and their orders. These
23 * objects are detected in kfree() by their page alignment.
24 *
25 * SLAB is emulated on top of SLOB by simply calling constructors and
Nick Piggin95b35122007-07-15 23:38:07 -070026 * destructors for every SLAB allocation. Objects are returned with the
27 * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
28 * case the low-level allocator will fragment blocks to create the proper
29 * alignment. Again, objects of page-size or greater are allocated by
30 * calling __get_free_pages. As SLAB objects know their size, no separate
31 * size bookkeeping is necessary and there is essentially no allocation
32 * space overhead.
Matt Mackall10cef602006-01-08 01:01:45 -080033 */
34
Nick Piggin95b35122007-07-15 23:38:07 -070035#include <linux/kernel.h>
Matt Mackall10cef602006-01-08 01:01:45 -080036#include <linux/slab.h>
37#include <linux/mm.h>
38#include <linux/cache.h>
39#include <linux/init.h>
40#include <linux/module.h>
Nick Pigginafc0ced2007-05-16 22:10:49 -070041#include <linux/rcupdate.h>
Nick Piggin95b35122007-07-15 23:38:07 -070042#include <linux/list.h>
43#include <asm/atomic.h>
Matt Mackall10cef602006-01-08 01:01:45 -080044
Nick Piggin95b35122007-07-15 23:38:07 -070045/* SLOB_MIN_ALIGN == sizeof(long) */
46#if BITS_PER_BYTE == 32
47#define SLOB_MIN_ALIGN 4
48#else
49#define SLOB_MIN_ALIGN 8
50#endif
51
52/*
53 * slob_block has a field 'units', which indicates size of block if +ve,
54 * or offset of next block if -ve (in SLOB_UNITs).
55 *
56 * Free blocks of size 1 unit simply contain the offset of the next block.
57 * Those with larger size contain their size in the first SLOB_UNIT of
58 * memory, and the offset of the next free block in the second SLOB_UNIT.
59 */
60#if PAGE_SIZE <= (32767 * SLOB_MIN_ALIGN)
61typedef s16 slobidx_t;
62#else
63typedef s32 slobidx_t;
64#endif
65
66/*
67 * Align struct slob_block to long for now, but can some embedded
68 * architectures get away with less?
69 */
Matt Mackall10cef602006-01-08 01:01:45 -080070struct slob_block {
Nick Piggin95b35122007-07-15 23:38:07 -070071 slobidx_t units;
72} __attribute__((aligned(SLOB_MIN_ALIGN)));
Matt Mackall10cef602006-01-08 01:01:45 -080073typedef struct slob_block slob_t;
74
Nick Piggin95b35122007-07-15 23:38:07 -070075/*
76 * We use struct page fields to manage some slob allocation aspects,
77 * however to avoid the horrible mess in include/linux/mm_types.h, we'll
78 * just define our own struct page type variant here.
79 */
80struct slob_page {
81 union {
82 struct {
83 unsigned long flags; /* mandatory */
84 atomic_t _count; /* mandatory */
85 slobidx_t units; /* free units left in page */
86 unsigned long pad[2];
87 slob_t *free; /* first free slob_t in page */
88 struct list_head list; /* linked list of free pages */
89 };
90 struct page page;
91 };
92};
93static inline void struct_slob_page_wrong_size(void)
94{ BUILD_BUG_ON(sizeof(struct slob_page) != sizeof(struct page)); }
95
96/*
97 * free_slob_page: call before a slob_page is returned to the page allocator.
98 */
99static inline void free_slob_page(struct slob_page *sp)
100{
101 reset_page_mapcount(&sp->page);
102 sp->page.mapping = NULL;
103}
104
105/*
106 * All (partially) free slob pages go on this list.
107 */
108static LIST_HEAD(free_slob_pages);
109
110/*
111 * slob_page: True for all slob pages (false for bigblock pages)
112 */
113static inline int slob_page(struct slob_page *sp)
114{
115 return test_bit(PG_active, &sp->flags);
116}
117
118static inline void set_slob_page(struct slob_page *sp)
119{
120 __set_bit(PG_active, &sp->flags);
121}
122
123static inline void clear_slob_page(struct slob_page *sp)
124{
125 __clear_bit(PG_active, &sp->flags);
126}
127
128/*
129 * slob_page_free: true for pages on free_slob_pages list.
130 */
131static inline int slob_page_free(struct slob_page *sp)
132{
133 return test_bit(PG_private, &sp->flags);
134}
135
136static inline void set_slob_page_free(struct slob_page *sp)
137{
138 list_add(&sp->list, &free_slob_pages);
139 __set_bit(PG_private, &sp->flags);
140}
141
142static inline void clear_slob_page_free(struct slob_page *sp)
143{
144 list_del(&sp->list);
145 __clear_bit(PG_private, &sp->flags);
146}
147
Matt Mackall10cef602006-01-08 01:01:45 -0800148#define SLOB_UNIT sizeof(slob_t)
149#define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
150#define SLOB_ALIGN L1_CACHE_BYTES
151
Nick Pigginafc0ced2007-05-16 22:10:49 -0700152/*
153 * struct slob_rcu is inserted at the tail of allocated slob blocks, which
154 * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
155 * the block using call_rcu.
156 */
157struct slob_rcu {
158 struct rcu_head head;
159 int size;
160};
161
Nick Piggin95b35122007-07-15 23:38:07 -0700162/*
163 * slob_lock protects all slob allocator structures.
164 */
Matt Mackall10cef602006-01-08 01:01:45 -0800165static DEFINE_SPINLOCK(slob_lock);
Matt Mackall10cef602006-01-08 01:01:45 -0800166
Nick Piggin95b35122007-07-15 23:38:07 -0700167/*
168 * Encode the given size and next info into a free slob block s.
169 */
170static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
171{
172 slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
173 slobidx_t offset = next - base;
Dimitri Gorokhovikbcb4ddb2006-12-29 16:48:28 -0800174
Nick Piggin95b35122007-07-15 23:38:07 -0700175 if (size > 1) {
176 s[0].units = size;
177 s[1].units = offset;
178 } else
179 s[0].units = -offset;
180}
Matt Mackall10cef602006-01-08 01:01:45 -0800181
Nick Piggin95b35122007-07-15 23:38:07 -0700182/*
183 * Return the size of a slob block.
184 */
185static slobidx_t slob_units(slob_t *s)
186{
187 if (s->units > 0)
188 return s->units;
189 return 1;
190}
191
192/*
193 * Return the next free slob block pointer after this one.
194 */
195static slob_t *slob_next(slob_t *s)
196{
197 slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
198 slobidx_t next;
199
200 if (s[0].units < 0)
201 next = -s[0].units;
202 else
203 next = s[1].units;
204 return base+next;
205}
206
207/*
208 * Returns true if s is the last free block in its page.
209 */
210static int slob_last(slob_t *s)
211{
212 return !((unsigned long)slob_next(s) & ~PAGE_MASK);
213}
214
215/*
216 * Allocate a slob block within a given slob_page sp.
217 */
218static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
Matt Mackall10cef602006-01-08 01:01:45 -0800219{
220 slob_t *prev, *cur, *aligned = 0;
221 int delta = 0, units = SLOB_UNITS(size);
Matt Mackall10cef602006-01-08 01:01:45 -0800222
Nick Piggin95b35122007-07-15 23:38:07 -0700223 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
224 slobidx_t avail = slob_units(cur);
225
Matt Mackall10cef602006-01-08 01:01:45 -0800226 if (align) {
227 aligned = (slob_t *)ALIGN((unsigned long)cur, align);
228 delta = aligned - cur;
229 }
Nick Piggin95b35122007-07-15 23:38:07 -0700230 if (avail >= units + delta) { /* room enough? */
231 slob_t *next;
232
Matt Mackall10cef602006-01-08 01:01:45 -0800233 if (delta) { /* need to fragment head to align? */
Nick Piggin95b35122007-07-15 23:38:07 -0700234 next = slob_next(cur);
235 set_slob(aligned, avail - delta, next);
236 set_slob(cur, delta, aligned);
Matt Mackall10cef602006-01-08 01:01:45 -0800237 prev = cur;
238 cur = aligned;
Nick Piggin95b35122007-07-15 23:38:07 -0700239 avail = slob_units(cur);
Matt Mackall10cef602006-01-08 01:01:45 -0800240 }
241
Nick Piggin95b35122007-07-15 23:38:07 -0700242 next = slob_next(cur);
243 if (avail == units) { /* exact fit? unlink. */
244 if (prev)
245 set_slob(prev, slob_units(prev), next);
246 else
247 sp->free = next;
248 } else { /* fragment */
249 if (prev)
250 set_slob(prev, slob_units(prev), cur + units);
251 else
252 sp->free = cur + units;
253 set_slob(cur + units, avail - units, next);
Matt Mackall10cef602006-01-08 01:01:45 -0800254 }
255
Nick Piggin95b35122007-07-15 23:38:07 -0700256 sp->units -= units;
257 if (!sp->units)
258 clear_slob_page_free(sp);
Matt Mackall10cef602006-01-08 01:01:45 -0800259 return cur;
260 }
Nick Piggin95b35122007-07-15 23:38:07 -0700261 if (slob_last(cur))
262 return NULL;
Matt Mackall10cef602006-01-08 01:01:45 -0800263 }
264}
265
Nick Piggin95b35122007-07-15 23:38:07 -0700266/*
267 * slob_alloc: entry point into the slob allocator.
268 */
269static void *slob_alloc(size_t size, gfp_t gfp, int align)
270{
271 struct slob_page *sp;
272 slob_t *b = NULL;
273 unsigned long flags;
274
275 spin_lock_irqsave(&slob_lock, flags);
276 /* Iterate through each partially free page, try to find room */
277 list_for_each_entry(sp, &free_slob_pages, list) {
278 if (sp->units >= SLOB_UNITS(size)) {
279 b = slob_page_alloc(sp, size, align);
280 if (b)
281 break;
282 }
283 }
284 spin_unlock_irqrestore(&slob_lock, flags);
285
286 /* Not enough space: must allocate a new page */
287 if (!b) {
288 b = (slob_t *)__get_free_page(gfp);
289 if (!b)
290 return 0;
291 sp = (struct slob_page *)virt_to_page(b);
292 set_slob_page(sp);
293
294 spin_lock_irqsave(&slob_lock, flags);
295 sp->units = SLOB_UNITS(PAGE_SIZE);
296 sp->free = b;
297 INIT_LIST_HEAD(&sp->list);
298 set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
299 set_slob_page_free(sp);
300 b = slob_page_alloc(sp, size, align);
301 BUG_ON(!b);
302 spin_unlock_irqrestore(&slob_lock, flags);
303 }
304 return b;
305}
306
307/*
308 * slob_free: entry point into the slob allocator.
309 */
Matt Mackall10cef602006-01-08 01:01:45 -0800310static void slob_free(void *block, int size)
311{
Nick Piggin95b35122007-07-15 23:38:07 -0700312 struct slob_page *sp;
313 slob_t *prev, *next, *b = (slob_t *)block;
314 slobidx_t units;
Matt Mackall10cef602006-01-08 01:01:45 -0800315 unsigned long flags;
316
317 if (!block)
318 return;
Nick Piggin95b35122007-07-15 23:38:07 -0700319 BUG_ON(!size);
Matt Mackall10cef602006-01-08 01:01:45 -0800320
Nick Piggin95b35122007-07-15 23:38:07 -0700321 sp = (struct slob_page *)virt_to_page(block);
322 units = SLOB_UNITS(size);
Matt Mackall10cef602006-01-08 01:01:45 -0800323
Matt Mackall10cef602006-01-08 01:01:45 -0800324 spin_lock_irqsave(&slob_lock, flags);
Matt Mackall10cef602006-01-08 01:01:45 -0800325
Nick Piggin95b35122007-07-15 23:38:07 -0700326 if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
327 /* Go directly to page allocator. Do not pass slob allocator */
328 if (slob_page_free(sp))
329 clear_slob_page_free(sp);
330 clear_slob_page(sp);
331 free_slob_page(sp);
332 free_page((unsigned long)b);
333 goto out;
334 }
Matt Mackall10cef602006-01-08 01:01:45 -0800335
Nick Piggin95b35122007-07-15 23:38:07 -0700336 if (!slob_page_free(sp)) {
337 /* This slob page is about to become partially free. Easy! */
338 sp->units = units;
339 sp->free = b;
340 set_slob(b, units,
341 (void *)((unsigned long)(b +
342 SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
343 set_slob_page_free(sp);
344 goto out;
345 }
Matt Mackall10cef602006-01-08 01:01:45 -0800346
Nick Piggin95b35122007-07-15 23:38:07 -0700347 /*
348 * Otherwise the page is already partially free, so find reinsertion
349 * point.
350 */
351 sp->units += units;
Matt Mackall10cef602006-01-08 01:01:45 -0800352
Nick Piggin95b35122007-07-15 23:38:07 -0700353 if (b < sp->free) {
354 set_slob(b, units, sp->free);
355 sp->free = b;
356 } else {
357 prev = sp->free;
358 next = slob_next(prev);
359 while (b > next) {
360 prev = next;
361 next = slob_next(prev);
362 }
363
364 if (!slob_last(prev) && b + units == next) {
365 units += slob_units(next);
366 set_slob(b, units, slob_next(next));
367 } else
368 set_slob(b, units, next);
369
370 if (prev + slob_units(prev) == b) {
371 units = slob_units(b) + slob_units(prev);
372 set_slob(prev, units, slob_next(b));
373 } else
374 set_slob(prev, slob_units(prev), b);
375 }
376out:
Matt Mackall10cef602006-01-08 01:01:45 -0800377 spin_unlock_irqrestore(&slob_lock, flags);
378}
379
Nick Piggin95b35122007-07-15 23:38:07 -0700380/*
381 * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
382 */
383
384struct bigblock {
385 int order;
386 void *pages;
387 struct bigblock *next;
388};
389typedef struct bigblock bigblock_t;
390
391static bigblock_t *bigblocks;
392
393static DEFINE_SPINLOCK(block_lock);
394
395
Christoph Lameter2e892f42006-12-13 00:34:23 -0800396void *__kmalloc(size_t size, gfp_t gfp)
Matt Mackall10cef602006-01-08 01:01:45 -0800397{
398 slob_t *m;
399 bigblock_t *bb;
400 unsigned long flags;
401
402 if (size < PAGE_SIZE - SLOB_UNIT) {
403 m = slob_alloc(size + SLOB_UNIT, gfp, 0);
Nick Piggin95b35122007-07-15 23:38:07 -0700404 if (m)
405 m->units = size;
406 return m+1;
Matt Mackall10cef602006-01-08 01:01:45 -0800407 }
408
409 bb = slob_alloc(sizeof(bigblock_t), gfp, 0);
410 if (!bb)
411 return 0;
412
Akinobu Mita4ab688c2007-05-06 14:50:19 -0700413 bb->order = get_order(size);
Matt Mackall10cef602006-01-08 01:01:45 -0800414 bb->pages = (void *)__get_free_pages(gfp, bb->order);
415
416 if (bb->pages) {
417 spin_lock_irqsave(&block_lock, flags);
418 bb->next = bigblocks;
419 bigblocks = bb;
420 spin_unlock_irqrestore(&block_lock, flags);
421 return bb->pages;
422 }
423
424 slob_free(bb, sizeof(bigblock_t));
425 return 0;
426}
Christoph Lameter2e892f42006-12-13 00:34:23 -0800427EXPORT_SYMBOL(__kmalloc);
Matt Mackall10cef602006-01-08 01:01:45 -0800428
Pekka Enbergfd76bab2007-05-06 14:48:40 -0700429/**
430 * krealloc - reallocate memory. The contents will remain unchanged.
431 *
432 * @p: object to reallocate memory for.
433 * @new_size: how many bytes of memory are required.
434 * @flags: the type of memory to allocate.
435 *
436 * The contents of the object pointed to are preserved up to the
437 * lesser of the new and old sizes. If @p is %NULL, krealloc()
438 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
439 * %NULL pointer, the object pointed to is freed.
440 */
441void *krealloc(const void *p, size_t new_size, gfp_t flags)
442{
443 void *ret;
444
445 if (unlikely(!p))
446 return kmalloc_track_caller(new_size, flags);
447
448 if (unlikely(!new_size)) {
449 kfree(p);
450 return NULL;
451 }
452
453 ret = kmalloc_track_caller(new_size, flags);
454 if (ret) {
455 memcpy(ret, p, min(new_size, ksize(p)));
456 kfree(p);
457 }
458 return ret;
459}
460EXPORT_SYMBOL(krealloc);
461
Matt Mackall10cef602006-01-08 01:01:45 -0800462void kfree(const void *block)
463{
Nick Piggin95b35122007-07-15 23:38:07 -0700464 struct slob_page *sp;
465 slob_t *m;
Matt Mackall10cef602006-01-08 01:01:45 -0800466 bigblock_t *bb, **last = &bigblocks;
467 unsigned long flags;
468
469 if (!block)
470 return;
471
Nick Piggin95b35122007-07-15 23:38:07 -0700472 sp = (struct slob_page *)virt_to_page(block);
473 if (!slob_page(sp)) {
474 /* on the big block list */
Matt Mackall10cef602006-01-08 01:01:45 -0800475 spin_lock_irqsave(&block_lock, flags);
476 for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) {
477 if (bb->pages == block) {
478 *last = bb->next;
479 spin_unlock_irqrestore(&block_lock, flags);
480 free_pages((unsigned long)block, bb->order);
481 slob_free(bb, sizeof(bigblock_t));
482 return;
483 }
484 }
485 spin_unlock_irqrestore(&block_lock, flags);
Nick Piggin95b35122007-07-15 23:38:07 -0700486 WARN_ON(1);
487 return;
Matt Mackall10cef602006-01-08 01:01:45 -0800488 }
489
Nick Piggin95b35122007-07-15 23:38:07 -0700490 m = (slob_t *)block - 1;
491 slob_free(m, m->units + SLOB_UNIT);
Matt Mackall10cef602006-01-08 01:01:45 -0800492 return;
493}
494
495EXPORT_SYMBOL(kfree);
496
Pekka Enbergfd76bab2007-05-06 14:48:40 -0700497size_t ksize(const void *block)
Matt Mackall10cef602006-01-08 01:01:45 -0800498{
Nick Piggin95b35122007-07-15 23:38:07 -0700499 struct slob_page *sp;
Matt Mackall10cef602006-01-08 01:01:45 -0800500 bigblock_t *bb;
501 unsigned long flags;
502
503 if (!block)
504 return 0;
505
Nick Piggin95b35122007-07-15 23:38:07 -0700506 sp = (struct slob_page *)virt_to_page(block);
507 if (!slob_page(sp)) {
Matt Mackall10cef602006-01-08 01:01:45 -0800508 spin_lock_irqsave(&block_lock, flags);
509 for (bb = bigblocks; bb; bb = bb->next)
510 if (bb->pages == block) {
511 spin_unlock_irqrestore(&slob_lock, flags);
512 return PAGE_SIZE << bb->order;
513 }
514 spin_unlock_irqrestore(&block_lock, flags);
515 }
516
Nick Piggin95b35122007-07-15 23:38:07 -0700517 return ((slob_t *)block - 1)->units + SLOB_UNIT;
Matt Mackall10cef602006-01-08 01:01:45 -0800518}
519
520struct kmem_cache {
521 unsigned int size, align;
Nick Pigginafc0ced2007-05-16 22:10:49 -0700522 unsigned long flags;
Matt Mackall10cef602006-01-08 01:01:45 -0800523 const char *name;
524 void (*ctor)(void *, struct kmem_cache *, unsigned long);
Matt Mackall10cef602006-01-08 01:01:45 -0800525};
526
527struct kmem_cache *kmem_cache_create(const char *name, size_t size,
528 size_t align, unsigned long flags,
529 void (*ctor)(void*, struct kmem_cache *, unsigned long),
530 void (*dtor)(void*, struct kmem_cache *, unsigned long))
531{
532 struct kmem_cache *c;
533
534 c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
535
536 if (c) {
537 c->name = name;
538 c->size = size;
Nick Pigginafc0ced2007-05-16 22:10:49 -0700539 if (flags & SLAB_DESTROY_BY_RCU) {
Nick Pigginafc0ced2007-05-16 22:10:49 -0700540 /* leave room for rcu footer at the end of object */
541 c->size += sizeof(struct slob_rcu);
542 }
543 c->flags = flags;
Matt Mackall10cef602006-01-08 01:01:45 -0800544 c->ctor = ctor;
Matt Mackall10cef602006-01-08 01:01:45 -0800545 /* ignore alignment unless it's forced */
Christoph Lameter5af60832007-05-06 14:49:56 -0700546 c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
Matt Mackall10cef602006-01-08 01:01:45 -0800547 if (c->align < align)
548 c->align = align;
Akinobu Mitabc0055a2007-05-06 14:49:52 -0700549 } else if (flags & SLAB_PANIC)
550 panic("Cannot create slab cache %s\n", name);
Matt Mackall10cef602006-01-08 01:01:45 -0800551
552 return c;
553}
554EXPORT_SYMBOL(kmem_cache_create);
555
Alexey Dobriyan133d2052006-09-27 01:49:41 -0700556void kmem_cache_destroy(struct kmem_cache *c)
Matt Mackall10cef602006-01-08 01:01:45 -0800557{
558 slob_free(c, sizeof(struct kmem_cache));
Matt Mackall10cef602006-01-08 01:01:45 -0800559}
560EXPORT_SYMBOL(kmem_cache_destroy);
561
562void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
563{
564 void *b;
565
566 if (c->size < PAGE_SIZE)
567 b = slob_alloc(c->size, flags, c->align);
568 else
Akinobu Mita4ab688c2007-05-06 14:50:19 -0700569 b = (void *)__get_free_pages(flags, get_order(c->size));
Matt Mackall10cef602006-01-08 01:01:45 -0800570
571 if (c->ctor)
Christoph Lametera35afb82007-05-16 22:10:57 -0700572 c->ctor(b, c, 0);
Matt Mackall10cef602006-01-08 01:01:45 -0800573
574 return b;
575}
576EXPORT_SYMBOL(kmem_cache_alloc);
577
Pekka Enberga8c0f9a2006-03-25 03:06:42 -0800578void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
579{
580 void *ret = kmem_cache_alloc(c, flags);
581 if (ret)
582 memset(ret, 0, c->size);
583
584 return ret;
585}
586EXPORT_SYMBOL(kmem_cache_zalloc);
587
Nick Pigginafc0ced2007-05-16 22:10:49 -0700588static void __kmem_cache_free(void *b, int size)
589{
590 if (size < PAGE_SIZE)
591 slob_free(b, size);
592 else
593 free_pages((unsigned long)b, get_order(size));
594}
595
596static void kmem_rcu_free(struct rcu_head *head)
597{
598 struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
599 void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
600
601 __kmem_cache_free(b, slob_rcu->size);
602}
603
Matt Mackall10cef602006-01-08 01:01:45 -0800604void kmem_cache_free(struct kmem_cache *c, void *b)
605{
Nick Pigginafc0ced2007-05-16 22:10:49 -0700606 if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
607 struct slob_rcu *slob_rcu;
608 slob_rcu = b + (c->size - sizeof(struct slob_rcu));
609 INIT_RCU_HEAD(&slob_rcu->head);
610 slob_rcu->size = c->size;
611 call_rcu(&slob_rcu->head, kmem_rcu_free);
612 } else {
Nick Pigginafc0ced2007-05-16 22:10:49 -0700613 __kmem_cache_free(b, c->size);
614 }
Matt Mackall10cef602006-01-08 01:01:45 -0800615}
616EXPORT_SYMBOL(kmem_cache_free);
617
618unsigned int kmem_cache_size(struct kmem_cache *c)
619{
620 return c->size;
621}
622EXPORT_SYMBOL(kmem_cache_size);
623
624const char *kmem_cache_name(struct kmem_cache *c)
625{
626 return c->name;
627}
628EXPORT_SYMBOL(kmem_cache_name);
629
Christoph Lameter2e892f42006-12-13 00:34:23 -0800630int kmem_cache_shrink(struct kmem_cache *d)
631{
632 return 0;
633}
634EXPORT_SYMBOL(kmem_cache_shrink);
635
Christoph Lameter55935a32006-12-13 00:34:24 -0800636int kmem_ptr_validate(struct kmem_cache *a, const void *b)
Christoph Lameter2e892f42006-12-13 00:34:23 -0800637{
638 return 0;
639}
640
Dimitri Gorokhovikbcb4ddb2006-12-29 16:48:28 -0800641void __init kmem_cache_init(void)
642{
Matt Mackall10cef602006-01-08 01:01:45 -0800643}