source: mainline/kernel/generic/include/mm/slab.h@ fc0de8c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc0de8c was b60615bd, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Modify kernel malloc()

This new implementation places the allocation size in front of the allocated
object, instead of relying on the slab allocator being able to determine source
slab cache for an object. This should improve scalability and help reduce
complexity of the memory management subsystem (further changes coming).

The drawback is more memory consumed by small malloc() allocations, however that
can be mitigated by switching to an API where the user provides known object
size to deallocation (most users know it either statically or from length they
necessarily remember).

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