summaryrefslogtreecommitdiff
path: root/Kernel/Heap/SlabAllocator.h
blob: e2fc3a5e7f7d1704af62afecebe5b1f4d05c59b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/Function.h>
#include <AK/Types.h>

namespace Kernel {

#define SLAB_ALLOC_SCRUB_BYTE 0xab
#define SLAB_DEALLOC_SCRUB_BYTE 0xbc

void* slab_alloc(size_t slab_size);
void slab_dealloc(void*, size_t slab_size);
void slab_alloc_init();
ErrorOr<void> slab_alloc_stats(Function<ErrorOr<void>(size_t slab_size, size_t allocated, size_t free)>);

#define MAKE_SLAB_ALLOCATED(type)                                            \
public:                                                                      \
    [[nodiscard]] void* operator new(size_t)                                 \
    {                                                                        \
        void* ptr = slab_alloc(sizeof(type));                                \
        VERIFY(ptr);                                                         \
        return ptr;                                                          \
    }                                                                        \
    [[nodiscard]] void* operator new(size_t, const std::nothrow_t&) noexcept \
    {                                                                        \
        return slab_alloc(sizeof(type));                                     \
    }                                                                        \
    void operator delete(void* ptr) noexcept                                 \
    {                                                                        \
        if (!ptr)                                                            \
            return;                                                          \
        slab_dealloc(ptr, sizeof(type));                                     \
    }                                                                        \
                                                                             \
private:

}