Rev 789 | Rev 814 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 789 | Rev 791 | ||
---|---|---|---|
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 | #ifndef __SLAB_H__ |
29 | #ifndef __SLAB_H__ |
30 | #define __SLAB_H__ |
30 | #define __SLAB_H__ |
31 | 31 | ||
32 | #include <adt/list.h> |
32 | #include <adt/list.h> |
33 | #include <synch/spinlock.h> |
33 | #include <synch/spinlock.h> |
34 | #include <arch/atomic.h> |
34 | #include <arch/atomic.h> |
35 | 35 | ||
36 | /** Minimum size to be allocated by malloc */ |
36 | /** Minimum size to be allocated by malloc */ |
37 | #define SLAB_MIN_MALLOC_W 3 |
37 | #define SLAB_MIN_MALLOC_W 4 |
38 | 38 | ||
39 | /** Maximum size to be allocated by malloc */ |
39 | /** Maximum size to be allocated by malloc */ |
40 | #define SLAB_MAX_MALLOC_W 17 |
40 | #define SLAB_MAX_MALLOC_W 17 |
41 | 41 | ||
42 | /** Initial Magazine size (TODO: dynamically growing magazines) */ |
42 | /** Initial Magazine size (TODO: dynamically growing magazines) */ |
43 | #define SLAB_MAG_SIZE 4 |
43 | #define SLAB_MAG_SIZE 4 |
44 | 44 | ||
45 | /** If object size is less, store control structure inside SLAB */ |
45 | /** If object size is less, store control structure inside SLAB */ |
46 | #define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3) |
46 | #define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3) |
47 | 47 | ||
48 | /** Maximum wasted space we allow for cache */ |
48 | /** Maximum wasted space we allow for cache */ |
49 | #define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2) |
49 | #define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2) |
50 | 50 | ||
51 | /* slab_reclaim constants */ |
51 | /* slab_reclaim constants */ |
52 | #define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because |
52 | #define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because |
53 | * we are in memory stress */ |
53 | * we are in memory stress */ |
54 | 54 | ||
55 | /* cache_create flags */ |
55 | /* cache_create flags */ |
56 | #define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */ |
56 | #define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */ |
57 | #define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */ |
57 | #define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */ |
58 | /** We add magazine cache later, if we have this flag */ |
58 | /** We add magazine cache later, if we have this flag */ |
59 | #define SLAB_CACHE_MAGDEFERRED (0x4 | SLAB_CACHE_NOMAGAZINE) |
59 | #define SLAB_CACHE_MAGDEFERRED (0x4 | SLAB_CACHE_NOMAGAZINE) |
60 | 60 | ||
61 | typedef struct { |
61 | typedef struct { |
62 | link_t link; |
62 | link_t link; |
63 | count_t busy; /**< Count of full slots in magazine */ |
63 | count_t busy; /**< Count of full slots in magazine */ |
64 | count_t size; /**< Number of slots in magazine */ |
64 | count_t size; /**< Number of slots in magazine */ |
65 | void *objs[0]; /**< Slots in magazine */ |
65 | void *objs[0]; /**< Slots in magazine */ |
66 | }slab_magazine_t; |
66 | }slab_magazine_t; |
67 | 67 | ||
68 | typedef struct { |
68 | typedef struct { |
69 | slab_magazine_t *current; |
69 | slab_magazine_t *current; |
70 | slab_magazine_t *last; |
70 | slab_magazine_t *last; |
71 | SPINLOCK_DECLARE(lock); |
71 | SPINLOCK_DECLARE(lock); |
72 | }slab_mag_cache_t; |
72 | }slab_mag_cache_t; |
73 | 73 | ||
74 | 74 | ||
75 | typedef struct { |
75 | typedef struct { |
76 | char *name; |
76 | char *name; |
77 | 77 | ||
78 | link_t link; |
78 | link_t link; |
79 | /* Configuration */ |
79 | /* Configuration */ |
80 | size_t size; /**< Size of SLAB position - align_up(sizeof(obj)) */ |
80 | size_t size; /**< Size of SLAB position - align_up(sizeof(obj)) */ |
81 | int (*constructor)(void *obj, int kmflag); |
81 | int (*constructor)(void *obj, int kmflag); |
82 | int (*destructor)(void *obj); |
82 | int (*destructor)(void *obj); |
83 | int flags; /**< Flags changing behaviour of cache */ |
83 | int flags; /**< Flags changing behaviour of cache */ |
84 | 84 | ||
85 | /* Computed values */ |
85 | /* Computed values */ |
86 | __u8 order; /**< Order of frames to be allocated */ |
86 | __u8 order; /**< Order of frames to be allocated */ |
87 | int objects; /**< Number of objects that fit in */ |
87 | int objects; /**< Number of objects that fit in */ |
88 | 88 | ||
89 | /* Statistics */ |
89 | /* Statistics */ |
90 | atomic_t allocated_slabs; |
90 | atomic_t allocated_slabs; |
91 | atomic_t allocated_objs; |
91 | atomic_t allocated_objs; |
92 | atomic_t cached_objs; |
92 | atomic_t cached_objs; |
93 | atomic_t magazine_counter; /*<< How many magazines in magazines list */ |
93 | atomic_t magazine_counter; /*<< How many magazines in magazines list */ |
94 | 94 | ||
95 | /* Slabs */ |
95 | /* Slabs */ |
96 | link_t full_slabs; /**< List of full slabs */ |
96 | link_t full_slabs; /**< List of full slabs */ |
97 | link_t partial_slabs; /**< List of partial slabs */ |
97 | link_t partial_slabs; /**< List of partial slabs */ |
98 | SPINLOCK_DECLARE(slablock); |
98 | SPINLOCK_DECLARE(slablock); |
99 | /* Magazines */ |
99 | /* Magazines */ |
100 | link_t magazines; /**< List o full magazines */ |
100 | link_t magazines; /**< List o full magazines */ |
101 | SPINLOCK_DECLARE(maglock); |
101 | SPINLOCK_DECLARE(maglock); |
102 | 102 | ||
103 | /** CPU cache */ |
103 | /** CPU cache */ |
104 | slab_mag_cache_t *mag_cache; |
104 | slab_mag_cache_t *mag_cache; |
105 | }slab_cache_t; |
105 | }slab_cache_t; |
106 | 106 | ||
107 | extern slab_cache_t * slab_cache_create(char *name, |
107 | extern slab_cache_t * slab_cache_create(char *name, |
108 | size_t size, |
108 | size_t size, |
109 | size_t align, |
109 | size_t align, |
110 | int (*constructor)(void *obj, int kmflag), |
110 | int (*constructor)(void *obj, int kmflag), |
111 | int (*destructor)(void *obj), |
111 | int (*destructor)(void *obj), |
112 | int flags); |
112 | int flags); |
113 | extern void slab_cache_destroy(slab_cache_t *cache); |
113 | extern void slab_cache_destroy(slab_cache_t *cache); |
114 | 114 | ||
115 | extern void * slab_alloc(slab_cache_t *cache, int flags); |
115 | extern void * slab_alloc(slab_cache_t *cache, int flags); |
116 | extern void slab_free(slab_cache_t *cache, void *obj); |
116 | extern void slab_free(slab_cache_t *cache, void *obj); |
117 | extern count_t slab_reclaim(int flags); |
117 | extern count_t slab_reclaim(int flags); |
118 | 118 | ||
119 | /** Initialize SLAB subsytem */ |
119 | /** Initialize SLAB subsytem */ |
120 | extern void slab_cache_init(void); |
120 | extern void slab_cache_init(void); |
121 | extern void slab_enable_cpucache(void); |
121 | extern void slab_enable_cpucache(void); |
122 | 122 | ||
123 | /* KConsole debug */ |
123 | /* KConsole debug */ |
124 | extern void slab_print_list(void); |
124 | extern void slab_print_list(void); |
125 | 125 | ||
126 | /* Malloc support */ |
126 | /* Malloc support */ |
127 | extern void * kalloc(unsigned int size, int flags); |
127 | extern void * kalloc(unsigned int size, int flags); |
128 | extern void kfree(void *obj); |
128 | extern void kfree(void *obj); |
129 | #endif |
129 | #endif |
130 | 130 |