Rev 762 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 759 | palkovsky | 1 | /* |
| 2 | * Copyright (C) 2005 Ondrej 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 | |||
| 29 | #ifndef __SLAB_H__ |
||
| 30 | #define __SLAB_H__ |
||
| 31 | |||
| 32 | #include <list.h> |
||
| 33 | #include <synch/spinlock.h> |
||
| 34 | |||
| 35 | /** Initial Magazine size (TODO: dynamically growing magazines) */ |
||
| 36 | #define SLAB_MAG_SIZE 4 |
||
| 37 | |||
| 38 | /** If object size is less, store control structure inside SLAB */ |
||
| 39 | #define SLAB_INSIDE_SIZE (PAGE_SIZE / 6) |
||
| 40 | |||
| 41 | /* slab_alloc constants */ |
||
| 42 | #define SLAB_ATOMIC 0x1 /**< Do not sleep when no free memory, |
||
| 43 | may return NULL */ |
||
| 44 | #define SLAB_NO_RECLAIM 0x2 /**< Do not try to call slab_reclaim, if no |
||
| 45 | free memory is found - avoid deadlock */ |
||
| 46 | |||
| 47 | /* slab_reclaim constants */ |
||
| 48 | #define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because |
||
| 49 | * we are in memory stress */ |
||
| 50 | |||
| 51 | /* cache_create flags */ |
||
| 52 | #define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */ |
||
| 53 | #define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */ |
||
| 54 | |||
| 55 | typedef struct { |
||
| 56 | link_t link; |
||
| 57 | count_t busy; |
||
| 58 | count_t size; |
||
| 59 | void *objs[0]; |
||
| 60 | }slab_magazine_t; |
||
| 61 | |||
| 62 | typedef struct { |
||
| 63 | char *name; |
||
| 64 | |||
| 65 | SPINLOCK_DECLARE(lock); |
||
| 66 | link_t link; |
||
| 67 | /* Configuration */ |
||
| 68 | size_t size; |
||
| 69 | size_t align; |
||
| 70 | int (*constructor)(void *obj, int kmflag); |
||
| 71 | void (*destructor)(void *obj); |
||
| 72 | int flags; |
||
| 73 | |||
| 74 | /* Computed values */ |
||
| 75 | int pages; |
||
| 76 | int objects; |
||
| 77 | |||
| 78 | /* Statistics */ |
||
| 79 | |||
| 80 | /* Slabs */ |
||
| 81 | link_t full_slabs; |
||
| 82 | link_t partial_slabs; |
||
| 83 | /* Magazines */ |
||
| 84 | link_t magazines; |
||
| 85 | /* CPU cache */ |
||
| 86 | struct { |
||
| 87 | slab_magazine_t *current; |
||
| 88 | slab_magazine_t *last; |
||
| 89 | SPINLOCK_DECLARE(lock); |
||
| 90 | }mag_cache[0]; |
||
| 91 | }slab_cache_t; |
||
| 92 | |||
| 93 | typedef struct { |
||
| 94 | slab_cache_t *cache; /**< Pointer to parent cache */ |
||
| 95 | void *start; /**< Start address of first available item */ |
||
| 96 | count_t available; /**< Count of available items in this slab */ |
||
| 97 | index_t nextavail; /**< The index of next available item */ |
||
| 98 | }slab_slab_t; |
||
| 99 | |||
| 100 | |||
| 101 | slab_cache_t * slab_cache_create(char *name, |
||
| 102 | size_t size, |
||
| 103 | size_t align, |
||
| 104 | int (*constructor)(void *obj, int kmflag), |
||
| 105 | void (*destructor)(void *obj), |
||
| 106 | int flags); |
||
| 107 | void slab_cache_destroy(slab_cache_t *cache); |
||
| 108 | |||
| 109 | void * slab_alloc(slab_cache_t *cache, int flags); |
||
| 110 | void slab_free(slab_cache_t *cache, void *obj); |
||
| 111 | count_t slab_reclaim(int flags); |
||
| 112 | |||
| 113 | /** Initialize SLAB subsytem */ |
||
| 114 | void slab_cache_init(void); |
||
| 115 | |||
| 116 | /* KConsole debug */ |
||
| 117 | void slab_print_list(void); |
||
| 118 | |||
| 119 | #endif |