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
WelsMalloc(const uint32_t kuiSize,char * pTag)38 void* WelsMalloc (const uint32_t kuiSize, char* pTag) {
39 const int32_t kiSizeVoidPointer = sizeof (void**);
40 const int32_t kiSizeInt32 = sizeof (int32_t);
41 const int32_t kiAlignedBytes = ALIGNBYTES - 1;
42
43 uint8_t* pBuf = (uint8_t*) ::malloc (kuiSize + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32);
44 uint8_t* pAlignedBuf = NULL;
45
46 if (NULL == pBuf)
47 return NULL;
48
49 // to fill zero values
50 WelsMemset (pBuf, 0, kuiSize + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32);
51
52 pAlignedBuf = pBuf + kiAlignedBytes + kiSizeVoidPointer + kiSizeInt32;
53 pAlignedBuf -= WelsCastFromPointer (pAlignedBuf) & kiAlignedBytes;
54 * ((void**) (pAlignedBuf - kiSizeVoidPointer)) = pBuf;
55 * ((int32_t*) (pAlignedBuf - (kiSizeVoidPointer + kiSizeInt32))) = kuiSize;
56
57 return (pAlignedBuf);
58 }
59
60 /////////////////////////////////////////////////////////////////////////////
61
WelsFree(void * pPointer,char * pTag)62 void WelsFree (void* pPointer, char* pTag) {
63 if (pPointer) {
64 ::free (* (((void**) pPointer) - 1));
65 }
66 }
67
68 /////////////////////////////////////////////////////////////////////////////
69
InternalReallocate(void * pPointer,const uint32_t kuiSize,char * pTag)70 void* InternalReallocate (void* pPointer, const uint32_t kuiSize, char* pTag) {
71 uint32_t iOldSize = 0;
72 uint8_t* pNew = NULL;
73 if (pPointer != NULL)
74 iOldSize = * ((int32_t*) ((uint8_t*) pPointer - sizeof (void**) - sizeof (int32_t)));
75 else
76 return WelsMalloc (kuiSize, pTag);
77
78 pNew = (uint8_t*)WelsMalloc (kuiSize, pTag);
79 if (0 == pNew) {
80 if (iOldSize > 0 && kuiSize > 0 && iOldSize >= kuiSize)
81 return (pPointer);
82 return 0;
83 } else if (iOldSize > 0 && kuiSize > 0)
84 memcpy (pNew, pPointer, (iOldSize < kuiSize) ? iOldSize : kuiSize);
85 else
86 return 0;
87
88 WelsFree (pPointer, pTag);
89 return (pNew);
90 }
91
92 /////////////////////////////////////////////////////////////////////////////
93
WelsRealloc(void * pPointer,uint32_t * pRealSize,const uint32_t kuiSize,char * pTag)94 void* WelsRealloc (void* pPointer, uint32_t* pRealSize, const uint32_t kuiSize, char* pTag) {
95 const uint32_t kuiOldSize = *pRealSize;
96 uint32_t kuiNewSize = 0;
97 void* pLocalPointer = NULL;
98 if (kuiOldSize >= kuiSize) // large enough of original block, so do nothing
99 return (pPointer);
100
101 // new request
102 kuiNewSize = kuiSize + 15;
103 kuiNewSize -= (kuiNewSize & 15);
104 kuiNewSize += 32;
105
106 pLocalPointer = InternalReallocate (pPointer, kuiNewSize, pTag);
107 if (NULL != pLocalPointer) {
108 *pRealSize = kuiNewSize;
109 return (pLocalPointer);
110 } else {
111 return NULL;
112 }
113
114 return NULL; // something wrong
115 }
116
117 WELSVP_NAMESPACE_END
118