VirtualBox

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

Last change on this file since 95897 was 93710, checked in by vboxsync, 3 years ago

IPRT/hardavl: Added RTCHardAvlTreeSlabAllocatorR3_T placeholder for use in ring-0. bugref:10093

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