summaryrefslogtreecommitdiff
path: root/AK/FixedArray.h
blob: 92593e5ffaf603da5544e815654d05bbbe9fce3d (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/Iterator.h>
#include <AK/Span.h>
#include <AK/kmalloc.h>

namespace AK {

// FixedArray is an Array with a size only known at run-time.
// It guarantees to only allocate when being constructed, and to only deallocate when being destructed.
template<typename T>
class FixedArray {
public:
    FixedArray() = default;

    static ErrorOr<FixedArray<T>> try_create(size_t size)
    {
        if (size == 0)
            return FixedArray<T>();
        T* elements = static_cast<T*>(kmalloc_array(size, sizeof(T)));
        if (!elements)
            return Error::from_errno(ENOMEM);
        for (size_t i = 0; i < size; ++i)
            new (&elements[i]) T();
        return FixedArray<T>(size, elements);
    }

    static FixedArray<T> must_create_but_fixme_should_propagate_errors(size_t size)
    {
        return MUST(try_create(size));
    }

    // NOTE:
    // Even though it may look like there will be a template instantiation of this function for every size,
    // the compiler will inline this anyway and therefore not generate any duplicate code.

    template<size_t N>
    static ErrorOr<FixedArray<T>> try_create(T(&&array)[N])
    {
        if (N == 0)
            return FixedArray<T>();
        T* elements = static_cast<T*>(kmalloc_array(N, sizeof(T)));
        if (!elements)
            return Error::from_errno(ENOMEM);
        for (size_t i = 0; i < N; ++i)
            new (&elements[i]) T(move(array[i]));
        return FixedArray<T>(N, elements);
    }

    template<typename U>
    static ErrorOr<FixedArray<T>> try_create(Span<U> span)
    {
        if (span.size() == 0)
            return FixedArray<T>();
        T* elements = static_cast<T*>(kmalloc_array(span.size(), sizeof(T)));
        if (!elements)
            return Error::from_errno(ENOMEM);
        for (size_t i = 0; i < span.size(); ++i)
            new (&elements[i]) T(span[i]);
        return FixedArray<T>(span.size(), elements);
    }

    ErrorOr<FixedArray<T>> try_clone() const
    {
        if (m_size == 0)
            return FixedArray<T>();
        T* elements = static_cast<T*>(kmalloc_array(m_size, sizeof(T)));
        if (!elements)
            return Error::from_errno(ENOMEM);
        for (size_t i = 0; i < m_size; ++i)
            new (&elements[i]) T(m_elements[i]);
        return FixedArray<T>(m_size, elements);
    }

    FixedArray<T> must_clone_but_fixme_should_propagate_errors() const
    {
        return MUST(try_clone());
    }

    // Nobody can ever use these functions, since it would be impossible to make them OOM-safe due to their signatures. We just explicitly delete them.
    FixedArray(FixedArray<T> const&) = delete;
    FixedArray<T>& operator=(FixedArray<T> const&) = delete;

    FixedArray(FixedArray<T>&& other)
        : m_size(other.m_size)
        , m_elements(other.m_elements)
    {
        other.m_size = 0;
        other.m_elements = nullptr;
    }
    // This function would violate the contract, as it would need to deallocate this FixedArray. As it also has no use case, we delete it.
    FixedArray<T>& operator=(FixedArray<T>&&) = delete;

    ~FixedArray()
    {
        if (!m_elements)
            return;
        for (size_t i = 0; i < m_size; ++i)
            m_elements[i].~T();
        kfree_sized(m_elements, sizeof(T) * m_size);
        // NOTE: should prevent use-after-free early
        m_size = 0;
        m_elements = nullptr;
    }

    size_t size() const { return m_size; }
    T* data() { return m_elements; }
    T const* data() const { return m_elements; }

    T& operator[](size_t index)
    {
        VERIFY(index < m_size);
        return m_elements[index];
    }

    T const& operator[](size_t index) const
    {
        VERIFY(index < m_size);
        return m_elements[index];
    }

    bool contains_slow(T const& value) const
    {
        for (size_t i = 0; i < m_size; ++i) {
            if (m_elements[i] == value)
                return true;
        }
        return false;
    }

    void swap(FixedArray<T>& other)
    {
        ::swap(m_size, other.m_size);
        ::swap(m_elements, other.m_elements);
    }

    using Iterator = SimpleIterator<FixedArray, T>;
    using ConstIterator = SimpleIterator<FixedArray const, T const>;

    Iterator begin() { return Iterator::begin(*this); }
    ConstIterator begin() const { return ConstIterator::begin(*this); }

    Iterator end() { return Iterator::end(*this); }
    ConstIterator end() const { return ConstIterator::end(*this); }

    Span<T> span() { return { data(), size() }; }
    Span<T const> span() const { return { data(), size() }; }

private:
    FixedArray(size_t size, T* elements)
        : m_size(size)
        , m_elements(elements)
    {
    }

    size_t m_size { 0 };
    T* m_elements { nullptr };
};

}

using AK::FixedArray;