Subversion Repositories HelenOS

Rev

Rev 1888 | Rev 1950 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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