[4e147a6] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[4e147a6] | 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 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_mm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_SLAB_H_
|
---|
| 36 | #define KERN_SLAB_H_
|
---|
[4e147a6] | 37 |
|
---|
[5c9a08b] | 38 | #include <adt/list.h>
|
---|
[4e147a6] | 39 | #include <synch/spinlock.h>
|
---|
[23684b7] | 40 | #include <atomic.h>
|
---|
[085d973] | 41 | #include <mm/frame.h>
|
---|
[4e147a6] | 42 |
|
---|
[c352c2e] | 43 | /** Minimum size to be allocated by malloc */
|
---|
[c3ebc47] | 44 | #define SLAB_MIN_MALLOC_W 4
|
---|
[c352c2e] | 45 |
|
---|
| 46 | /** Maximum size to be allocated by malloc */
|
---|
[c3ebc47] | 47 | #define SLAB_MAX_MALLOC_W 22
|
---|
[c352c2e] | 48 |
|
---|
[4e147a6] | 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 */
|
---|
[c3ebc47] | 53 | #define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3)
|
---|
[4e147a6] | 54 |
|
---|
[a294ad0] | 55 | /** Maximum wasted space we allow for cache */
|
---|
[c3ebc47] | 56 | #define SLAB_MAX_BADNESS(cache) \
|
---|
[b0c2075] | 57 | (FRAMES2SIZE((cache)->frames) >> 2)
|
---|
[4e147a6] | 58 |
|
---|
| 59 | /* slab_reclaim constants */
|
---|
[80bcaed] | 60 |
|
---|
| 61 | /** Reclaim all possible memory, because we are in memory stress */
|
---|
[5f0f29ce] | 62 | #define SLAB_RECLAIM_ALL 0x01
|
---|
[4e147a6] | 63 |
|
---|
| 64 | /* cache_create flags */
|
---|
[80bcaed] | 65 |
|
---|
| 66 | /** Do not use per-cpu cache */
|
---|
[c3ebc47] | 67 | #define SLAB_CACHE_NOMAGAZINE 0x01
|
---|
[80bcaed] | 68 | /** Have control structure inside SLAB */
|
---|
[c3ebc47] | 69 | #define SLAB_CACHE_SLINSIDE 0x02
|
---|
[80bcaed] | 70 | /** We add magazine cache later, if we have this flag */
|
---|
[c3ebc47] | 71 | #define SLAB_CACHE_MAGDEFERRED (0x04 | SLAB_CACHE_NOMAGAZINE)
|
---|
[4e147a6] | 72 |
|
---|
| 73 | typedef struct {
|
---|
| 74 | link_t link;
|
---|
[98000fb] | 75 | size_t busy; /**< Count of full slots in magazine */
|
---|
| 76 | size_t size; /**< Number of slots in magazine */
|
---|
[c3ebc47] | 77 | void *objs[]; /**< Slots in magazine */
|
---|
[f3272e98] | 78 | } slab_magazine_t;
|
---|
[4e147a6] | 79 |
|
---|
[8e1ea655] | 80 | typedef struct {
|
---|
| 81 | slab_magazine_t *current;
|
---|
| 82 | slab_magazine_t *last;
|
---|
[25ebfbd] | 83 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
[f3272e98] | 84 | } slab_mag_cache_t;
|
---|
[8e1ea655] | 85 |
|
---|
[4e147a6] | 86 | typedef struct {
|
---|
[a000878c] | 87 | const char *name;
|
---|
[a35b458] | 88 |
|
---|
[4e147a6] | 89 | link_t link;
|
---|
[a35b458] | 90 |
|
---|
[4e147a6] | 91 | /* Configuration */
|
---|
[a35b458] | 92 |
|
---|
[80bcaed] | 93 | /** Size of slab position - align_up(sizeof(obj)) */
|
---|
| 94 | size_t size;
|
---|
[a35b458] | 95 |
|
---|
[b7fd2a0] | 96 | errno_t (*constructor)(void *obj, unsigned int kmflag);
|
---|
[da1bafb] | 97 | size_t (*destructor)(void *obj);
|
---|
[a35b458] | 98 |
|
---|
[80bcaed] | 99 | /** Flags changing behaviour of cache */
|
---|
[da1bafb] | 100 | unsigned int flags;
|
---|
[a35b458] | 101 |
|
---|
[4e147a6] | 102 | /* Computed values */
|
---|
[b0c2075] | 103 | size_t frames; /**< Number of frames to be allocated */
|
---|
[da1bafb] | 104 | size_t objects; /**< Number of objects that fit in */
|
---|
[a35b458] | 105 |
|
---|
[4e147a6] | 106 | /* Statistics */
|
---|
[bc504ef2] | 107 | atomic_t allocated_slabs;
|
---|
| 108 | atomic_t allocated_objs;
|
---|
[4a5b2b0e] | 109 | atomic_t cached_objs;
|
---|
[80bcaed] | 110 | /** How many magazines in magazines list */
|
---|
[da1bafb] | 111 | atomic_t magazine_counter;
|
---|
[a35b458] | 112 |
|
---|
[4e147a6] | 113 | /* Slabs */
|
---|
[55b77d9] | 114 | list_t full_slabs; /**< List of full slabs */
|
---|
| 115 | list_t partial_slabs; /**< List of partial slabs */
|
---|
[ddb56be] | 116 | IRQ_SPINLOCK_DECLARE(slablock);
|
---|
[c3ebc47] | 117 | /* Magazines */
|
---|
[55b77d9] | 118 | list_t magazines; /**< List o full magazines */
|
---|
[4d194be] | 119 | IRQ_SPINLOCK_DECLARE(maglock);
|
---|
[a35b458] | 120 |
|
---|
[a294ad0] | 121 | /** CPU cache */
|
---|
[8e1ea655] | 122 | slab_mag_cache_t *mag_cache;
|
---|
[f3272e98] | 123 | } slab_cache_t;
|
---|
[4e147a6] | 124 |
|
---|
[a000878c] | 125 | extern slab_cache_t *slab_cache_create(const char *, size_t, size_t,
|
---|
[b7fd2a0] | 126 | errno_t (*)(void *, unsigned int), size_t (*)(void *), unsigned int);
|
---|
[1a48bcd] | 127 | extern void slab_cache_destroy(slab_cache_t *);
|
---|
[4e147a6] | 128 |
|
---|
[273b958] | 129 | extern void *slab_alloc(slab_cache_t *, unsigned int)
|
---|
| 130 | __attribute__((malloc));
|
---|
[1a48bcd] | 131 | extern void slab_free(slab_cache_t *, void *);
|
---|
[da1bafb] | 132 | extern size_t slab_reclaim(unsigned int);
|
---|
[4e147a6] | 133 |
|
---|
[80bcaed] | 134 | /* slab subsytem initialization */
|
---|
[a294ad0] | 135 | extern void slab_cache_init(void);
|
---|
[8e1ea655] | 136 | extern void slab_enable_cpucache(void);
|
---|
[4e147a6] | 137 |
|
---|
[2cb5e64] | 138 | /* kconsole debug */
|
---|
[a294ad0] | 139 | extern void slab_print_list(void);
|
---|
[4e147a6] | 140 |
|
---|
[2cb5e64] | 141 | /* malloc support */
|
---|
[11b285d] | 142 | extern void *malloc(size_t)
|
---|
[273b958] | 143 | __attribute__((malloc));
|
---|
[11b285d] | 144 | extern void *realloc(void *, size_t)
|
---|
[53031c2] | 145 | __attribute__((warn_unused_result));
|
---|
[1a48bcd] | 146 | extern void free(void *);
|
---|
[c3ebc47] | 147 |
|
---|
[11b285d] | 148 | extern void *nfmalloc(size_t)
|
---|
| 149 | __attribute__((malloc, returns_nonnull));
|
---|
| 150 |
|
---|
[4e147a6] | 151 | #endif
|
---|
[b45c443] | 152 |
|
---|
[06e1e95] | 153 | /** @}
|
---|
[b45c443] | 154 | */
|
---|