Rev 1702 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
759 | palkovsky | 1 | /* |
762 | palkovsky | 2 | * Copyright (C) 2006 Ondrej Palkovsky |
759 | palkovsky | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1702 | cejka | 29 | /** @addtogroup genericmm |
30 | * @{ |
||
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
759 | palkovsky | 35 | #ifndef __SLAB_H__ |
36 | #define __SLAB_H__ |
||
37 | |||
788 | jermar | 38 | #include <adt/list.h> |
759 | palkovsky | 39 | #include <synch/spinlock.h> |
1104 | jermar | 40 | #include <atomic.h> |
814 | palkovsky | 41 | #include <mm/frame.h> |
759 | palkovsky | 42 | |
771 | palkovsky | 43 | /** Minimum size to be allocated by malloc */ |
791 | palkovsky | 44 | #define SLAB_MIN_MALLOC_W 4 |
771 | palkovsky | 45 | |
46 | /** Maximum size to be allocated by malloc */ |
||
1428 | palkovsky | 47 | #define SLAB_MAX_MALLOC_W 18 |
771 | palkovsky | 48 | |
759 | palkovsky | 49 | /** Initial Magazine size (TODO: dynamically growing magazines) */ |
50 | #define SLAB_MAG_SIZE 4 |
||
51 | |||
52 | /** If object size is less, store control structure inside SLAB */ |
||
766 | palkovsky | 53 | #define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3) |
759 | palkovsky | 54 | |
762 | palkovsky | 55 | /** Maximum wasted space we allow for cache */ |
771 | palkovsky | 56 | #define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2) |
759 | palkovsky | 57 | |
58 | /* slab_reclaim constants */ |
||
59 | #define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because |
||
60 | * we are in memory stress */ |
||
61 | |||
62 | /* cache_create flags */ |
||
63 | #define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */ |
||
64 | #define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */ |
||
789 | palkovsky | 65 | /** We add magazine cache later, if we have this flag */ |
66 | #define SLAB_CACHE_MAGDEFERRED (0x4 | SLAB_CACHE_NOMAGAZINE) |
||
759 | palkovsky | 67 | |
68 | typedef struct { |
||
69 | link_t link; |
||
762 | palkovsky | 70 | count_t busy; /**< Count of full slots in magazine */ |
71 | count_t size; /**< Number of slots in magazine */ |
||
72 | void *objs[0]; /**< Slots in magazine */ |
||
759 | palkovsky | 73 | }slab_magazine_t; |
74 | |||
75 | typedef struct { |
||
789 | palkovsky | 76 | slab_magazine_t *current; |
77 | slab_magazine_t *last; |
||
78 | SPINLOCK_DECLARE(lock); |
||
79 | }slab_mag_cache_t; |
||
80 | |||
81 | |||
82 | typedef struct { |
||
759 | palkovsky | 83 | char *name; |
84 | |||
85 | link_t link; |
||
86 | /* Configuration */ |
||
1576 | jermar | 87 | size_t size; /**< Size of slab position - align_up(sizeof(obj)) */ |
759 | palkovsky | 88 | int (*constructor)(void *obj, int kmflag); |
787 | palkovsky | 89 | int (*destructor)(void *obj); |
762 | palkovsky | 90 | int flags; /**< Flags changing behaviour of cache */ |
759 | palkovsky | 91 | |
92 | /* Computed values */ |
||
1780 | jermar | 93 | uint8_t order; /**< Order of frames to be allocated */ |
762 | palkovsky | 94 | int objects; /**< Number of objects that fit in */ |
759 | palkovsky | 95 | |
96 | /* Statistics */ |
||
764 | palkovsky | 97 | atomic_t allocated_slabs; |
98 | atomic_t allocated_objs; |
||
767 | palkovsky | 99 | atomic_t cached_objs; |
1576 | jermar | 100 | atomic_t magazine_counter; /**< How many magazines in magazines list */ |
759 | palkovsky | 101 | |
102 | /* Slabs */ |
||
762 | palkovsky | 103 | link_t full_slabs; /**< List of full slabs */ |
104 | link_t partial_slabs; /**< List of partial slabs */ |
||
776 | palkovsky | 105 | SPINLOCK_DECLARE(slablock); |
759 | palkovsky | 106 | /* Magazines */ |
762 | palkovsky | 107 | link_t magazines; /**< List o full magazines */ |
776 | palkovsky | 108 | SPINLOCK_DECLARE(maglock); |
762 | palkovsky | 109 | |
110 | /** CPU cache */ |
||
789 | palkovsky | 111 | slab_mag_cache_t *mag_cache; |
759 | palkovsky | 112 | }slab_cache_t; |
113 | |||
762 | palkovsky | 114 | extern slab_cache_t * slab_cache_create(char *name, |
115 | size_t size, |
||
116 | size_t align, |
||
117 | int (*constructor)(void *obj, int kmflag), |
||
787 | palkovsky | 118 | int (*destructor)(void *obj), |
762 | palkovsky | 119 | int flags); |
120 | extern void slab_cache_destroy(slab_cache_t *cache); |
||
759 | palkovsky | 121 | |
762 | palkovsky | 122 | extern void * slab_alloc(slab_cache_t *cache, int flags); |
123 | extern void slab_free(slab_cache_t *cache, void *obj); |
||
124 | extern count_t slab_reclaim(int flags); |
||
759 | palkovsky | 125 | |
1576 | jermar | 126 | /** Initialize slab subsytem */ |
762 | palkovsky | 127 | extern void slab_cache_init(void); |
789 | palkovsky | 128 | extern void slab_enable_cpucache(void); |
759 | palkovsky | 129 | |
1576 | jermar | 130 | /* kconsole debug */ |
762 | palkovsky | 131 | extern void slab_print_list(void); |
759 | palkovsky | 132 | |
1576 | jermar | 133 | /* malloc support */ |
822 | palkovsky | 134 | extern void * malloc(unsigned int size, int flags); |
135 | extern void free(void *obj); |
||
759 | palkovsky | 136 | #endif |
1702 | cejka | 137 | |
138 | /** @} |
||
139 | */ |
||
140 |