Subversion Repositories HelenOS-historic

Rev

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

Rev 762 Rev 764
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
#ifndef __SLAB_H__
29
#ifndef __SLAB_H__
30
#define __SLAB_H__
30
#define __SLAB_H__
31
 
31
 
32
#include <list.h>
32
#include <list.h>
33
#include <synch/spinlock.h>
33
#include <synch/spinlock.h>
-
 
34
#include <arch/atomic.h>
34
 
35
 
35
/** Initial Magazine size (TODO: dynamically growing magazines) */
36
/** Initial Magazine size (TODO: dynamically growing magazines) */
36
#define SLAB_MAG_SIZE  4
37
#define SLAB_MAG_SIZE  4
37
 
38
 
38
/** If object size is less, store control structure inside SLAB */
39
/** If object size is less, store control structure inside SLAB */
39
#define SLAB_INSIDE_SIZE   (PAGE_SIZE / 6)
40
#define SLAB_INSIDE_SIZE   (PAGE_SIZE / 6)
40
 
41
 
41
/** Maximum wasted space we allow for cache */
42
/** Maximum wasted space we allow for cache */
42
#define SLAB_MAX_BADNESS(cache)   ((PAGE_SIZE << (cache)->order) / 4)
43
#define SLAB_MAX_BADNESS(cache)   ((PAGE_SIZE << (cache)->order) / 4)
43
 
44
 
44
/* slab_reclaim constants */
45
/* slab_reclaim constants */
45
#define SLAB_RECLAIM_ALL  0x1 /**< Reclaim all possible memory, because
46
#define SLAB_RECLAIM_ALL  0x1 /**< Reclaim all possible memory, because
46
                   *   we are in memory stress */
47
                   *   we are in memory stress */
47
 
48
 
48
/* cache_create flags */
49
/* cache_create flags */
49
#define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
50
#define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
50
#define SLAB_CACHE_SLINSIDE   0x2 /**< Have control structure inside SLAB */
51
#define SLAB_CACHE_SLINSIDE   0x2 /**< Have control structure inside SLAB */
51
 
52
 
52
typedef struct {
53
typedef struct {
53
    link_t link;
54
    link_t link;
54
    count_t busy;  /**< Count of full slots in magazine */
55
    count_t busy;  /**< Count of full slots in magazine */
55
    count_t size;  /**< Number of slots in magazine */
56
    count_t size;  /**< Number of slots in magazine */
56
    void *objs[0]; /**< Slots in magazine */
57
    void *objs[0]; /**< Slots in magazine */
57
}slab_magazine_t;
58
}slab_magazine_t;
58
 
59
 
59
typedef struct {
60
typedef struct {
60
    char *name;
61
    char *name;
61
 
62
 
62
    SPINLOCK_DECLARE(lock);
63
    SPINLOCK_DECLARE(lock);
63
    link_t link;
64
    link_t link;
64
    /* Configuration */
65
    /* Configuration */
65
    size_t size;      /**< Size of SLAB position - align_up(sizeof(obj)) */
66
    size_t size;      /**< Size of SLAB position - align_up(sizeof(obj)) */
66
    int (*constructor)(void *obj, int kmflag);
67
    int (*constructor)(void *obj, int kmflag);
67
    void (*destructor)(void *obj);
68
    void (*destructor)(void *obj);
68
    int flags;        /**< Flags changing behaviour of cache */
69
    int flags;        /**< Flags changing behaviour of cache */
69
 
70
 
70
    /* Computed values */
71
    /* Computed values */
71
    __u8 order;        /**< Order of frames to be allocated */
72
    __u8 order;        /**< Order of frames to be allocated */
72
    int objects;      /**< Number of objects that fit in */
73
    int objects;      /**< Number of objects that fit in */
73
 
74
 
74
    /* Statistics */
75
    /* Statistics */
-
 
76
    atomic_t allocated_slabs;
-
 
77
    atomic_t allocated_objs;
75
 
78
 
76
    /* Slabs */
79
    /* Slabs */
77
    link_t full_slabs;     /**< List of full slabs */
80
    link_t full_slabs;     /**< List of full slabs */
78
    link_t partial_slabs;  /**< List of partial slabs */
81
    link_t partial_slabs;  /**< List of partial slabs */
79
    /* Magazines  */
82
    /* Magazines  */
80
    link_t magazines;      /**< List o full magazines */
83
    link_t magazines;      /**< List o full magazines */
81
 
84
 
82
    /** CPU cache */
85
    /** CPU cache */
83
    struct {
86
    struct {
84
        slab_magazine_t *current;
87
        slab_magazine_t *current;
85
        slab_magazine_t *last;
88
        slab_magazine_t *last;
86
        SPINLOCK_DECLARE(lock);
89
        SPINLOCK_DECLARE(lock);
87
    }mag_cache[0];
90
    }mag_cache[0];
88
}slab_cache_t;
91
}slab_cache_t;
89
 
92
 
90
extern slab_cache_t * slab_cache_create(char *name,
93
extern slab_cache_t * slab_cache_create(char *name,
91
                    size_t size,
94
                    size_t size,
92
                    size_t align,
95
                    size_t align,
93
                    int (*constructor)(void *obj, int kmflag),
96
                    int (*constructor)(void *obj, int kmflag),
94
                    void (*destructor)(void *obj),
97
                    void (*destructor)(void *obj),
95
                    int flags);
98
                    int flags);
96
extern void slab_cache_destroy(slab_cache_t *cache);
99
extern void slab_cache_destroy(slab_cache_t *cache);
97
 
100
 
98
extern void * slab_alloc(slab_cache_t *cache, int flags);
101
extern void * slab_alloc(slab_cache_t *cache, int flags);
99
extern void slab_free(slab_cache_t *cache, void *obj);
102
extern void slab_free(slab_cache_t *cache, void *obj);
100
extern count_t slab_reclaim(int flags);
103
extern count_t slab_reclaim(int flags);
101
 
104
 
102
/** Initialize SLAB subsytem */
105
/** Initialize SLAB subsytem */
103
extern void slab_cache_init(void);
106
extern void slab_cache_init(void);
104
 
107
 
105
/* KConsole debug */
108
/* KConsole debug */
106
extern void slab_print_list(void);
109
extern void slab_print_list(void);
107
 
110
 
108
#endif
111
#endif
109
 
112