[ALSA] bt87x: add Voodoo TV 200 whitelist entry
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / slob.c
bloba68255ba4553770f7c4050857a11661160ea3942
1 /*
2 * SLOB Allocator: Simple List Of Blocks
4 * Matt Mackall <mpm@selenic.com> 12/30/03
6 * How SLOB works:
8 * The core of SLOB is a traditional K&R style heap allocator, with
9 * support for returning aligned objects. The granularity of this
10 * allocator is 8 bytes on x86, though it's perhaps possible to reduce
11 * this to 4 if it's deemed worth the effort. The slob heap is a
12 * singly-linked list of pages from __get_free_page, grown on demand
13 * and allocation from the heap is currently first-fit.
15 * Above this is an implementation of kmalloc/kfree. Blocks returned
16 * from kmalloc are 8-byte aligned and prepended with a 8-byte header.
17 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
18 * __get_free_pages directly so that it can return page-aligned blocks
19 * and keeps a linked list of such pages and their orders. These
20 * objects are detected in kfree() by their page alignment.
22 * SLAB is emulated on top of SLOB by simply calling constructors and
23 * destructors for every SLAB allocation. Objects are returned with
24 * the 8-byte alignment unless the SLAB_MUST_HWCACHE_ALIGN flag is
25 * set, in which case the low-level allocator will fragment blocks to
26 * create the proper alignment. Again, objects of page-size or greater
27 * are allocated by calling __get_free_pages. As SLAB objects know
28 * their size, no separate size bookkeeping is necessary and there is
29 * essentially no allocation space overhead.
32 #include <linux/config.h>
33 #include <linux/slab.h>
34 #include <linux/mm.h>
35 #include <linux/cache.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/timer.h>
40 struct slob_block {
41 int units;
42 struct slob_block *next;
44 typedef struct slob_block slob_t;
46 #define SLOB_UNIT sizeof(slob_t)
47 #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
48 #define SLOB_ALIGN L1_CACHE_BYTES
50 struct bigblock {
51 int order;
52 void *pages;
53 struct bigblock *next;
55 typedef struct bigblock bigblock_t;
57 static slob_t arena = { .next = &arena, .units = 1 };
58 static slob_t *slobfree = &arena;
59 static bigblock_t *bigblocks;
60 static DEFINE_SPINLOCK(slob_lock);
61 static DEFINE_SPINLOCK(block_lock);
63 static void slob_free(void *b, int size);
65 static void *slob_alloc(size_t size, gfp_t gfp, int align)
67 slob_t *prev, *cur, *aligned = 0;
68 int delta = 0, units = SLOB_UNITS(size);
69 unsigned long flags;
71 spin_lock_irqsave(&slob_lock, flags);
72 prev = slobfree;
73 for (cur = prev->next; ; prev = cur, cur = cur->next) {
74 if (align) {
75 aligned = (slob_t *)ALIGN((unsigned long)cur, align);
76 delta = aligned - cur;
78 if (cur->units >= units + delta) { /* room enough? */
79 if (delta) { /* need to fragment head to align? */
80 aligned->units = cur->units - delta;
81 aligned->next = cur->next;
82 cur->next = aligned;
83 cur->units = delta;
84 prev = cur;
85 cur = aligned;
88 if (cur->units == units) /* exact fit? */
89 prev->next = cur->next; /* unlink */
90 else { /* fragment */
91 prev->next = cur + units;
92 prev->next->units = cur->units - units;
93 prev->next->next = cur->next;
94 cur->units = units;
97 slobfree = prev;
98 spin_unlock_irqrestore(&slob_lock, flags);
99 return cur;
101 if (cur == slobfree) {
102 spin_unlock_irqrestore(&slob_lock, flags);
104 if (size == PAGE_SIZE) /* trying to shrink arena? */
105 return 0;
107 cur = (slob_t *)__get_free_page(gfp);
108 if (!cur)
109 return 0;
111 slob_free(cur, PAGE_SIZE);
112 spin_lock_irqsave(&slob_lock, flags);
113 cur = slobfree;
118 static void slob_free(void *block, int size)
120 slob_t *cur, *b = (slob_t *)block;
121 unsigned long flags;
123 if (!block)
124 return;
126 if (size)
127 b->units = SLOB_UNITS(size);
129 /* Find reinsertion point */
130 spin_lock_irqsave(&slob_lock, flags);
131 for (cur = slobfree; !(b > cur && b < cur->next); cur = cur->next)
132 if (cur >= cur->next && (b > cur || b < cur->next))
133 break;
135 if (b + b->units == cur->next) {
136 b->units += cur->next->units;
137 b->next = cur->next->next;
138 } else
139 b->next = cur->next;
141 if (cur + cur->units == b) {
142 cur->units += b->units;
143 cur->next = b->next;
144 } else
145 cur->next = b;
147 slobfree = cur;
149 spin_unlock_irqrestore(&slob_lock, flags);
152 static int FASTCALL(find_order(int size));
153 static int fastcall find_order(int size)
155 int order = 0;
156 for ( ; size > 4096 ; size >>=1)
157 order++;
158 return order;
161 void *kmalloc(size_t size, gfp_t gfp)
163 slob_t *m;
164 bigblock_t *bb;
165 unsigned long flags;
167 if (size < PAGE_SIZE - SLOB_UNIT) {
168 m = slob_alloc(size + SLOB_UNIT, gfp, 0);
169 return m ? (void *)(m + 1) : 0;
172 bb = slob_alloc(sizeof(bigblock_t), gfp, 0);
173 if (!bb)
174 return 0;
176 bb->order = find_order(size);
177 bb->pages = (void *)__get_free_pages(gfp, bb->order);
179 if (bb->pages) {
180 spin_lock_irqsave(&block_lock, flags);
181 bb->next = bigblocks;
182 bigblocks = bb;
183 spin_unlock_irqrestore(&block_lock, flags);
184 return bb->pages;
187 slob_free(bb, sizeof(bigblock_t));
188 return 0;
191 EXPORT_SYMBOL(kmalloc);
193 void kfree(const void *block)
195 bigblock_t *bb, **last = &bigblocks;
196 unsigned long flags;
198 if (!block)
199 return;
201 if (!((unsigned long)block & (PAGE_SIZE-1))) {
202 /* might be on the big block list */
203 spin_lock_irqsave(&block_lock, flags);
204 for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) {
205 if (bb->pages == block) {
206 *last = bb->next;
207 spin_unlock_irqrestore(&block_lock, flags);
208 free_pages((unsigned long)block, bb->order);
209 slob_free(bb, sizeof(bigblock_t));
210 return;
213 spin_unlock_irqrestore(&block_lock, flags);
216 slob_free((slob_t *)block - 1, 0);
217 return;
220 EXPORT_SYMBOL(kfree);
222 unsigned int ksize(const void *block)
224 bigblock_t *bb;
225 unsigned long flags;
227 if (!block)
228 return 0;
230 if (!((unsigned long)block & (PAGE_SIZE-1))) {
231 spin_lock_irqsave(&block_lock, flags);
232 for (bb = bigblocks; bb; bb = bb->next)
233 if (bb->pages == block) {
234 spin_unlock_irqrestore(&slob_lock, flags);
235 return PAGE_SIZE << bb->order;
237 spin_unlock_irqrestore(&block_lock, flags);
240 return ((slob_t *)block - 1)->units * SLOB_UNIT;
243 struct kmem_cache {
244 unsigned int size, align;
245 const char *name;
246 void (*ctor)(void *, struct kmem_cache *, unsigned long);
247 void (*dtor)(void *, struct kmem_cache *, unsigned long);
250 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
251 size_t align, unsigned long flags,
252 void (*ctor)(void*, struct kmem_cache *, unsigned long),
253 void (*dtor)(void*, struct kmem_cache *, unsigned long))
255 struct kmem_cache *c;
257 c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
259 if (c) {
260 c->name = name;
261 c->size = size;
262 c->ctor = ctor;
263 c->dtor = dtor;
264 /* ignore alignment unless it's forced */
265 c->align = (flags & SLAB_MUST_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
266 if (c->align < align)
267 c->align = align;
270 return c;
272 EXPORT_SYMBOL(kmem_cache_create);
274 int kmem_cache_destroy(struct kmem_cache *c)
276 slob_free(c, sizeof(struct kmem_cache));
277 return 0;
279 EXPORT_SYMBOL(kmem_cache_destroy);
281 void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
283 void *b;
285 if (c->size < PAGE_SIZE)
286 b = slob_alloc(c->size, flags, c->align);
287 else
288 b = (void *)__get_free_pages(flags, find_order(c->size));
290 if (c->ctor)
291 c->ctor(b, c, SLAB_CTOR_CONSTRUCTOR);
293 return b;
295 EXPORT_SYMBOL(kmem_cache_alloc);
297 void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
299 void *ret = kmem_cache_alloc(c, flags);
300 if (ret)
301 memset(ret, 0, c->size);
303 return ret;
305 EXPORT_SYMBOL(kmem_cache_zalloc);
307 void kmem_cache_free(struct kmem_cache *c, void *b)
309 if (c->dtor)
310 c->dtor(b, c, 0);
312 if (c->size < PAGE_SIZE)
313 slob_free(b, c->size);
314 else
315 free_pages((unsigned long)b, find_order(c->size));
317 EXPORT_SYMBOL(kmem_cache_free);
319 unsigned int kmem_cache_size(struct kmem_cache *c)
321 return c->size;
323 EXPORT_SYMBOL(kmem_cache_size);
325 const char *kmem_cache_name(struct kmem_cache *c)
327 return c->name;
329 EXPORT_SYMBOL(kmem_cache_name);
331 static struct timer_list slob_timer = TIMER_INITIALIZER(
332 (void (*)(unsigned long))kmem_cache_init, 0, 0);
334 void kmem_cache_init(void)
336 void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1);
338 if (p)
339 free_page((unsigned long)p);
341 mod_timer(&slob_timer, jiffies + HZ);
344 atomic_t slab_reclaim_pages = ATOMIC_INIT(0);
345 EXPORT_SYMBOL(slab_reclaim_pages);
347 #ifdef CONFIG_SMP
349 void *__alloc_percpu(size_t size)
351 int i;
352 struct percpu_data *pdata = kmalloc(sizeof (*pdata), GFP_KERNEL);
354 if (!pdata)
355 return NULL;
357 for_each_possible_cpu(i) {
358 pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
359 if (!pdata->ptrs[i])
360 goto unwind_oom;
361 memset(pdata->ptrs[i], 0, size);
364 /* Catch derefs w/o wrappers */
365 return (void *) (~(unsigned long) pdata);
367 unwind_oom:
368 while (--i >= 0) {
369 if (!cpu_possible(i))
370 continue;
371 kfree(pdata->ptrs[i]);
373 kfree(pdata);
374 return NULL;
376 EXPORT_SYMBOL(__alloc_percpu);
378 void
379 free_percpu(const void *objp)
381 int i;
382 struct percpu_data *p = (struct percpu_data *) (~(unsigned long) objp);
384 for_each_possible_cpu(i)
385 kfree(p->ptrs[i]);
387 kfree(p);
389 EXPORT_SYMBOL(free_percpu);
391 #endif