Subversion Repositories HelenOS-historic

Rev

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

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