1 | /** @file
|
---|
2 | * IPRT - Hardened AVL tree slab allocator.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2022-2024 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_cpp_hardavlslaballocator_h
|
---|
37 | #define IPRT_INCLUDED_cpp_hardavlslaballocator_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/asm-mem.h>
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 | /** @addtogroup grp_rt_cpp_hardavl
|
---|
49 | * @{
|
---|
50 | */
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Slab allocator for the hardened AVL tree.
|
---|
55 | */
|
---|
56 | template<typename NodeType>
|
---|
57 | struct RTCHardAvlTreeSlabAllocator
|
---|
58 | {
|
---|
59 | /** Pointer to an array of nodes. */
|
---|
60 | NodeType *m_paNodes;
|
---|
61 | /** Node allocation bitmap: 1 = free, 0 = allocated. */
|
---|
62 | uint64_t *m_pbmAlloc;
|
---|
63 | /** Max number of nodes in m_paNodes and valid bits in m_pbmAlloc. */
|
---|
64 | uint32_t m_cNodes;
|
---|
65 | /** Pointer error counter. */
|
---|
66 | uint32_t m_cErrors;
|
---|
67 | /** Allocation hint. */
|
---|
68 | uint32_t m_idxAllocHint;
|
---|
69 | uint32_t m_uPadding;
|
---|
70 |
|
---|
71 | enum
|
---|
72 | {
|
---|
73 | kNilIndex = 0,
|
---|
74 | kErr_IndexOutOfBound = -1,
|
---|
75 | kErr_PointerOutOfBound = -2,
|
---|
76 | kErr_MisalignedPointer = -3,
|
---|
77 | kErr_NodeIsFree = -4,
|
---|
78 | kErr_Last = kErr_NodeIsFree
|
---|
79 | };
|
---|
80 |
|
---|
81 | RTCHardAvlTreeSlabAllocator() RT_NOEXCEPT
|
---|
82 | : m_paNodes(NULL)
|
---|
83 | , m_pbmAlloc(NULL)
|
---|
84 | , m_cNodes(0)
|
---|
85 | , m_cErrors(0)
|
---|
86 | , m_idxAllocHint(0)
|
---|
87 | , m_uPadding(0)
|
---|
88 | {}
|
---|
89 |
|
---|
90 | inline void initSlabAllocator(uint32_t a_cNodes, NodeType *a_paNodes, uint64_t *a_pbmAlloc) RT_NOEXCEPT
|
---|
91 | {
|
---|
92 | m_cNodes = a_cNodes;
|
---|
93 | m_paNodes = a_paNodes;
|
---|
94 | m_pbmAlloc = a_pbmAlloc;
|
---|
95 |
|
---|
96 | /* Initialize the allocation bit. */
|
---|
97 | RT_BZERO(a_pbmAlloc, (a_cNodes + 63) / 64 * 8);
|
---|
98 | ASMBitSetRange(a_pbmAlloc, 0, a_cNodes);
|
---|
99 | }
|
---|
100 |
|
---|
101 | inline NodeType *ptrFromInt(uint32_t a_idxNode1) RT_NOEXCEPT
|
---|
102 | {
|
---|
103 | if (a_idxNode1 == (uint32_t)kNilIndex)
|
---|
104 | return NULL;
|
---|
105 | AssertMsgReturnStmt(a_idxNode1 <= m_cNodes, ("a_idxNode1=%#x m_cNodes=%#x\n", a_idxNode1, m_cNodes),
|
---|
106 | m_cErrors++, (NodeType *)(intptr_t)kErr_IndexOutOfBound);
|
---|
107 | AssertMsgReturnStmt(ASMBitTest(m_pbmAlloc, a_idxNode1 - 1) == false, ("a_idxNode1=%#x\n", a_idxNode1),
|
---|
108 | m_cErrors++, (NodeType *)(intptr_t)kErr_NodeIsFree);
|
---|
109 | return &m_paNodes[a_idxNode1 - 1];
|
---|
110 | }
|
---|
111 |
|
---|
112 | static inline bool isPtrRetOkay(NodeType *a_pNode) RT_NOEXCEPT
|
---|
113 | {
|
---|
114 | return (uintptr_t)a_pNode < (uintptr_t)kErr_Last;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static inline int ptrErrToStatus(NodeType *a_pNode) RT_NOEXCEPT
|
---|
118 | {
|
---|
119 | return (int)(intptr_t)a_pNode - (VERR_HARDAVL_INDEX_OUT_OF_BOUNDS - kErr_IndexOutOfBound);
|
---|
120 | }
|
---|
121 |
|
---|
122 | inline uint32_t ptrToInt(NodeType *a_pNode) RT_NOEXCEPT
|
---|
123 | {
|
---|
124 | if (a_pNode == NULL)
|
---|
125 | return 0;
|
---|
126 | uintptr_t const offNode = (uintptr_t)a_pNode - (uintptr_t)m_paNodes;
|
---|
127 | uintptr_t const idxNode0 = offNode / sizeof(m_paNodes[0]);
|
---|
128 | AssertMsgReturnStmt((offNode % sizeof(m_paNodes[0])) == 0,
|
---|
129 | ("pNode=%p / offNode=%#zx vs m_paNodes=%p L %#x, each %#x bytes\n",
|
---|
130 | a_pNode, offNode, m_paNodes, m_cNodes, sizeof(m_paNodes[0])),
|
---|
131 | m_cErrors++, (uint32_t)kErr_MisalignedPointer);
|
---|
132 | AssertMsgReturnStmt(idxNode0 < m_cNodes,
|
---|
133 | ("pNode=%p vs m_paNodes=%p L %#x\n", a_pNode, m_paNodes, m_cNodes),
|
---|
134 | m_cErrors++, (uint32_t)kErr_PointerOutOfBound);
|
---|
135 | AssertMsgReturnStmt(ASMBitTest(m_pbmAlloc, idxNode0) == false, ("a_pNode=%p idxNode0=%#x\n", a_pNode, idxNode0),
|
---|
136 | m_cErrors++, (uint32_t)kErr_NodeIsFree);
|
---|
137 | return idxNode0 + 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static inline bool isIdxRetOkay(uint32_t a_idxNode) RT_NOEXCEPT
|
---|
141 | {
|
---|
142 | return a_idxNode < (uint32_t)kErr_Last;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static inline int idxErrToStatus(uint32_t a_idxNode) RT_NOEXCEPT
|
---|
146 | {
|
---|
147 | return (int)a_idxNode - (VERR_HARDAVL_INDEX_OUT_OF_BOUNDS - kErr_IndexOutOfBound);
|
---|
148 | }
|
---|
149 |
|
---|
150 | inline bool isIntValid(uint32_t a_idxNode1) RT_NOEXCEPT
|
---|
151 | {
|
---|
152 | return a_idxNode1 <= m_cNodes;
|
---|
153 | }
|
---|
154 |
|
---|
155 | inline int freeNode(NodeType *a_pNode) RT_NOEXCEPT
|
---|
156 | {
|
---|
157 | uint32_t idxNode1 = ptrToInt(a_pNode);
|
---|
158 | if (idxNode1 == (uint32_t)kNilIndex)
|
---|
159 | return 0;
|
---|
160 | if (idxNode1 < (uint32_t)kErr_Last)
|
---|
161 | {
|
---|
162 | AssertMsgReturnStmt(ASMAtomicBitTestAndSet(m_pbmAlloc, idxNode1 - 1) == false,
|
---|
163 | ("a_pNode=%p idxNode1=%#x\n", a_pNode, idxNode1),
|
---|
164 | m_cErrors++, kErr_NodeIsFree);
|
---|
165 | return 0;
|
---|
166 | }
|
---|
167 | return (int)idxNode1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | inline NodeType *allocateNode(void) RT_NOEXCEPT
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * Use the hint first, then scan the whole bitmap.
|
---|
174 | * Note! We don't expect concurrent allocation calls, so no need to repeat.
|
---|
175 | */
|
---|
176 | uint32_t const idxHint = m_idxAllocHint;
|
---|
177 | uint32_t idxNode0;
|
---|
178 | if ( idxHint >= m_cNodes
|
---|
179 | || (int32_t)(idxNode0 = (uint32_t)ASMBitNextSet(m_pbmAlloc, m_cNodes, idxHint)) < 0)
|
---|
180 | idxNode0 = (uint32_t)ASMBitFirstSet(m_pbmAlloc, m_cNodes);
|
---|
181 | if ((int32_t)idxNode0 >= 0)
|
---|
182 | {
|
---|
183 | if (ASMAtomicBitTestAndClear(m_pbmAlloc, idxNode0) == true)
|
---|
184 | {
|
---|
185 | m_idxAllocHint = idxNode0;
|
---|
186 | return &m_paNodes[idxNode0];
|
---|
187 | }
|
---|
188 | AssertMsgFailed(("idxNode0=%#x\n", idxNode0));
|
---|
189 | m_cErrors++;
|
---|
190 | }
|
---|
191 | return NULL;
|
---|
192 | }
|
---|
193 | };
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Placeholder structure for ring-3 slab allocator.
|
---|
198 | */
|
---|
199 | typedef struct RTCHardAvlTreeSlabAllocatorR3_T
|
---|
200 | {
|
---|
201 | /** Pointer to an array of nodes. */
|
---|
202 | RTR3PTR m_paNodes;
|
---|
203 | /** Node allocation bitmap: 1 = free, 0 = allocated. */
|
---|
204 | RTR3PTR m_pbmAlloc;
|
---|
205 | /** Max number of nodes in m_paNodes and valid bits in m_pbmAlloc. */
|
---|
206 | uint32_t m_cNodes;
|
---|
207 | /** Pointer error counter. */
|
---|
208 | uint32_t m_cErrors;
|
---|
209 | /** Allocation hint. */
|
---|
210 | uint32_t m_idxAllocHint;
|
---|
211 | uint32_t m_uPadding;
|
---|
212 | } RTCHardAvlTreeSlabAllocatorR3_T;
|
---|
213 | AssertCompileSize(RTCHardAvlTreeSlabAllocatorR3_T,
|
---|
214 | sizeof(RTCHardAvlTreeSlabAllocator<RTUINT128U>) - (sizeof(void *) - sizeof(RTR3PTR)) * 2);
|
---|
215 |
|
---|
216 | /** @} */
|
---|
217 |
|
---|
218 | #endif /* !IPRT_INCLUDED_cpp_hardavlslaballocator_h */
|
---|
219 |
|
---|