Subversion Repositories HelenOS

Rev

Rev 4490 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
759 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Ondrej Palkovsky
759 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
 
1888 jermar 29
/** @addtogroup genericmm
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1888 jermar 35
#ifndef KERN_SLAB_H_
36
#define KERN_SLAB_H_
759 palkovsky 37
 
788 jermar 38
#include <adt/list.h>
759 palkovsky 39
#include <synch/spinlock.h>
1104 jermar 40
#include <atomic.h>
814 palkovsky 41
#include <mm/frame.h>
759 palkovsky 42
 
771 palkovsky 43
/** Minimum size to be allocated by malloc */
4174 decky 44
#define SLAB_MIN_MALLOC_W  4
771 palkovsky 45
 
46
/** Maximum size to be allocated by malloc */
4174 decky 47
#define SLAB_MAX_MALLOC_W  22
771 palkovsky 48
 
759 palkovsky 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 */
4174 decky 53
#define SLAB_INSIDE_SIZE  (PAGE_SIZE >> 3)
759 palkovsky 54
 
762 palkovsky 55
/** Maximum wasted space we allow for cache */
4174 decky 56
#define SLAB_MAX_BADNESS(cache) \
3182 jermar 57
    (((unsigned int) PAGE_SIZE << (cache)->order) >> 2)
759 palkovsky 58
 
59
/* slab_reclaim constants */
60
 
2106 jermar 61
/** Reclaim all possible memory, because we are in memory stress */
3972 decky 62
#define SLAB_RECLAIM_ALL  0x01
2106 jermar 63
 
759 palkovsky 64
/* cache_create flags */
65
 
2106 jermar 66
/** Do not use per-cpu cache */
4174 decky 67
#define SLAB_CACHE_NOMAGAZINE   0x01
2106 jermar 68
/** Have control structure inside SLAB */
4174 decky 69
#define SLAB_CACHE_SLINSIDE     0x02
2106 jermar 70
/** We add magazine cache later, if we have this flag */
4174 decky 71
#define SLAB_CACHE_MAGDEFERRED  (0x04 | SLAB_CACHE_NOMAGAZINE)
2106 jermar 72
 
759 palkovsky 73
typedef struct {
74
    link_t link;
4490 decky 75
    size_t busy;  /**< Count of full slots in magazine */
76
    size_t size;  /**< Number of slots in magazine */
4174 decky 77
    void *objs[];  /**< Slots in magazine */
1950 jermar 78
} slab_magazine_t;
759 palkovsky 79
 
80
typedef struct {
789 palkovsky 81
    slab_magazine_t *current;
82
    slab_magazine_t *last;
83
    SPINLOCK_DECLARE(lock);
1950 jermar 84
} slab_mag_cache_t;
789 palkovsky 85
 
86
 
87
typedef struct {
759 palkovsky 88
    char *name;
4174 decky 89
 
759 palkovsky 90
    link_t link;
4174 decky 91
 
759 palkovsky 92
    /* Configuration */
2106 jermar 93
    /** Size of slab position - align_up(sizeof(obj)) */
94
    size_t size;
4174 decky 95
 
759 palkovsky 96
    int (*constructor)(void *obj, int kmflag);
787 palkovsky 97
    int (*destructor)(void *obj);
4174 decky 98
 
2106 jermar 99
    /** Flags changing behaviour of cache */
100
    int flags;
4174 decky 101
 
759 palkovsky 102
    /* Computed values */
4174 decky 103
    uint8_t order;         /**< Order of frames to be allocated */
104
    unsigned int objects;  /**< Number of objects that fit in */
105
 
759 palkovsky 106
    /* Statistics */
764 palkovsky 107
    atomic_t allocated_slabs;
108
    atomic_t allocated_objs;
767 palkovsky 109
    atomic_t cached_objs;
2106 jermar 110
    /** How many magazines in magazines list */
111
    atomic_t magazine_counter;
4174 decky 112
 
759 palkovsky 113
    /* Slabs */
4174 decky 114
    link_t full_slabs;     /**< List of full slabs */
115
    link_t partial_slabs;  /**< List of partial slabs */
776 palkovsky 116
    SPINLOCK_DECLARE(slablock);
4174 decky 117
    /* Magazines */
118
    link_t magazines;  /**< List o full magazines */
776 palkovsky 119
    SPINLOCK_DECLARE(maglock);
4174 decky 120
 
762 palkovsky 121
    /** CPU cache */
789 palkovsky 122
    slab_mag_cache_t *mag_cache;
1950 jermar 123
} slab_cache_t;
759 palkovsky 124
 
3182 jermar 125
extern slab_cache_t *slab_cache_create(char *, size_t, size_t,
126
    int (*)(void *, int), int (*)(void *), int);
127
extern void slab_cache_destroy(slab_cache_t *);
759 palkovsky 128
 
3182 jermar 129
extern void * slab_alloc(slab_cache_t *, int);
130
extern void slab_free(slab_cache_t *, void *);
4490 decky 131
extern size_t slab_reclaim(int);
759 palkovsky 132
 
2106 jermar 133
/* slab subsytem initialization */
762 palkovsky 134
extern void slab_cache_init(void);
789 palkovsky 135
extern void slab_enable_cpucache(void);
759 palkovsky 136
 
1576 jermar 137
/* kconsole debug */
762 palkovsky 138
extern void slab_print_list(void);
759 palkovsky 139
 
1576 jermar 140
/* malloc support */
3182 jermar 141
extern void *malloc(unsigned int, int);
142
extern void *realloc(void *, unsigned int, int);
143
extern void free(void *);
4174 decky 144
 
759 palkovsky 145
#endif
1702 cejka 146
 
1888 jermar 147
/** @}
1702 cejka 148
 */