VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-efence-allocator.cpp

Last change on this file was 98103, checked in by vboxsync, 16 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.2 KB
Line 
1/* $Id: asn1-efence-allocator.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Electric Fense Allocator.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/asn1.h>
43
44#include <iprt/mem.h>
45#include <iprt/errcore.h>
46#include <iprt/string.h>
47
48
49/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnFree} */
50static DECLCALLBACK(void) rtAsn1EFenceAllocator_Free(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation, void *pv)
51{
52 RT_NOREF_PV(pThis);
53 RTMemEfFreeNP(pv);
54 pAllocation->cbAllocated = 0;
55}
56
57
58/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnAlloc} */
59static DECLCALLBACK(int) rtAsn1EFenceAllocator_Alloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
60 void **ppv, size_t cb)
61{
62 void *pv = RTMemEfAllocZNP(cb, RTMEM_TAG);
63 if (pv)
64 {
65 *ppv = pv;
66 pAllocation->cbAllocated = (uint32_t)cb;
67 return VINF_SUCCESS;
68 }
69 RT_NOREF_PV(pThis);
70 return VERR_NO_MEMORY;
71}
72
73
74/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnRealloc} */
75static DECLCALLBACK(int) rtAsn1EFenceAllocator_Realloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
76 void *pvOld, void **ppvNew, size_t cbNew)
77{
78 Assert(pvOld);
79 Assert(cbNew);
80 void *pv = RTMemEfReallocNP(pvOld, cbNew, RTMEM_TAG);
81 if (pv)
82 {
83 *ppvNew = pv;
84 pAllocation->cbAllocated = (uint32_t)cbNew;
85 return VINF_SUCCESS;
86 }
87 RT_NOREF_PV(pThis);
88 return VERR_NO_MEMORY;
89}
90
91
92/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnFreeArray} */
93static DECLCALLBACK(void) rtAsn1EFenceAllocator_FreeArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
94 void **papvArray)
95{
96 RT_NOREF_PV(pThis);
97 Assert(papvArray);
98 Assert(pAllocation->cbEntry);
99 Assert(pAllocation->cEntriesAllocated <= pAllocation->cPointersAllocated);
100
101 uint32_t i = pAllocation->cEntriesAllocated;
102 while (i-- > 0)
103 {
104 RTMemEfFreeNP(papvArray[i]);
105 papvArray[i] = NULL;
106 }
107 RTMemEfFreeNP(papvArray);
108
109 pAllocation->cEntriesAllocated = 0;
110 pAllocation->cPointersAllocated = 0;
111}
112
113
114/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnGrowArray} */
115static DECLCALLBACK(int) rtAsn1EFenceAllocator_GrowArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
116 void ***ppapvArray, uint32_t cMinEntries)
117{
118 RT_NOREF_PV(pThis);
119 Assert(pAllocation->cbEntry);
120 Assert(pAllocation->cEntriesAllocated <= pAllocation->cPointersAllocated);
121
122 /*
123 * Resize the pointer array.
124 */
125 void **papvArray = *ppapvArray;
126 void *pvPointers = RTMemEfReallocNP(papvArray, cMinEntries * sizeof(void *), RTMEM_TAG);
127 if (pvPointers)
128 {
129 *ppapvArray = papvArray = (void **)pvPointers;
130 if (cMinEntries > pAllocation->cPointersAllocated) /* possible on multiple shrink failures */
131 RT_BZERO(&papvArray[pAllocation->cPointersAllocated],
132 (cMinEntries - pAllocation->cPointersAllocated) * sizeof(void *));
133 else
134 AssertFailed();
135 pAllocation->cPointersAllocated = cMinEntries;
136 }
137 else if (cMinEntries > pAllocation->cPointersAllocated)
138 return VERR_NO_MEMORY;
139 /* else: possible but unlikely */
140
141 /*
142 * Add more entries.
143 */
144 while (pAllocation->cEntriesAllocated < cMinEntries)
145 {
146 void *pv;
147 papvArray[pAllocation->cEntriesAllocated] = pv = RTMemEfAllocZNP(pAllocation->cbEntry, RTMEM_TAG);
148 if (pv)
149 pAllocation->cEntriesAllocated++;
150 else
151 return VERR_NO_MEMORY;
152 }
153
154 return VINF_SUCCESS;
155}
156
157
158/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnShrinkArray} */
159static DECLCALLBACK(void) rtAsn1EFenceAllocator_ShrinkArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
160 void ***ppapvArray, uint32_t cNew, uint32_t cCurrent)
161{
162 RT_NOREF_PV(pThis);
163 Assert(pAllocation->cbEntry);
164 Assert(pAllocation->cEntriesAllocated <= pAllocation->cPointersAllocated);
165
166 /*
167 * We always free and resize.
168 */
169 Assert(pAllocation->cEntriesAllocated == cCurrent);
170 Assert(cNew < cCurrent);
171
172 /* Free entries. */
173 void **papvArray = *ppapvArray;
174 while (cCurrent-- > cNew)
175 {
176 RTMemEfFreeNP(papvArray[cCurrent]);
177 papvArray[cCurrent] = NULL;
178 }
179 pAllocation->cEntriesAllocated = cNew;
180
181 /* Try resize pointer array. Failure here is a genuine possibility since the
182 efence code will try allocate a new block. This causes extra fun in the
183 grow method above. */
184 void *pvPointers = RTMemEfReallocNP(papvArray, cNew * sizeof(void *), RTMEM_TAG);
185 if (pvPointers)
186 {
187 *ppapvArray = (void **)pvPointers;
188 pAllocation->cPointersAllocated = cNew;
189 }
190}
191
192
193/** The Electric Fence ASN.1 allocator. */
194RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1EFenceAllocator =
195{
196 rtAsn1EFenceAllocator_Free,
197 rtAsn1EFenceAllocator_Alloc,
198 rtAsn1EFenceAllocator_Realloc,
199 rtAsn1EFenceAllocator_FreeArray,
200 rtAsn1EFenceAllocator_GrowArray,
201 rtAsn1EFenceAllocator_ShrinkArray
202};
203
204#if 0 && defined(IN_RING3) /* for efence testing */
205RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator =
206{
207 rtAsn1EFenceAllocator_Free,
208 rtAsn1EFenceAllocator_Alloc,
209 rtAsn1EFenceAllocator_Realloc,
210 rtAsn1EFenceAllocator_FreeArray,
211 rtAsn1EFenceAllocator_GrowArray,
212 rtAsn1EFenceAllocator_ShrinkArray
213};
214#endif
215
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use