summaryrefslogtreecommitdiff
path: root/audio_codec/libraac/memory_utils.c (plain)
blob: b7d46b29c57560b92a47b46eff7a17ea3cff1805
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: memory_utils.c,v 1.1.1.1.2.1 2005/05/04 18:21:23 hubbe Exp $
3 *
4 * REALNETWORKS CONFIDENTIAL--NOT FOR DISTRIBUTION IN SOURCE CODE FORM
5 * Portions Copyright (c) 1995-2005 RealNetworks, Inc.
6 * All Rights Reserved.
7 *
8 * The contents of this file, and the files included with this file,
9 * are subject to the current version of the Real Format Source Code
10 * Porting and Optimization License, available at
11 * https://helixcommunity.org/2005/license/realformatsource (unless
12 * RealNetworks otherwise expressly agrees in writing that you are
13 * subject to a different license). You may also obtain the license
14 * terms directly from RealNetworks. You may not use this file except
15 * in compliance with the Real Format Source Code Porting and
16 * Optimization License. There are no redistribution rights for the
17 * source code of this file. Please see the Real Format Source Code
18 * Porting and Optimization License for the rights, obligations and
19 * limitations governing use of the contents of the file.
20 *
21 * RealNetworks is the developer of the Original Code and owns the
22 * copyrights in the portions it created.
23 *
24 * This file, and the files included with this file, is distributed and
25 * made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND,
26 * EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL
27 * SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT
29 * OR NON-INFRINGEMENT.
30 *
31 * Technology Compatibility Kit Test Suite(s) Location:
32 * https://rarvcode-tck.helixcommunity.org
33 *
34 * Contributor(s):
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include <memory.h>
39#include "../include/helix_types.h"
40#include "../include/helix_result.h"
41#include "../include/rm_memory.h"
42#include "../include/memory_utils.h"
43
44HX_RESULT rm_enforce_buffer_min_size(void* pUserMem,
45 rm_malloc_func_ptr fpMalloc,
46 rm_free_func_ptr fpFree,
47 BYTE** ppBuf,
48 UINT32* pulCurLen,
49 UINT32 ulReqLen)
50{
51 HX_RESULT retVal = HXR_OUTOFMEMORY;
52
53 if (fpMalloc && fpFree && ppBuf && pulCurLen) {
54 if (ulReqLen > *pulCurLen) {
55 /* Allocate a new buffer */
56 BYTE* pBuf = fpMalloc(pUserMem, ulReqLen);
57 if (pBuf) {
58 /* Was the old buffer allocated? */
59 if (*ppBuf && *pulCurLen > 0) {
60 /* Copy the old buffer into the new one */
61 memcpy(pBuf, *ppBuf, *pulCurLen);
62 /* Free the old buffer */
63 fpFree(pUserMem, *ppBuf);
64 }
65 /* Assign the buffer out parameter */
66 *ppBuf = pBuf;
67 /* Change the current size to the requested size */
68 *pulCurLen = ulReqLen;
69 /* Clear the return value */
70 retVal = HXR_OK;
71 }
72 } else {
73 /* Current buffer size is adequate - don't
74 * have to do anything here.
75 */
76 retVal = HXR_OK;
77 }
78 }
79
80 return retVal;
81}
82
83BYTE* copy_buffer(void* pUserMem,
84 rm_malloc_func_ptr fpMalloc,
85 BYTE* pBuf,
86 UINT32 ulLen)
87{
88 BYTE* pRet = HXNULL;
89
90 if (fpMalloc && pBuf && ulLen) {
91 /* Allocate a buffer */
92 pRet = (BYTE*) fpMalloc(pUserMem, ulLen);
93 if (pRet) {
94 /* Copy the buffer */
95 memcpy(pRet, pBuf, ulLen);
96 }
97 }
98
99 return pRet;
100}
101