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