VirtualBox

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

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

Drop electric fences before looking for leaks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1/* $Id: asn1-default-allocator.cpp 51915 2014-07-08 01:36:31Z 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#include "internal/iprt.h"
31#include <iprt/asn1.h>
32
33#include <iprt/mem.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36
37
38/**
39 * Aligns allocation sizes a little.
40 *
41 * @returns Aligned size.
42 * @param cb Requested size.
43 */
44static size_t rtAsn1DefaultAllocator_AlignSize(size_t cb)
45{
46 if (cb >= 64)
47 return RT_ALIGN_Z(cb, 64);
48 if (cb >= 32)
49 return RT_ALIGN_Z(cb, 32);
50 if (cb >= 16)
51 return RT_ALIGN_Z(cb, 16);
52 return cb;
53}
54
55
56/** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnFree} */
57static DECLCALLBACK(void) rtAsn1DefaultAllocator_Free(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation, void *pv)
58{
59 RTMemFree(pv);
60 pAllocation->cbAllocated = 0;
61}
62
63
64/** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnAlloc} */
65static DECLCALLBACK(int) rtAsn1DefaultAllocator_Alloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
66 void **ppv, size_t cb)
67{
68 size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cb);
69 void *pv = RTMemAllocZ(cbAlloc);
70 if (pv)
71 {
72 *ppv = pv;
73 pAllocation->cbAllocated = (uint32_t)cbAlloc;
74 return VINF_SUCCESS;
75 }
76 return VERR_NO_MEMORY;
77}
78
79
80/** @interface_method_impl{RTASN1ALLOCATORVTABLE, pfnRealloc} */
81static DECLCALLBACK(int) rtAsn1DefaultAllocator_Realloc(PCRTASN1ALLOCATORVTABLE pThis, PRTASN1ALLOCATION pAllocation,
82 void *pvOld, void **ppvNew, size_t cbNew)
83{
84 Assert(pvOld);
85 Assert(cbNew);
86 size_t cbAlloc = rtAsn1DefaultAllocator_AlignSize(cbNew);
87 void *pv = RTMemRealloc(pvOld, cbAlloc);
88 if (pv)
89 {
90 *ppvNew = pv;
91 pAllocation->cbAllocated = (uint32_t)cbAlloc;
92 return VINF_SUCCESS;
93 }
94 return VERR_NO_MEMORY;
95}
96
97
98/** The default ASN.1 allocator. */
99RT_DECL_DATA_CONST(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator =
100{
101 rtAsn1DefaultAllocator_Free,
102 rtAsn1DefaultAllocator_Alloc,
103 rtAsn1DefaultAllocator_Realloc
104};
105
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