/src/openh264/codec/common/src/memory_align.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 <stdlib.h> |
34 | | #include <string.h> |
35 | | #include "memory_align.h" |
36 | | #include "macros.h" |
37 | | |
38 | | namespace WelsCommon { |
39 | | |
40 | | #ifdef MEMORY_CHECK |
41 | | static FILE* fpMemChkPoint; |
42 | | static uint32_t nCountRequestNum; |
43 | | static int32_t g_iMemoryLength; |
44 | | #endif |
45 | | |
46 | | |
47 | | CMemoryAlign::CMemoryAlign (const uint32_t kuiCacheLineSize) |
48 | | #ifdef MEMORY_MONITOR |
49 | 0 | : m_nMemoryUsageInBytes (0) |
50 | | #endif//MEMORY_MONITOR |
51 | 0 | { |
52 | 0 | if ((kuiCacheLineSize == 0) || (kuiCacheLineSize & 0x0f)) |
53 | 0 | m_nCacheLineSize = 0x10; |
54 | 0 | else |
55 | 0 | m_nCacheLineSize = kuiCacheLineSize; |
56 | 0 | } |
57 | | |
58 | 0 | CMemoryAlign::~CMemoryAlign() { |
59 | 0 | #ifdef MEMORY_MONITOR |
60 | 0 | assert (m_nMemoryUsageInBytes == 0); |
61 | 0 | #endif//MEMORY_MONITOR |
62 | 0 | } |
63 | | |
64 | 0 | void* WelsMalloc (const uint32_t kuiSize, const char* kpTag, const uint32_t kiAlign) { |
65 | 0 | const uint32_t kiSizeOfVoidPointer = sizeof (void**); |
66 | 0 | const uint32_t kiSizeOfInt = sizeof (int32_t); |
67 | 0 | const uint32_t kiAlignedBytes = kiAlign - 1; |
68 | 0 | const uint32_t kiTrialRequestedSize = kuiSize + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; |
69 | 0 | const uint32_t kiActualRequestedSize = kiTrialRequestedSize; |
70 | 0 | const uint32_t kiPayloadSize = kuiSize; |
71 | |
|
72 | 0 | uint8_t* pBuf = (uint8_t*) malloc (kiActualRequestedSize); |
73 | 0 | if (NULL == pBuf) |
74 | 0 | return NULL; |
75 | | |
76 | | #ifdef MEMORY_CHECK |
77 | | if (fpMemChkPoint == NULL) { |
78 | | fpMemChkPoint = fopen ("./enc_mem_check_point.txt", "at+"); |
79 | | nCountRequestNum = 0; |
80 | | } |
81 | | |
82 | | if (fpMemChkPoint != NULL) { |
83 | | if (kpTag != NULL) |
84 | | fprintf (fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d - %s\n", |
85 | | (void*)pBuf, kiActualRequestedSize, kuiSize, nCountRequestNum++, kpTag); |
86 | | else |
87 | | fprintf (fpMemChkPoint, "WelsMalloc(), 0x%x : actual uiSize:\t%d\tbytes, input uiSize: %d bytes, %d \n", (void*)pBuf, |
88 | | kiActualRequestedSize, kuiSize, nCountRequestNum++); |
89 | | fflush (fpMemChkPoint); |
90 | | } |
91 | | #endif |
92 | 0 | uint8_t* pAlignedBuffer; |
93 | 0 | pAlignedBuffer = pBuf + kiAlignedBytes + kiSizeOfVoidPointer + kiSizeOfInt; |
94 | 0 | pAlignedBuffer -= ((uintptr_t) pAlignedBuffer & kiAlignedBytes); |
95 | 0 | * ((void**) (pAlignedBuffer - kiSizeOfVoidPointer)) = pBuf; |
96 | 0 | * ((int32_t*) (pAlignedBuffer - (kiSizeOfVoidPointer + kiSizeOfInt))) = kiPayloadSize; |
97 | |
|
98 | 0 | return pAlignedBuffer; |
99 | 0 | } |
100 | | |
101 | 0 | void WelsFree (void* pPointer, const char* kpTag) { |
102 | 0 | if (pPointer) { |
103 | | #ifdef MEMORY_CHECK |
104 | | if (fpMemChkPoint != NULL) { |
105 | | if (kpTag != NULL) |
106 | | fprintf (fpMemChkPoint, "WelsFree(), 0x%x - %s: \t%d\t bytes \n", (void*) (* (((void**) pPointer) - 1)), kpTag, |
107 | | g_iMemoryLength); |
108 | | else |
109 | | fprintf (fpMemChkPoint, "WelsFree(), 0x%x \n", (void*) (* (((void**) pPointer) - 1))); |
110 | | fflush (fpMemChkPoint); |
111 | | } |
112 | | #endif |
113 | 0 | free (* (((void**) pPointer) - 1)); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | void* CMemoryAlign::WelsMallocz (const uint32_t kuiSize, const char* kpTag) { |
118 | 0 | void* pPointer = WelsMalloc (kuiSize, kpTag); |
119 | 0 | if (NULL == pPointer) { |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | // zero memory |
123 | 0 | memset (pPointer, 0, kuiSize); |
124 | |
|
125 | 0 | return pPointer; |
126 | 0 | } |
127 | | |
128 | 0 | void* CMemoryAlign::WelsMalloc (const uint32_t kuiSize, const char* kpTag) { |
129 | 0 | void* pPointer = WelsCommon::WelsMalloc (kuiSize, kpTag, m_nCacheLineSize); |
130 | 0 | #ifdef MEMORY_MONITOR |
131 | 0 | if (pPointer != NULL) { |
132 | 0 | const int32_t kiMemoryLength = * ((int32_t*) ((uint8_t*)pPointer - sizeof (void**) - sizeof ( |
133 | 0 | int32_t))) + m_nCacheLineSize - 1 + sizeof (void**) + sizeof (int32_t); |
134 | 0 | m_nMemoryUsageInBytes += kiMemoryLength; |
135 | | #ifdef MEMORY_CHECK |
136 | | g_iMemoryLength = kiMemoryLength; |
137 | | #endif |
138 | 0 | } |
139 | 0 | #endif//MEMORY_MONITOR |
140 | 0 | return pPointer; |
141 | 0 | } |
142 | | |
143 | 0 | void CMemoryAlign::WelsFree (void* pPointer, const char* kpTag) { |
144 | 0 | #ifdef MEMORY_MONITOR |
145 | 0 | if (pPointer) { |
146 | 0 | const int32_t kiMemoryLength = * ((int32_t*) ((uint8_t*)pPointer - sizeof (void**) - sizeof ( |
147 | 0 | int32_t))) + m_nCacheLineSize - 1 + sizeof (void**) + sizeof (int32_t); |
148 | 0 | m_nMemoryUsageInBytes -= kiMemoryLength; |
149 | | #ifdef MEMORY_CHECK |
150 | | g_iMemoryLength = kiMemoryLength; |
151 | | #endif |
152 | 0 | } |
153 | 0 | #endif//MEMORY_MONITOR |
154 | 0 | WelsCommon::WelsFree (pPointer, kpTag); |
155 | 0 | } |
156 | | |
157 | 0 | void* WelsMallocz (const uint32_t kuiSize, const char* kpTag) { |
158 | 0 | void* pPointer = WelsMalloc (kuiSize, kpTag, 16); |
159 | 0 | if (NULL == pPointer) { |
160 | 0 | return NULL; |
161 | 0 | } |
162 | 0 | memset (pPointer, 0, kuiSize); |
163 | 0 | return pPointer; |
164 | 0 | } |
165 | | |
166 | 0 | const uint32_t CMemoryAlign::WelsGetCacheLineSize() const { |
167 | 0 | return m_nCacheLineSize; |
168 | 0 | } |
169 | | |
170 | 0 | const uint32_t CMemoryAlign::WelsGetMemoryUsage() const { |
171 | 0 | return m_nMemoryUsageInBytes; |
172 | 0 | } |
173 | | |
174 | | } // end of namespace WelsCommon |