VirtualBox

source: vbox/trunk/include/iprt/cpp/hardavlslaballocator.h@ 102361

Last change on this file since 102361 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/** @file
2 * IPRT - Hardened AVL tree slab allocator.
3 */
4
5/*
6 * Copyright (C) 2022-2023 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.h>
43#include <iprt/assert.h>
44#include <iprt/err.h>
45#include <iprt/string.h>
46
47/** @addtogroup grp_rt_cpp_hardavl
48 * @{
49 */
50
51
52/**
53 * Slab allocator for the hardened AVL tree.
54 */
55template<typename NodeType>
56struct RTCHardAvlTreeSlabAllocator
57{
58 /** Pointer to an array of nodes. */
59 NodeType *m_paNodes;
60 /** Node allocation bitmap: 1 = free, 0 = allocated. */
61 uint64_t *m_pbmAlloc;
62 /** Max number of nodes in m_paNodes and valid bits in m_pbmAlloc. */
63 uint32_t m_cNodes;
64 /** Pointer error counter. */
65 uint32_t m_cErrors;
66 /** Allocation hint. */
67 uint32_t m_idxAllocHint;
68 uint32_t m_uPadding;
69
70 enum
71 {
72 kNilIndex = 0,
73 kErr_IndexOutOfBound = -1,
74 kErr_PointerOutOfBound = -2,
75 kErr_MisalignedPointer = -3,
76 kErr_NodeIsFree = -4,
77 kErr_Last = kErr_NodeIsFree
78 };
79
80 RTCHardAvlTreeSlabAllocator() RT_NOEXCEPT
81 : m_paNodes(NULL)
82 , m_pbmAlloc(NULL)
83 , m_cNodes(0)
84 , m_cErrors(0)
85 , m_idxAllocHint(0)
86 , m_uPadding(0)
87 {}
88
89 inline void initSlabAllocator(uint32_t a_cNodes, NodeType *a_paNodes, uint64_t *a_pbmAlloc) RT_NOEXCEPT
90 {
91 m_cNodes = a_cNodes;
92 m_paNodes = a_paNodes;
93 m_pbmAlloc = a_pbmAlloc;
94
95 /* Initialize the allocation bit. */
96 RT_BZERO(a_pbmAlloc, (a_cNodes + 63) / 64 * 8);
97 ASMBitSetRange(a_pbmAlloc, 0, a_cNodes);
98 }
99
100 inline NodeType *ptrFromInt(uint32_t a_idxNode1) RT_NOEXCEPT
101 {
102 if (a_idxNode1 == (uint32_t)kNilIndex)
103 return NULL;
104 AssertMsgReturnStmt(a_idxNode1 <= m_cNodes, ("a_idxNode1=%#x m_cNodes=%#x\n", a_idxNode1, m_cNodes),
105 m_cErrors++, (NodeType *)(intptr_t)kErr_IndexOutOfBound);
106 AssertMsgReturnStmt(ASMBitTest(m_pbmAlloc, a_idxNode1 - 1) == false, ("a_idxNode1=%#x\n", a_idxNode1),
107 m_cErrors++, (NodeType *)(intptr_t)kErr_NodeIsFree);
108 return &m_paNodes[a_idxNode1 - 1];
109 }
110
111 static inline bool isPtrRetOkay(NodeType *a_pNode) RT_NOEXCEPT
112 {
113 return (uintptr_t)a_pNode < (uintptr_t)kErr_Last;
114 }
115
116 static inline int ptrErrToStatus(NodeType *a_pNode) RT_NOEXCEPT
117 {
118 return (int)(intptr_t)a_pNode - (VERR_HARDAVL_INDEX_OUT_OF_BOUNDS - kErr_IndexOutOfBound);
119 }
120
121 inline uint32_t ptrToInt(NodeType *a_pNode) RT_NOEXCEPT
122 {
123 if (a_pNode == NULL)
124 return 0;
125 uintptr_t const offNode = (uintptr_t)a_pNode - (uintptr_t)m_paNodes;
126 uintptr_t const idxNode0 = offNode / sizeof(m_paNodes[0]);
127 AssertMsgReturnStmt((offNode % sizeof(m_paNodes[0])) == 0,
128 ("pNode=%p / offNode=%#zx vs m_paNodes=%p L %#x, each %#x bytes\n",
129 a_pNode, offNode, m_paNodes, m_cNodes, sizeof(m_paNodes[0])),
130 m_cErrors++, (uint32_t)kErr_MisalignedPointer);
131 AssertMsgReturnStmt(idxNode0 < m_cNodes,
132 ("pNode=%p vs m_paNodes=%p L %#x\n", a_pNode, m_paNodes, m_cNodes),
133 m_cErrors++, (uint32_t)kErr_PointerOutOfBound);
134 AssertMsgReturnStmt(ASMBitTest(m_pbmAlloc, idxNode0) == false, ("a_pNode=%p idxNode0=%#x\n", a_pNode, idxNode0),
135 m_cErrors++, (uint32_t)kErr_NodeIsFree);
136 return idxNode0 + 1;
137 }
138
139 static inline bool isIdxRetOkay(uint32_t a_idxNode) RT_NOEXCEPT
140 {
141 return a_idxNode < (uint32_t)kErr_Last;
142 }
143
144 static inline int idxErrToStatus(uint32_t a_idxNode) RT_NOEXCEPT
145 {
146 return (int)a_idxNode - (VERR_HARDAVL_INDEX_OUT_OF_BOUNDS - kErr_IndexOutOfBound);
147 }
148
149 inline bool isIntValid(uint32_t a_idxNode1) RT_NOEXCEPT
150 {
151 return a_idxNode1 <= m_cNodes;
152 }
153
154 inline int freeNode(NodeType *a_pNode) RT_NOEXCEPT
155 {
156 uint32_t idxNode1 = ptrToInt(a_pNode);
157 if (idxNode1 == (uint32_t)kNilIndex)
158 return 0;
159 if (idxNode1 < (uint32_t)kErr_Last)
160 {
161 AssertMsgReturnStmt(ASMAtomicBitTestAndSet(m_pbmAlloc, idxNode1 - 1) == false,
162 ("a_pNode=%p idxNode1=%#x\n", a_pNode, idxNode1),
163 m_cErrors++, kErr_NodeIsFree);
164 return 0;
165 }
166 return (int)idxNode1;
167 }
168
169 inline NodeType *allocateNode(void) RT_NOEXCEPT
170 {
171 /*
172 * Use the hint first, then scan the whole bitmap.
173 * Note! We don't expect concurrent allocation calls, so no need to repeat.
174 */
175 uint32_t const idxHint = m_idxAllocHint;
176 uint32_t idxNode0;
177 if ( idxHint >= m_cNodes
178 || (int32_t)(idxNode0 = (uint32_t)ASMBitNextSet(m_pbmAlloc, m_cNodes, idxHint)) < 0)
179 idxNode0 = (uint32_t)ASMBitFirstSet(m_pbmAlloc, m_cNodes);
180 if ((int32_t)idxNode0 >= 0)
181 {
182 if (ASMAtomicBitTestAndClear(m_pbmAlloc, idxNode0) == true)
183 {
184 m_idxAllocHint = idxNode0;
185 return &m_paNodes[idxNode0];
186 }
187 AssertMsgFailed(("idxNode0=%#x\n", idxNode0));
188 m_cErrors++;
189 }
190 return NULL;
191 }
192};
193
194
195/**
196 * Placeholder structure for ring-3 slab allocator.
197 */
198typedef struct RTCHardAvlTreeSlabAllocatorR3_T
199{
200 /** Pointer to an array of nodes. */
201 RTR3PTR m_paNodes;
202 /** Node allocation bitmap: 1 = free, 0 = allocated. */
203 RTR3PTR m_pbmAlloc;
204 /** Max number of nodes in m_paNodes and valid bits in m_pbmAlloc. */
205 uint32_t m_cNodes;
206 /** Pointer error counter. */
207 uint32_t m_cErrors;
208 /** Allocation hint. */
209 uint32_t m_idxAllocHint;
210 uint32_t m_uPadding;
211} RTCHardAvlTreeSlabAllocatorR3_T;
212AssertCompileSize(RTCHardAvlTreeSlabAllocatorR3_T,
213 sizeof(RTCHardAvlTreeSlabAllocator<RTUINT128U>) - (sizeof(void *) - sizeof(RTR3PTR)) * 2);
214
215/** @} */
216
217#endif /* !IPRT_INCLUDED_cpp_hardavlslaballocator_h */
218
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette