Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
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 are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Assertions.h>
28#include <Kernel/Heap/SlabAllocator.h>
29#include <Kernel/Heap/kmalloc.h>
30#include <Kernel/VM/Region.h>
31
32namespace Kernel {
33
34template<size_t templated_slab_size>
35class SlabAllocator {
36public:
37 SlabAllocator() {}
38
39 void init(size_t size)
40 {
41 m_base = kmalloc_eternal(size);
42 m_end = (u8*)m_base + size;
43 FreeSlab* slabs = (FreeSlab*)m_base;
44 size_t slab_count = size / templated_slab_size;
45 for (size_t i = 1; i < slab_count; ++i) {
46 slabs[i].next = &slabs[i - 1];
47 }
48 slabs[0].next = nullptr;
49 m_freelist = &slabs[slab_count - 1];
50 m_num_allocated = 0;
51 m_num_free = slab_count;
52 }
53
54 constexpr size_t slab_size() const { return templated_slab_size; }
55
56 void* alloc()
57 {
58 InterruptDisabler disabler;
59 if (!m_freelist)
60 return kmalloc(slab_size());
61 ASSERT(m_freelist);
62 void* ptr = m_freelist;
63 m_freelist = m_freelist->next;
64 ++m_num_allocated;
65 --m_num_free;
66#ifdef SANITIZE_KMALLOC
67 memset(ptr, SLAB_ALLOC_SCRUB_BYTE, slab_size());
68#endif
69 return ptr;
70 }
71
72 void dealloc(void* ptr)
73 {
74 InterruptDisabler disabler;
75 ASSERT(ptr);
76 if (ptr < m_base || ptr >= m_end) {
77 kfree(ptr);
78 return;
79 }
80 ((FreeSlab*)ptr)->next = m_freelist;
81#ifdef SANITIZE_KMALLOC
82 if (slab_size() > sizeof(FreeSlab*))
83 memset(((FreeSlab*)ptr)->padding, SLAB_DEALLOC_SCRUB_BYTE, sizeof(FreeSlab::padding));
84#endif
85 m_freelist = (FreeSlab*)ptr;
86 ++m_num_allocated;
87 --m_num_free;
88 }
89
90 size_t num_allocated() const { return m_num_allocated; }
91 size_t num_free() const { return m_num_free; }
92
93private:
94 struct FreeSlab {
95 FreeSlab* next { nullptr };
96 char padding[templated_slab_size - sizeof(FreeSlab*)];
97 };
98
99 // NOTE: These are not default-initialized to prevent an init-time constructor from overwriting them
100 FreeSlab* m_freelist;
101 size_t m_num_allocated;
102 size_t m_num_free;
103 void* m_base;
104 void* m_end;
105
106 static_assert(sizeof(FreeSlab) == templated_slab_size);
107};
108
109static SlabAllocator<16> s_slab_allocator_16;
110static SlabAllocator<32> s_slab_allocator_32;
111static SlabAllocator<64> s_slab_allocator_64;
112
113static_assert(sizeof(Region) <= s_slab_allocator_64.slab_size());
114
115template<typename Callback>
116void for_each_allocator(Callback callback)
117{
118 callback(s_slab_allocator_16);
119 callback(s_slab_allocator_32);
120 callback(s_slab_allocator_64);
121}
122
123void slab_alloc_init()
124{
125 s_slab_allocator_16.init(128 * KB);
126 s_slab_allocator_32.init(128 * KB);
127 s_slab_allocator_64.init(512 * KB);
128}
129
130void* slab_alloc(size_t slab_size)
131{
132 if (slab_size <= 16)
133 return s_slab_allocator_16.alloc();
134 if (slab_size <= 32)
135 return s_slab_allocator_32.alloc();
136 if (slab_size <= 64)
137 return s_slab_allocator_64.alloc();
138 ASSERT_NOT_REACHED();
139}
140
141void slab_dealloc(void* ptr, size_t slab_size)
142{
143 if (slab_size <= 16)
144 return s_slab_allocator_16.dealloc(ptr);
145 if (slab_size <= 32)
146 return s_slab_allocator_32.dealloc(ptr);
147 if (slab_size <= 64)
148 return s_slab_allocator_64.dealloc(ptr);
149 ASSERT_NOT_REACHED();
150}
151
152void slab_alloc_stats(Function<void(size_t slab_size, size_t allocated, size_t free)> callback)
153{
154 for_each_allocator([&](auto& allocator) {
155 callback(allocator.slab_size(), allocator.num_allocated(), allocator.num_free());
156 });
157}
158
159}