[4e147a6] | 1 | /*
|
---|
[a294ad0] | 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 |
|
---|
[06e1e95] | 29 | /** @addtogroup genericmm
|
---|
[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 */
|
---|
[214f5bb] | 44 | #define SLAB_MIN_MALLOC_W 4
|
---|
[c352c2e] | 45 |
|
---|
| 46 | /** Maximum size to be allocated by malloc */
|
---|
[46fc2f9] | 47 | #define SLAB_MAX_MALLOC_W 18
|
---|
[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 */
|
---|
[14e5d88] | 53 | #define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3)
|
---|
[4e147a6] | 54 |
|
---|
[a294ad0] | 55 | /** Maximum wasted space we allow for cache */
|
---|
[c352c2e] | 56 | #define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2)
|
---|
[4e147a6] | 57 |
|
---|
| 58 | /* slab_reclaim constants */
|
---|
[cd13c2a] | 59 | #define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because we are in memory stress */
|
---|
[4e147a6] | 60 |
|
---|
| 61 | /* cache_create flags */
|
---|
| 62 | #define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
|
---|
| 63 | #define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */
|
---|
[cd13c2a] | 64 | #define SLAB_CACHE_MAGDEFERRED (0x4 | SLAB_CACHE_NOMAGAZINE) /**< We add magazine cache later, if we have this flag */
|
---|
[4e147a6] | 65 |
|
---|
| 66 | typedef struct {
|
---|
| 67 | link_t link;
|
---|
[a294ad0] | 68 | count_t busy; /**< Count of full slots in magazine */
|
---|
| 69 | count_t size; /**< Number of slots in magazine */
|
---|
| 70 | void *objs[0]; /**< Slots in magazine */
|
---|
[f3272e98] | 71 | } slab_magazine_t;
|
---|
[4e147a6] | 72 |
|
---|
[8e1ea655] | 73 | typedef struct {
|
---|
| 74 | slab_magazine_t *current;
|
---|
| 75 | slab_magazine_t *last;
|
---|
| 76 | SPINLOCK_DECLARE(lock);
|
---|
[f3272e98] | 77 | } slab_mag_cache_t;
|
---|
[8e1ea655] | 78 |
|
---|
| 79 |
|
---|
[4e147a6] | 80 | typedef struct {
|
---|
| 81 | char *name;
|
---|
| 82 |
|
---|
| 83 | link_t link;
|
---|
| 84 | /* Configuration */
|
---|
[f3272e98] | 85 | size_t size; /**< Size of slab position - align_up(sizeof(obj)) */
|
---|
[4e147a6] | 86 | int (*constructor)(void *obj, int kmflag);
|
---|
[266294a9] | 87 | int (*destructor)(void *obj);
|
---|
[f3272e98] | 88 | int flags; /**< Flags changing behaviour of cache */
|
---|
[4e147a6] | 89 |
|
---|
| 90 | /* Computed values */
|
---|
[f3272e98] | 91 | uint8_t order; /**< Order of frames to be allocated */
|
---|
| 92 | int objects; /**< Number of objects that fit in */
|
---|
[4e147a6] | 93 |
|
---|
| 94 | /* Statistics */
|
---|
[bc504ef2] | 95 | atomic_t allocated_slabs;
|
---|
| 96 | atomic_t allocated_objs;
|
---|
[4a5b2b0e] | 97 | atomic_t cached_objs;
|
---|
[2cb5e64] | 98 | atomic_t magazine_counter; /**< How many magazines in magazines list */
|
---|
[4e147a6] | 99 |
|
---|
| 100 | /* Slabs */
|
---|
[f3272e98] | 101 | link_t full_slabs; /**< List of full slabs */
|
---|
| 102 | link_t partial_slabs; /**< List of partial slabs */
|
---|
[428aabf] | 103 | SPINLOCK_DECLARE(slablock);
|
---|
[4e147a6] | 104 | /* Magazines */
|
---|
[f3272e98] | 105 | link_t magazines /**< List o full magazines */
|
---|
[428aabf] | 106 | SPINLOCK_DECLARE(maglock);
|
---|
[a294ad0] | 107 |
|
---|
| 108 | /** CPU cache */
|
---|
[8e1ea655] | 109 | slab_mag_cache_t *mag_cache;
|
---|
[f3272e98] | 110 | } slab_cache_t;
|
---|
[4e147a6] | 111 |
|
---|
[a294ad0] | 112 | extern slab_cache_t * slab_cache_create(char *name,
|
---|
| 113 | size_t size,
|
---|
| 114 | size_t align,
|
---|
| 115 | int (*constructor)(void *obj, int kmflag),
|
---|
[266294a9] | 116 | int (*destructor)(void *obj),
|
---|
[a294ad0] | 117 | int flags);
|
---|
| 118 | extern void slab_cache_destroy(slab_cache_t *cache);
|
---|
[4e147a6] | 119 |
|
---|
[a294ad0] | 120 | extern void * slab_alloc(slab_cache_t *cache, int flags);
|
---|
| 121 | extern void slab_free(slab_cache_t *cache, void *obj);
|
---|
| 122 | extern count_t slab_reclaim(int flags);
|
---|
[4e147a6] | 123 |
|
---|
[2cb5e64] | 124 | /** Initialize slab subsytem */
|
---|
[a294ad0] | 125 | extern void slab_cache_init(void);
|
---|
[8e1ea655] | 126 | extern void slab_enable_cpucache(void);
|
---|
[4e147a6] | 127 |
|
---|
[2cb5e64] | 128 | /* kconsole debug */
|
---|
[a294ad0] | 129 | extern void slab_print_list(void);
|
---|
[4e147a6] | 130 |
|
---|
[2cb5e64] | 131 | /* malloc support */
|
---|
[bb68433] | 132 | extern void * malloc(unsigned int size, int flags);
|
---|
| 133 | extern void free(void *obj);
|
---|
[4e147a6] | 134 | #endif
|
---|
[b45c443] | 135 |
|
---|
[06e1e95] | 136 | /** @}
|
---|
[b45c443] | 137 | */
|
---|