1 | /* $Id: asn1-default-allocator.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, Default Allocator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 | */
|
---|
45 | static 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} */
|
---|
58 | static DECLCALLBACK(void) rtAsn1DefaultAllocator_Free(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation, void *pv)
|
---|
59 | {
|
---|
60 | RTMemFree(pv);
|
---|
61 | pAllocation->cbAllocated = 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnAlloc} */
|
---|
66 | static DECLCALLBACK(int) rtAsn1DefaultAllocator_Alloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
|
---|
67 | void **ppv, size_t cb)
|
---|
68 | {
|
---|
69 | size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cb);
|
---|
70 | void *pv = RTMemAllocZ(cbAlloc);
|
---|
71 | if (pv)
|
---|
72 | {
|
---|
73 | *ppv = pv;
|
---|
74 | pAllocation->cbAllocated = (uint32_t)cbAlloc;
|
---|
75 | return VINF_SUCCESS;
|
---|
76 | }
|
---|
77 | return VERR_NO_MEMORY;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnRealloc} */
|
---|
82 | static DECLCALLBACK(int) rtAsn1DefaultAllocator_Realloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
|
---|
83 | void *pvOld, void **ppvNew, size_t cbNew)
|
---|
84 | {
|
---|
85 | Assert(pvOld);
|
---|
86 | Assert(cbNew);
|
---|
87 | size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cbNew);
|
---|
88 | void *pv = RTMemRealloc(pvOld, cbAlloc);
|
---|
89 | if (pv)
|
---|
90 | {
|
---|
91 | *ppvNew = pv;
|
---|
92 | pAllocation->cbAllocated = (uint32_t)cbAlloc;
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 | return VERR_NO_MEMORY;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /** The default ASN.1 allocator. */
|
---|
100 | RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator =
|
---|
101 | {
|
---|
102 | rtAsn1DefaultAllocator_Free,
|
---|
103 | rtAsn1DefaultAllocator_Alloc,
|
---|
104 | rtAsn1DefaultAllocator_Realloc
|
---|
105 | };
|
---|
106 |
|
---|