OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [mmnommu/] [kmalloc.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/mm/kmalloc.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds & Roger Wolff.
5
 *
6
 *  Written by R.E. Wolff Sept/Oct '93.
7
 *
8
 */
9
 
10
/*
11
 * Modified by Alex Bligh (alex@cconcepts.co.uk) 4 Apr 1994 to use multiple
12
 * pages. So for 'page' throughout, read 'area'.
13
 *
14
 * Largely rewritten.. Linus
15
 */
16
 
17
#include <linux/mm.h>
18
#include <linux/delay.h>
19
#include <linux/interrupt.h>
20
 
21
#include <asm/system.h>
22
#include <asm/dma.h>
23
 
24
/* Define this if you want slow routines that try to trip errors */
25
#undef SADISTIC_KMALLOC
26
 
27
/* Private flags. */
28
 
29
#define MF_USED 0xffaa0055
30
#define MF_DMA  0xff00aa55
31
#define MF_FREE 0x0055ffaa
32
 
33
 
34
/*
35
 * Much care has gone into making these routines in this file reentrant.
36
 *
37
 * The fancy bookkeeping of nbytesmalloced and the like are only used to
38
 * report them to the user (oooohhhhh, aaaaahhhhh....) are not
39
 * protected by cli(). (If that goes wrong. So what?)
40
 *
41
 * These routines restore the interrupt status to allow calling with ints
42
 * off.
43
 */
44
 
45
/*
46
 * A block header. This is in front of every malloc-block, whether free or not.
47
 */
48
struct block_header {
49
        unsigned long bh_flags;
50
        union {
51
                unsigned long ubh_length;
52
                struct block_header *fbh_next;
53
        } vp;
54
};
55
 
56
 
57
#define bh_length vp.ubh_length
58
#define bh_next   vp.fbh_next
59
#define BH(p) ((struct block_header *)(p))
60
 
61
 
62
/*
63
 * The page descriptor is at the front of every page that malloc has in use.
64
 */
65
struct page_descriptor {
66
        struct page_descriptor *next;
67
        struct block_header *firstfree;
68
        int order;
69
        int nfree;
70
};
71
 
72
 
73
#define PAGE_DESC(p) ((struct page_descriptor *)(((unsigned long)(p)) & PAGE_MASK))
74
 
75
 
76
/*
77
 * A size descriptor describes a specific class of malloc sizes.
78
 * Each class of sizes has its own freelist.
79
 */
80
struct size_descriptor {
81
        struct page_descriptor *firstfree;
82
        struct page_descriptor *dmafree;        /* DMA-able memory */
83
        int nblocks;
84
 
85
        int nmallocs;
86
        int nfrees;
87
        int nbytesmalloced;
88
        int npages;
89
        unsigned long gfporder; /* number of pages in the area required */
90
};
91
 
92
/*
93
 * For now it is unsafe to allocate bucket sizes between n and
94
 * n-sizeof(page_descriptor) where n is PAGE_SIZE * any power of two
95
 *
96
 * The blocksize and sizes arrays _must_ match!
97
 */
98
#if PAGE_SIZE == 4096
99
static const unsigned int blocksize[] = {
100
        32,
101
        64,
102
        128,
103
        252,
104
        508,
105
        1020,
106
        2040,
107
        4096 - 16,
108
        8192 - 16,
109
        16384 - 16,
110
        32768 - 16,
111
        65536 - 16,
112
        131072 - 16,
113
        262144 - 16,
114
#ifdef BIGALLOCS
115
        524288 - 16,
116
        1048576 - 16,
117
#endif
118
 
119
};
120
 
121
static struct size_descriptor sizes[] =
122
{
123
        {NULL, NULL, 127, 0, 0, 0, 0, 0},
124
        {NULL, NULL, 63, 0, 0, 0, 0, 0},
125
        {NULL, NULL, 31, 0, 0, 0, 0, 0},
126
        {NULL, NULL, 16, 0, 0, 0, 0, 0},
127
        {NULL, NULL, 8, 0, 0, 0, 0, 0},
128
        {NULL, NULL, 4, 0, 0, 0, 0, 0},
129
        {NULL, NULL, 2, 0, 0, 0, 0, 0},
130
        {NULL, NULL, 1, 0, 0, 0, 0, 0},
131
        {NULL, NULL, 1, 0, 0, 0, 0, 1},
132
        {NULL, NULL, 1, 0, 0, 0, 0, 2},
133
        {NULL, NULL, 1, 0, 0, 0, 0, 3},
134
        {NULL, NULL, 1, 0, 0, 0, 0, 4},
135
        {NULL, NULL, 1, 0, 0, 0, 0, 5},
136
        {NULL, NULL, 1, 0, 0, 0, 0, 6},
137
#ifdef BIGALLOCS
138
        {NULL, NULL, 1, 0, 0, 0, 0, 7},
139
        {NULL, NULL, 1, 0, 0, 0, 0, 8},
140
#endif
141
        {NULL, NULL, 0, 0, 0, 0, 0, 0},
142
};
143
#elif PAGE_SIZE == 8192
144
static const unsigned int blocksize[] = {
145
        64,
146
        128,
147
        248,
148
        504,
149
        1016,
150
        2040,
151
        4080,
152
        8192 - 32,
153
        16384 - 32,
154
        32768 - 32,
155
        65536 - 32,
156
        131072 - 32,
157
        262144 - 32,
158
 
159
};
160
 
161
struct size_descriptor sizes[] =
162
{
163
        {NULL, NULL, 127, 0, 0, 0, 0, 0},
164
        {NULL, NULL, 63, 0, 0, 0, 0, 0},
165
        {NULL, NULL, 31, 0, 0, 0, 0, 0},
166
        {NULL, NULL, 16, 0, 0, 0, 0, 0},
167
        {NULL, NULL, 8, 0, 0, 0, 0, 0},
168
        {NULL, NULL, 4, 0, 0, 0, 0, 0},
169
        {NULL, NULL, 2, 0, 0, 0, 0, 0},
170
        {NULL, NULL, 1, 0, 0, 0, 0, 0},
171
        {NULL, NULL, 1, 0, 0, 0, 0, 1},
172
        {NULL, NULL, 1, 0, 0, 0, 0, 2},
173
        {NULL, NULL, 1, 0, 0, 0, 0, 3},
174
        {NULL, NULL, 1, 0, 0, 0, 0, 4},
175
        {NULL, NULL, 1, 0, 0, 0, 0, 5},
176
        {NULL, NULL, 0, 0, 0, 0, 0, 0}
177
};
178
#else
179
#error you need to make a version for your pagesize
180
#endif
181
 
182
#define NBLOCKS(order)          (sizes[order].nblocks)
183
#define BLOCKSIZE(order)        (blocksize[order])
184
#define AREASIZE(order)         (PAGE_SIZE<<(sizes[order].gfporder))
185
 
186
/*
187
 * Create a small cache of page allocations: this helps a bit with
188
 * those pesky 8kB+ allocations for NFS when we're temporarily
189
 * out of memory..
190
 *
191
 * This is a _truly_ small cache, we just cache one single page
192
 * order (for orders 0, 1 and 2, that is  4, 8 and 16kB on x86).
193
 */
194
#define MAX_CACHE_ORDER 3
195
struct page_descriptor * kmalloc_cache[MAX_CACHE_ORDER];
196
 
197
static inline struct page_descriptor * get_kmalloc_pages(unsigned long priority,
198
        unsigned long order, int dma)
199
{
200
        return (struct page_descriptor *) __get_free_pages(priority, order, dma);
201
}
202
 
203
static inline void free_kmalloc_pages(struct page_descriptor * page,
204
        unsigned long order, int dma)
205
{
206
        if (!dma && order < MAX_CACHE_ORDER) {
207
                page = xchg(kmalloc_cache+order, page);
208
                if (!page)
209
                        return;
210
        }
211
        free_pages((unsigned long) page, order);
212
}
213
 
214
long kmalloc_init(long start_mem, long end_mem)
215
{
216
        int order;
217
 
218
/*
219
 * Check the static info array. Things will blow up terribly if it's
220
 * incorrect. This is a late "compile time" check.....
221
 */
222
        for (order = 0; BLOCKSIZE(order); order++) {
223
                if ((NBLOCKS(order) * BLOCKSIZE(order) + sizeof(struct page_descriptor)) >
224
                    AREASIZE(order)) {
225
                        printk("Cannot use %d bytes out of %d in order = %d block mallocs\n",
226
                               (int) (NBLOCKS(order) * BLOCKSIZE(order) +
227
                                      sizeof(struct page_descriptor)),
228
                                (int) AREASIZE(order),
229
                               BLOCKSIZE(order));
230
                        panic("This only happens if someone messes with kmalloc");
231
                }
232
        }
233
        return start_mem;
234
}
235
 
236
 
237
/*
238
 * Ugh, this is ugly, but we want the default case to run
239
 * straight through, which is why we have the ugly goto's
240
 */
241
void *kmalloc(size_t size, int priority)
242
{
243
        unsigned long flags;
244
        unsigned long type;
245
        int order, dma;
246
        struct block_header *p;
247
        struct page_descriptor *page, **pg;
248
        struct size_descriptor *bucket = sizes;
249
 
250
        /* Get order */
251
        order = 0;
252
        {
253
                unsigned int realsize = size + sizeof(struct block_header);
254
                for (;;) {
255
                        int ordersize = BLOCKSIZE(order);
256
                        if (realsize <= ordersize)
257
                                break;
258
                        order++;
259
                        bucket++;
260
                        if (ordersize)
261
                                continue;
262
                        printk("kmalloc of too large a block (%d bytes).\n", (int) size);
263
                        return NULL;
264
                }
265
        }
266
 
267
        dma = 0;
268
        type = MF_USED;
269
        pg = &bucket->firstfree;
270
        if (priority & GFP_DMA) {
271
                dma = 1;
272
                type = MF_DMA;
273
                pg = &bucket->dmafree;
274
        }
275
 
276
        priority &= GFP_LEVEL_MASK;
277
 
278
/* Sanity check... */
279
        if (intr_count && priority != GFP_ATOMIC) {
280
                static int count = 0;
281
                if (++count < 5) {
282
                        printk("kmalloc called nonatomically from interrupt %p\n",
283
                               __builtin_return_address(0));
284
                        priority = GFP_ATOMIC;
285
                }
286
        }
287
 
288
        save_flags(flags);
289
        cli();
290
        page = *pg;
291
        if (!page)
292
                goto no_bucket_page;
293
 
294
        p = page->firstfree;
295
        if (p->bh_flags != MF_FREE)
296
                goto not_free_on_freelist;
297
 
298
found_it:
299
        page->firstfree = p->bh_next;
300
        page->nfree--;
301
        if (!page->nfree)
302
                *pg = page->next;
303
        restore_flags(flags);
304
        bucket->nmallocs++;
305
        bucket->nbytesmalloced += size;
306
        p->bh_flags = type;     /* As of now this block is officially in use */
307
        p->bh_length = size;
308
#ifdef SADISTIC_KMALLOC
309
        memset(p+1, 0xf0, size);
310
#endif
311
        return p + 1;           /* Pointer arithmetic: increments past header */
312
 
313
 
314
no_bucket_page:
315
        /*
316
         * If we didn't find a page already allocated for this
317
         * bucket size, we need to get one..
318
         *
319
         * This can be done with ints on: it is private to this invocation
320
         */
321
        restore_flags(flags);
322
 
323
        {
324
                int i, sz;
325
 
326
                /* sz is the size of the blocks we're dealing with */
327
                sz = BLOCKSIZE(order);
328
 
329
                page = get_kmalloc_pages(priority, bucket->gfporder, dma);
330
                if (!page)
331
                        goto no_free_page;
332
found_cached_page:
333
 
334
                bucket->npages++;
335
 
336
                page->order = order;
337
                /* Loop for all but last block: */
338
                i = (page->nfree = bucket->nblocks) - 1;
339
                p = BH(page + 1);
340
                while (i > 0) {
341
                        i--;
342
                        p->bh_flags = MF_FREE;
343
                        p->bh_next = BH(((long) p) + sz);
344
                        p = p->bh_next;
345
                }
346
                /* Last block: */
347
                p->bh_flags = MF_FREE;
348
                p->bh_next = NULL;
349
 
350
                p = BH(page+1);
351
        }
352
 
353
        /*
354
         * Now we're going to muck with the "global" freelist
355
         * for this size: this should be uninterruptible
356
         */
357
        cli();
358
        page->next = *pg;
359
        *pg = page;
360
        goto found_it;
361
 
362
 
363
no_free_page:
364
        /*
365
         * No free pages, check the kmalloc cache of
366
         * pages to see if maybe we have something available
367
         */
368
        if (!dma && order < MAX_CACHE_ORDER) {
369
                page = xchg(kmalloc_cache+order, page);
370
                if (page)
371
                        goto found_cached_page;
372
        }
373
        {
374
                static unsigned long last = 0;
375
                if (priority != GFP_BUFFER && priority != GFP_IO &&
376
                    (last + 10 * HZ < jiffies)) {
377
                        last = jiffies;
378
                        printk("Couldn't get a free page.....\n");
379
                }
380
                return NULL;
381
        }
382
 
383
not_free_on_freelist:
384
        restore_flags(flags);
385
        printk("Problem: block on freelist at %08lx isn't free.\n", (long) p);
386
        return NULL;
387
}
388
 
389
void kfree(void *__ptr)
390
{
391
        int dma;
392
        unsigned long flags;
393
        unsigned int order;
394
        struct page_descriptor *page, **pg;
395
        struct size_descriptor *bucket;
396
 
397
        if (!__ptr)
398
                goto null_kfree;
399
#define ptr ((struct block_header *) __ptr)
400
        page = PAGE_DESC(ptr);
401
        __ptr = ptr - 1;
402
        if (~PAGE_MASK & (unsigned long)page->next)
403
                goto bad_order;
404
        order = page->order;
405
        if (order >= sizeof(sizes) / sizeof(sizes[0]))
406
                goto bad_order;
407
        bucket = sizes + order;
408
        dma = 0;
409
        pg = &bucket->firstfree;
410
        if (ptr->bh_flags == MF_DMA) {
411
                dma = 1;
412
                ptr->bh_flags = MF_USED;
413
                pg = &bucket->dmafree;
414
        }
415
        if (ptr->bh_flags != MF_USED)
416
                goto bad_order;
417
        ptr->bh_flags = MF_FREE;        /* As of now this block is officially free */
418
#ifdef SADISTIC_KMALLOC
419
        memset(ptr+1, 0xe0, ptr->bh_length);
420
#endif
421
        save_flags(flags);
422
        cli();
423
 
424
        bucket->nfrees++;
425
        bucket->nbytesmalloced -= ptr->bh_length;
426
 
427
        ptr->bh_next = page->firstfree;
428
        page->firstfree = ptr;
429
        if (!page->nfree++) {
430
/* Page went from full to one free block: put it on the freelist. */
431
                if (bucket->nblocks == 1)
432
                        goto free_page;
433
                page->next = *pg;
434
                *pg = page;
435
        }
436
/* If page is completely free, free it */
437
        if (page->nfree == bucket->nblocks) {
438
                for (;;) {
439
                        struct page_descriptor *tmp = *pg;
440
                        if (!tmp)
441
                                goto not_on_freelist;
442
                        if (tmp == page)
443
                                break;
444
                        pg = &tmp->next;
445
                }
446
                *pg = page->next;
447
free_page:
448
                bucket->npages--;
449
                free_kmalloc_pages(page, bucket->gfporder, dma);
450
        }
451
        restore_flags(flags);
452
null_kfree:
453
        return;
454
 
455
bad_order:
456
        printk("kfree of non-kmalloced memory: %p, next= %p, order=%d\n",
457
               ptr+1, page->next, page->order);
458
        return;
459
 
460
not_on_freelist:
461
        restore_flags(flags);
462
        printk("Ooops. page %p doesn't show on freelist.\n", page);
463
}
464
 
465
unsigned int ksize(void *__ptr)
466
{
467
        unsigned int order;
468
        struct page_descriptor *page;
469
 
470
        if (!ptr) return 0;
471
#define ptr ((struct block_header *) __ptr)
472
        page = PAGE_DESC(ptr);
473
        __ptr = ptr - 1;
474
 
475
        if (~PAGE_MASK & (unsigned long)page->next) return 0;
476
 
477
        order = page->order;
478
        if (order >= sizeof(sizes) / sizeof(sizes[0])) return 0;
479
 
480
        return BLOCKSIZE(order);
481
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.