VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-default-allocator.cpp@ 72426

Last change on this file since 72426 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: asn1-default-allocator.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Default Allocator.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/asn1.h>
33
34#include <iprt/mem.h>
35#include <iprt/err.h>
36#include <iprt/string.h>
37
38
39/**
40 * Aligns allocation sizes a little.
41 *
42 * @returns Aligned size.
43 * @param cb Requested size.
44 */
45static size_t rtAsn1DefaultAllocator_AlignSize(size_t cb)
46{
47 if (cb >= 64)
48 return RT_ALIGN_Z(cb, 64);
49 if (cb >= 32)
50 return RT_ALIGN_Z(cb, 32);
51 if (cb >= 16)
52 return RT_ALIGN_Z(cb, 16);
53 return cb;
54}
55
56
57/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnFree} */
58static DECLCALLBACK(void) rtAsn1DefaultAllocator_Free(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation, void *pv)
59{
60 RT_NOREF_PV(pThis);
61 RTMemFree(pv);
62 pAllocation->cbAllocated = 0;
63}
64
65
66/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnAlloc} */
67static DECLCALLBACK(int) rtAsn1DefaultAllocator_Alloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
68 void **ppv, size_t cb)
69{
70 size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cb);
71 void *pv = RTMemAllocZ(cbAlloc);
72 if (pv)
73 {
74 *ppv = pv;
75 pAllocation->cbAllocated = (uint32_t)cbAlloc;
76 return VINF_SUCCESS;
77 }
78 RT_NOREF_PV(pThis);
79 return VERR_NO_MEMORY;
80}
81
82
83/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnRealloc} */
84static DECLCALLBACK(int) rtAsn1DefaultAllocator_Realloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
85 void *pvOld, void **ppvNew, size_t cbNew)
86{
87 Assert(pvOld);
88 Assert(cbNew);
89 size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cbNew);
90 void *pv = RTMemRealloc(pvOld, cbAlloc);
91 if (pv)
92 {
93 *ppvNew = pv;
94 pAllocation->cbAllocated = (uint32_t)cbAlloc;
95 return VINF_SUCCESS;
96 }
97 RT_NOREF_PV(pThis);
98 return VERR_NO_MEMORY;
99}
100
101
102/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnFreeArray} */
103static DECLCALLBACK(void) rtAsn1DefaultAllocator_FreeArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
104 void **papvArray)
105{
106 RT_NOREF_PV(pThis);
107 Assert(papvArray);
108 Assert(pAllocation->cbEntry);
109
110 uint32_t i = pAllocation->cEntriesAllocated;
111 while (i-- > 0)
112 RTMemFree(papvArray[i]);
113 RTMemFree(papvArray);
114
115 pAllocation->cEntriesAllocated = 0;
116 pAllocation->cPointersAllocated = 0;
117}
118
119
120/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnGrowArray} */
121static DECLCALLBACK(int) rtAsn1DefaultAllocator_GrowArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
122 void ***ppapvArray, uint32_t cMinEntries)
123{
124 RT_NOREF_PV(pThis);
125
126 /*
127 * Resize the pointer array. We do chunks of 64 bytes for now.
128 */
129 void **papvArray = *ppapvArray;
130 uint32_t cPointers = RT_ALIGN_32(cMinEntries, 64 / sizeof(void *));
131 if (cPointers > pAllocation->cPointersAllocated)
132 {
133 void *pvPointers = RTMemRealloc(papvArray, cPointers * sizeof(void *));
134 if (pvPointers)
135 { /* likely */ }
136 else if (cMinEntries > pAllocation->cPointersAllocated)
137 {
138 cPointers = cMinEntries;
139 pvPointers = RTMemRealloc(*ppapvArray, cPointers * sizeof(void *));
140 if (!pvPointers)
141 return VERR_NO_MEMORY;
142 }
143 else
144 {
145 cPointers = pAllocation->cPointersAllocated;
146 pvPointers = papvArray;
147 }
148
149 *ppapvArray = papvArray = (void **)pvPointers;
150 RT_BZERO(&papvArray[pAllocation->cPointersAllocated], (cPointers - pAllocation->cPointersAllocated) * sizeof(void *));
151 pAllocation->cPointersAllocated = cPointers;
152 }
153
154 /*
155 * Add more entries. Do multiple as the array grows.
156 *
157 * Note! We could possibly optimize this by allocating slabs of entries and
158 * slice them up. However, keep things as simple as possible for now.
159 */
160 uint32_t cEntries = cMinEntries;
161 if (cEntries > 2)
162 {
163 if (cEntries > 8)
164 cEntries = RT_ALIGN_32(cEntries, 4);
165 else
166 cEntries = RT_ALIGN_32(cEntries, 2);
167 cEntries = RT_MIN(cEntries, cPointers);
168 Assert(cEntries >= cMinEntries);
169 }
170 Assert(cEntries <= pAllocation->cPointersAllocated);
171
172 while (pAllocation->cEntriesAllocated < cEntries)
173 {
174 void *pv;
175 papvArray[pAllocation->cEntriesAllocated] = pv = RTMemAllocZ(pAllocation->cbEntry);
176 if (pv)
177 pAllocation->cEntriesAllocated++;
178 else if (pAllocation->cEntriesAllocated >= cMinEntries)
179 break;
180 else
181 return VERR_NO_MEMORY;
182 }
183
184 return VINF_SUCCESS;
185}
186
187
188/** @interface_method_impl{RTASN1ALLOCATORVTABLE,pfnShrinkArray} */
189static DECLCALLBACK(void) rtAsn1DefaultAllocator_ShrinkArray(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ARRAYALLOCATION pAllocation,
190 void ***ppapvArray, uint32_t cNew, uint32_t cCurrent)
191{
192 RT_NOREF_PV(pThis);
193
194 /*
195 * For now we only zero the entries being removed.
196 */
197 void **papvArray = *ppapvArray;
198 while (cNew < cCurrent)
199 {
200 RT_BZERO(papvArray[cNew], pAllocation->cbEntry);
201 cNew++;
202 }
203}
204
205
206
207/** The default ASN.1 allocator. */
208#if 1 || !defined(IN_RING3) || defined(DOXYGEN_RUNNING)
209RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator =
210#else
211RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocatorDisabled =
212#endif
213{
214 rtAsn1DefaultAllocator_Free,
215 rtAsn1DefaultAllocator_Alloc,
216 rtAsn1DefaultAllocator_Realloc,
217 rtAsn1DefaultAllocator_FreeArray,
218 rtAsn1DefaultAllocator_GrowArray,
219 rtAsn1DefaultAllocator_ShrinkArray
220};
221
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