4 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
17 * * Neither the name Texas Instruments nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #define __FILE_ID__ FILE_ID_129
39 #define MEM_BLOCK_START (('m'<<24) | ('e'<<16) | ('m'<<8) | 's')
40 #define MEM_BLOCK_END (('m'<<24) | ('e'<<16) | ('m'<<8) | 'e')
46 TI_UINT32 uMaxAllocated;
47 TI_UINT32 uCurAllocated;
61 /****************************************************************************************
65 ****************************************************************************************/
66 TI_HANDLE mem_Create (TI_HANDLE hOs)
68 TMemMng *pMemMng = (TMemMng *)os_memoryAlloc (hOs, sizeof(TMemMng));
73 pMemMng->uCurAllocated = 0;
74 pMemMng->uMaxAllocated = 0;
77 return (TI_HANDLE)pMemMng;
81 TI_STATUS mem_Destroy (TI_HANDLE hMem)
83 TMemMng *pMemMng = (TMemMng *)hMem;
87 os_memoryFree (pMemMng->hOs, (void *)pMemMng, sizeof(TMemMng));
95 /****************************************************************************************
97 ****************************************************************************************
98 DESCRIPTION: Allocates resident (nonpaged) system-space memory.
100 ARGUMENTS: OsContext - our adapter context.
101 Size - Specifies the size, in bytes, to be allocated.
103 RETURN: Pointer to the allocated memory.
104 NULL if there is insufficient memory available.
106 NOTES: With the call to vmalloc it is assumed that this function will
107 never be called in an interrupt context. vmalloc has the potential to
108 sleep the caller while waiting for memory to become available.
110 *****************************************************************************************/
111 void* mem_Alloc (TI_HANDLE hMem, TI_UINT32 size)
113 TMemMng *pMemMng = (TMemMng *)hMem;
114 TMemBlock *pMemBlock;
115 TI_UINT32 total = size + sizeof(TMemBlock) + sizeof(TI_UINT32);
117 #ifdef TI_MEM_ALLOC_TRACE
118 os_printf ("mem_Alloc(0x%p, %lu) : %u\n", hMem, size, uTotalSize);
121 pMemBlock = (TMemBlock *) os_memoryAlloc (pMemMng->hOs, total);
122 pMemBlock->size = size;
123 pMemBlock->signature = MEM_BLOCK_START;
124 *(TI_UINT32 *)((TI_UINT8 *)pMemBlock + total - sizeof(TI_UINT32)) = MEM_BLOCK_END;
126 pMemMng->uCurAllocated += total;
127 if (pMemMng->uMaxAllocated < pMemMng->uCurAllocated)
129 pMemMng->uMaxAllocated = pMemMng->uCurAllocated;
132 return (void*)((TI_UINT8 *)pMemBlock + sizeof(TMemBlock));
136 /****************************************************************************************
138 ****************************************************************************************
139 DESCRIPTION: Allocates an array in memory with elements initialized to 0.
141 ARGUMENTS: OsContext - our adapter context.
142 Number - Number of elements
143 Size - Length in bytes of each element
148 *****************************************************************************************/
149 void* mem_Calloc (TI_HANDLE hMem, TI_UINT32 number, TI_UINT32 size)
151 TMemMng *pMemMng = (TMemMng *)hMem;
155 total = number * size;
157 #ifdef TI_MEM_ALLOC_TRACE
158 os_printf ("os_memoryCAlloc(%u, %u) : %u\n", number, size, total);
161 ptr = mem_Alloc (hMem, total);
165 os_memorySet (pMemMng->hOs, ptr, 0, total);
173 /****************************************************************************************
175 ****************************************************************************************
176 DESCRIPTION: This function releases a block of memory previously allocated with the
177 os_memoryAlloc function.
180 ARGUMENTS: OsContext - our adapter context.
181 pMemPtr - Pointer to the base virtual address of the allocated memory.
182 This address was returned by the os_memoryAlloc function.
183 Size - Specifies the size, in bytes, of the memory block to be released.
184 This parameter must be identical to the Length that was passed to
190 *****************************************************************************************/
191 void mem_Free (TI_HANDLE hMem, void* ptr, TI_UINT32 size)
193 TMemMng *pMemMng = (TMemMng *)hMem;
194 TMemBlock *pMemBlock = (TMemBlock *)((TI_UINT8 *)ptr - sizeof(TMemBlock));
196 #ifdef TI_MEM_ALLOC_TRACE
197 os_printf ("os_memoryFree(%p, %u)\n", ptr, size);
200 if (pMemBlock->signature != MEM_BLOCK_START)
202 #ifdef TI_MEM_ALLOC_TRACE
203 os_printf ("os_memoryFree: memory block signature is incorrect - 0x%x\n", pMemBlock->signature);
209 *(TI_UINT8 *)(&pMemBlock->signature) = '~';
210 if (*(TI_UINT32 *)((TI_UINT8 *)pMemBlock + pMemBlock->size + sizeof(TMemBlock)) != MEM_BLOCK_END)
212 #ifdef TI_MEM_ALLOC_TRACE
213 os_printf ("os_memoryFree: memory block corruption, size - %u\n", pMemBlock->size);
217 os_memoryFree (pMemMng->hOs, pMemBlock, pMemBlock->signature + sizeof(TMemBlock) + sizeof(TI_UINT32));
219 pMemMng->uCurAllocated -= size + sizeof(TMemBlock) + sizeof(TI_UINT32);
221 if ((int)pMemMng->uCurAllocated < 0)
223 #ifdef TI_MEM_ALLOC_TRACE
224 os_printf ("os_memoryFree: memory heap allocation calculation corrupted, size=%u, current=%u\n",
225 size, drv->cur_heap_bytes_allocated);
227 pMemMng->uCurAllocated = 0;