|
1 /* |
|
2 * mem.c |
|
3 * |
|
4 * Copyright(c) 1998 - 2010 Texas Instruments. All rights reserved. |
|
5 * All rights reserved. |
|
6 * |
|
7 * This program and the accompanying materials are made available under the |
|
8 * terms of the Eclipse Public License v1.0 or BSD License which accompanies |
|
9 * this distribution. The Eclipse Public License is available at |
|
10 * http://www.eclipse.org/legal/epl-v10.html and the BSD License is as below. |
|
11 * |
|
12 * Redistribution and use in source and binary forms, with or without |
|
13 * modification, are permitted provided that the following conditions |
|
14 * are met: |
|
15 * |
|
16 * * Redistributions of source code must retain the above copyright |
|
17 * notice, this list of conditions and the following disclaimer. |
|
18 * * Redistributions in binary form must reproduce the above copyright |
|
19 * notice, this list of conditions and the following disclaimer in |
|
20 * the documentation and/or other materials provided with the |
|
21 * distribution. |
|
22 * * Neither the name Texas Instruments nor the names of its |
|
23 * contributors may be used to endorse or promote products derived |
|
24 * from this software without specific prior written permission. |
|
25 * |
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
37 */ |
|
38 |
|
39 #define __FILE_ID__ FILE_ID_129 |
|
40 #include "tidef.h" |
|
41 #include "osApi.h" |
|
42 |
|
43 |
|
44 #define MEM_BLOCK_START (('m'<<24) | ('e'<<16) | ('m'<<8) | 's') |
|
45 #define MEM_BLOCK_END (('m'<<24) | ('e'<<16) | ('m'<<8) | 'e') |
|
46 |
|
47 |
|
48 typedef struct |
|
49 { |
|
50 TI_HANDLE hOs; |
|
51 TI_UINT32 uMaxAllocated; |
|
52 TI_UINT32 uCurAllocated; |
|
53 |
|
54 } TMemMng; |
|
55 |
|
56 |
|
57 typedef struct |
|
58 { |
|
59 TI_UINT32 size; |
|
60 TI_UINT32 signature; |
|
61 |
|
62 } TMemBlock; |
|
63 |
|
64 |
|
65 |
|
66 /**************************************************************************************** |
|
67 * * |
|
68 * OS Memory API * |
|
69 * * |
|
70 ****************************************************************************************/ |
|
71 TI_HANDLE mem_Create (TI_HANDLE hOs) |
|
72 { |
|
73 TMemMng *pMemMng = (TMemMng *)os_memoryAlloc (hOs, sizeof(TMemMng),MemoryNormal); |
|
74 |
|
75 if (pMemMng != NULL) |
|
76 { |
|
77 pMemMng->hOs = hOs; |
|
78 pMemMng->uCurAllocated = 0; |
|
79 pMemMng->uMaxAllocated = 0; |
|
80 } |
|
81 |
|
82 return (TI_HANDLE)pMemMng; |
|
83 } |
|
84 |
|
85 |
|
86 TI_STATUS mem_Destroy (TI_HANDLE hMem) |
|
87 { |
|
88 TMemMng *pMemMng = (TMemMng *)hMem; |
|
89 |
|
90 if (pMemMng != NULL) |
|
91 { |
|
92 os_memoryFree (pMemMng->hOs, (void *)pMemMng, sizeof(TMemMng)); |
|
93 } |
|
94 |
|
95 return TI_OK; |
|
96 } |
|
97 |
|
98 |
|
99 |
|
100 /**************************************************************************************** |
|
101 * os_memoryAlloc() |
|
102 **************************************************************************************** |
|
103 DESCRIPTION: Allocates resident (nonpaged) system-space memory. |
|
104 |
|
105 ARGUMENTS: OsContext - our adapter context. |
|
106 Size - Specifies the size, in bytes, to be allocated. |
|
107 |
|
108 RETURN: Pointer to the allocated memory. |
|
109 NULL if there is insufficient memory available. |
|
110 |
|
111 NOTES: With the call to vmalloc it is assumed that this function will |
|
112 never be called in an interrupt context. vmalloc has the potential to |
|
113 sleep the caller while waiting for memory to become available. |
|
114 |
|
115 *****************************************************************************************/ |
|
116 void* mem_Alloc (TI_HANDLE hMem, TI_UINT32 size) |
|
117 { |
|
118 TMemMng *pMemMng = (TMemMng *)hMem; |
|
119 TMemBlock *pMemBlock; |
|
120 TI_UINT32 total = size + sizeof(TMemBlock) + sizeof(TI_UINT32); |
|
121 |
|
122 #ifdef TI_MEM_ALLOC_TRACE |
|
123 os_printf ("mem_Alloc(0x%p, %lu) : %u\n", hMem, size, uTotalSize); |
|
124 #endif |
|
125 |
|
126 pMemBlock = (TMemBlock *) os_memoryAlloc (pMemMng->hOs, total,MemoryNormal); |
|
127 pMemBlock->size = size; |
|
128 pMemBlock->signature = MEM_BLOCK_START; |
|
129 *(TI_UINT32 *)((TI_UINT8 *)pMemBlock + total - sizeof(TI_UINT32)) = MEM_BLOCK_END; |
|
130 |
|
131 pMemMng->uCurAllocated += total; |
|
132 if (pMemMng->uMaxAllocated < pMemMng->uCurAllocated) |
|
133 { |
|
134 pMemMng->uMaxAllocated = pMemMng->uCurAllocated; |
|
135 } |
|
136 |
|
137 return (void*)((TI_UINT8 *)pMemBlock + sizeof(TMemBlock)); |
|
138 } |
|
139 |
|
140 |
|
141 /**************************************************************************************** |
|
142 * os_memoryCAlloc() |
|
143 **************************************************************************************** |
|
144 DESCRIPTION: Allocates an array in memory with elements initialized to 0. |
|
145 |
|
146 ARGUMENTS: OsContext - our adapter context. |
|
147 Number - Number of elements |
|
148 Size - Length in bytes of each element |
|
149 |
|
150 RETURN: None |
|
151 |
|
152 NOTES: |
|
153 *****************************************************************************************/ |
|
154 void* mem_Calloc (TI_HANDLE hMem, TI_UINT32 number, TI_UINT32 size) |
|
155 { |
|
156 TMemMng *pMemMng = (TMemMng *)hMem; |
|
157 void *ptr; |
|
158 TI_UINT32 total; |
|
159 |
|
160 total = number * size; |
|
161 |
|
162 #ifdef TI_MEM_ALLOC_TRACE |
|
163 os_printf ("os_memoryCAlloc(%u, %u) : %u\n", number, size, total); |
|
164 #endif |
|
165 |
|
166 ptr = mem_Alloc (hMem, total); |
|
167 |
|
168 if (ptr != NULL) |
|
169 { |
|
170 os_memorySet (pMemMng->hOs, ptr, 0, total); |
|
171 } |
|
172 |
|
173 return ptr; |
|
174 } |
|
175 |
|
176 |
|
177 |
|
178 /**************************************************************************************** |
|
179 * os_memoryFree() |
|
180 **************************************************************************************** |
|
181 DESCRIPTION: This function releases a block of memory previously allocated with the |
|
182 os_memoryAlloc function. |
|
183 |
|
184 |
|
185 ARGUMENTS: OsContext - our adapter context. |
|
186 pMemPtr - Pointer to the base virtual address of the allocated memory. |
|
187 This address was returned by the os_memoryAlloc function. |
|
188 Size - Specifies the size, in bytes, of the memory block to be released. |
|
189 This parameter must be identical to the Length that was passed to |
|
190 os_memoryAlloc. |
|
191 |
|
192 RETURN: None |
|
193 |
|
194 NOTES: |
|
195 *****************************************************************************************/ |
|
196 void mem_Free (TI_HANDLE hMem, void* ptr, TI_UINT32 size) |
|
197 { |
|
198 TMemMng *pMemMng = (TMemMng *)hMem; |
|
199 TMemBlock *pMemBlock = (TMemBlock *)((TI_UINT8 *)ptr - sizeof(TMemBlock)); |
|
200 |
|
201 #ifdef TI_MEM_ALLOC_TRACE |
|
202 os_printf ("os_memoryFree(%p, %u)\n", ptr, size); |
|
203 #endif |
|
204 |
|
205 if (pMemBlock->signature != MEM_BLOCK_START) |
|
206 { |
|
207 #ifdef TI_MEM_ALLOC_TRACE |
|
208 os_printf ("os_memoryFree: memory block signature is incorrect - 0x%x\n", pMemBlock->signature); |
|
209 #endif |
|
210 } |
|
211 |
|
212 else |
|
213 { |
|
214 *(TI_UINT8 *)(&pMemBlock->signature) = '~'; |
|
215 if (*(TI_UINT32 *)((TI_UINT8 *)pMemBlock + pMemBlock->size + sizeof(TMemBlock)) != MEM_BLOCK_END) |
|
216 { |
|
217 #ifdef TI_MEM_ALLOC_TRACE |
|
218 os_printf ("os_memoryFree: memory block corruption, size - %u\n", pMemBlock->size); |
|
219 #endif |
|
220 } |
|
221 |
|
222 os_memoryFree (pMemMng->hOs, pMemBlock, pMemBlock->signature + sizeof(TMemBlock) + sizeof(TI_UINT32)); |
|
223 |
|
224 pMemMng->uCurAllocated -= size + sizeof(TMemBlock) + sizeof(TI_UINT32); |
|
225 |
|
226 if ((int)pMemMng->uCurAllocated < 0) |
|
227 { |
|
228 #ifdef TI_MEM_ALLOC_TRACE |
|
229 os_printf ("os_memoryFree: memory heap allocation calculation corrupted, size=%u, current=%u\n", |
|
230 size, drv->cur_heap_bytes_allocated); |
|
231 #endif |
|
232 pMemMng->uCurAllocated = 0; |
|
233 } |
|
234 } |
|
235 } |