/src/openh264/codec/processing/src/common/memory.cpp
Line | Count | Source |
1 | | /*! |
2 | | * \copy |
3 | | * Copyright (c) 2013, Cisco Systems |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * * Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * * Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in |
15 | | * the documentation and/or other materials provided with the |
16 | | * distribution. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
21 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
22 | | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
24 | | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
28 | | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | | * POSSIBILITY OF SUCH DAMAGE. |
30 | | * |
31 | | */ |
32 | | |
33 | | #include "memory.h" |
34 | | |
35 | | WELSVP_NAMESPACE_BEGIN |
36 | | ///////////////////////////////////////////////////////////////////////////////// |
37 | | |
38 | 0 | void* WelsMalloc (const uint32_t kuiSize, char* pTag) { |
39 | 0 | const int32_t kiSizeVoidPointer = sizeof (void**); |
40 | 0 | const int32_t kiSizeInt32 = sizeof (int32_t); |
41 | 0 | const int32_t kiAlignedBytes = ALIGNBYTES - 1; |
42 | |
|
43 | 0 | uint8_t* pBuf = (uint8_t*) ::malloc (kuiSize + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32); |
44 | 0 | uint8_t* pAlignedBuf = NULL; |
45 | |
|
46 | 0 | if (NULL == pBuf) |
47 | 0 | return NULL; |
48 | | |
49 | | // to fill zero values |
50 | 0 | WelsMemset (pBuf, 0, kuiSize + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32); |
51 | |
|
52 | 0 | pAlignedBuf = pBuf + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32; |
53 | 0 | pAlignedBuf -= WelsCastFromPointer (pAlignedBuf) & kiAlignedBytes; |
54 | 0 | * ((void**) (pAlignedBuf - kiSizeVoidPointer)) = pBuf; |
55 | 0 | * ((int32_t*) (pAlignedBuf - (kiSizeVoidPointer + kiSizeInt32))) = kuiSize; |
56 | |
|
57 | 0 | return (pAlignedBuf); |
58 | 0 | } |
59 | | |
60 | | ///////////////////////////////////////////////////////////////////////////// |
61 | | |
62 | 0 | void WelsFree (void* pPointer, char* pTag) { |
63 | 0 | if (pPointer) { |
64 | 0 | ::free (* (((void**) pPointer) - 1)); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | ///////////////////////////////////////////////////////////////////////////// |
69 | | |
70 | 0 | void* InternalReallocate (void* pPointer, const uint32_t kuiSize, char* pTag) { |
71 | 0 | uint32_t iOldSize = 0; |
72 | 0 | uint8_t* pNew = NULL; |
73 | 0 | if (pPointer != NULL) |
74 | 0 | iOldSize = * ((int32_t*) ((uint8_t*) pPointer - sizeof (void**) - sizeof (int32_t))); |
75 | 0 | else |
76 | 0 | return WelsMalloc (kuiSize, pTag); |
77 | | |
78 | 0 | pNew = (uint8_t*)WelsMalloc (kuiSize, pTag); |
79 | 0 | if (0 == pNew) { |
80 | 0 | if (iOldSize > 0 && kuiSize > 0 && iOldSize >= kuiSize) |
81 | 0 | return (pPointer); |
82 | 0 | return 0; |
83 | 0 | } else if (iOldSize > 0 && kuiSize > 0) |
84 | 0 | memcpy (pNew, pPointer, (iOldSize < kuiSize) ? iOldSize : kuiSize); |
85 | 0 | else |
86 | 0 | return 0; |
87 | | |
88 | 0 | WelsFree (pPointer, pTag); |
89 | 0 | return (pNew); |
90 | 0 | } |
91 | | |
92 | | ///////////////////////////////////////////////////////////////////////////// |
93 | | |
94 | 0 | void* WelsRealloc (void* pPointer, uint32_t* pRealSize, const uint32_t kuiSize, char* pTag) { |
95 | 0 | const uint32_t kuiOldSize = *pRealSize; |
96 | 0 | uint32_t kuiNewSize = 0; |
97 | 0 | void* pLocalPointer = NULL; |
98 | 0 | if (kuiOldSize >= kuiSize) // large enough of original block, so do nothing |
99 | 0 | return (pPointer); |
100 | | |
101 | | // new request |
102 | 0 | kuiNewSize = kuiSize + 15; |
103 | 0 | kuiNewSize -= (kuiNewSize & 15); |
104 | 0 | kuiNewSize += 32; |
105 | |
|
106 | 0 | pLocalPointer = InternalReallocate (pPointer, kuiNewSize, pTag); |
107 | 0 | if (NULL != pLocalPointer) { |
108 | 0 | *pRealSize = kuiNewSize; |
109 | 0 | return (pLocalPointer); |
110 | 0 | } else { |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | 0 | return NULL; // something wrong |
115 | 0 | } |
116 | | |
117 | | WELSVP_NAMESPACE_END |