VirtualBox

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

Last change on this file since 51770 was 51770, checked in by vboxsync, 11 years ago

Merged in iprt++ dev branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/* $Id: asn1-default-allocator.cpp 51770 2014-07-01 18:14:02Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Default Allocator.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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* Header Files *
29*******************************************************************************/
30#if defined(DEBUG_bird) && !defined(IPRT_NO_CRT)
31# define RTMEM_WRAP_TO_EF_APIS
32#endif
33#include "internal/iprt.h"
34#include <iprt/asn1.h>
35
36#include <iprt/mem.h>
37#include <iprt/err.h>
38#include <iprt/string.h>
39
40
41/**
42 * Aligns allocation sizes a little.
43 *
44 * @returns Aligned size.
45 * @param cb Requested size.
46 */
47static size_t rtAsn1DefaultAllocator_AlignSize(size_t cb)
48{
49 if (cb >= 64)
50 return RT_ALIGN_Z(cb, 64);
51 if (cb >= 32)
52 return RT_ALIGN_Z(cb, 32);
53 if (cb >= 16)
54 return RT_ALIGN_Z(cb, 16);
55 return cb;
56}
57
58
59/** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnFree} */
60static DECLCALLBACK(void) rtAsn1DefaultAllocator_Free(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation, void *pv)
61{
62 RTMemFree(pv);
63 pAllocation->cbAllocated = 0;
64}
65
66
67/** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnAlloc} */
68static DECLCALLBACK(int) rtAsn1DefaultAllocator_Alloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
69 void **ppv, size_t cb)
70{
71 size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cb);
72 void *pv = RTMemAllocZ(cbAlloc);
73 if (pv)
74 {
75 *ppv = pv;
76 pAllocation->cbAllocated = (uint32_t)cbAlloc;
77 return VINF_SUCCESS;
78 }
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 return VERR_NO_MEMORY;
98}
99
100
101/** The default ASN.1 allocator. */
102RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator =
103{
104 rtAsn1DefaultAllocator_Free,
105 rtAsn1DefaultAllocator_Alloc,
106 rtAsn1DefaultAllocator_Realloc
107};
108
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