• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * mem.c
3  *
4  * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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.
32  */
33 
34 #define __FILE_ID__  FILE_ID_129
35 #include "tidef.h"
36 #include "osApi.h"
37 
38 
39 #define MEM_BLOCK_START  (('m'<<24) | ('e'<<16) | ('m'<<8) | 's')
40 #define MEM_BLOCK_END    (('m'<<24) | ('e'<<16) | ('m'<<8) | 'e')
41 
42 
43 typedef struct
44 {
45     TI_HANDLE hOs;
46     TI_UINT32 uMaxAllocated;
47     TI_UINT32 uCurAllocated;
48 
49 } TMemMng;
50 
51 
52 typedef struct
53 {
54     TI_UINT32 size;
55     TI_UINT32 signature;
56 
57 } TMemBlock;
58 
59 
60 
61 /****************************************************************************************
62  *                                                                                      *
63  *                      OS Memory API                                                   *
64  *                                                                                      *
65  ****************************************************************************************/
mem_Create(TI_HANDLE hOs)66 TI_HANDLE mem_Create (TI_HANDLE hOs)
67 {
68     TMemMng *pMemMng = (TMemMng *)os_memoryAlloc (hOs, sizeof(TMemMng));
69 
70     if (pMemMng != NULL)
71     {
72         pMemMng->hOs = hOs;
73         pMemMng->uCurAllocated = 0;
74         pMemMng->uMaxAllocated = 0;
75     }
76 
77     return (TI_HANDLE)pMemMng;
78 }
79 
80 
mem_Destroy(TI_HANDLE hMem)81 TI_STATUS mem_Destroy (TI_HANDLE hMem)
82 {
83     TMemMng *pMemMng = (TMemMng *)hMem;
84 
85     if (pMemMng != NULL)
86     {
87         os_memoryFree (pMemMng->hOs, (void *)pMemMng, sizeof(TMemMng));
88     }
89 
90     return TI_OK;
91 }
92 
93 
94 
95 /****************************************************************************************
96  *                        os_memoryAlloc()
97  ****************************************************************************************
98 DESCRIPTION:    Allocates resident (nonpaged) system-space memory.
99 
100 ARGUMENTS:      OsContext   - our adapter context.
101                 Size        - Specifies the size, in bytes, to be allocated.
102 
103 RETURN:         Pointer to the allocated memory.
104                 NULL if there is insufficient memory available.
105 
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.
109 
110 *****************************************************************************************/
mem_Alloc(TI_HANDLE hMem,TI_UINT32 size)111 void* mem_Alloc (TI_HANDLE hMem, TI_UINT32 size)
112 {
113     TMemMng *pMemMng = (TMemMng *)hMem;
114     TMemBlock *pMemBlock;
115     TI_UINT32 total = size + sizeof(TMemBlock) + sizeof(TI_UINT32);
116 
117   #ifdef TI_MEM_ALLOC_TRACE
118     os_printf ("mem_Alloc(0x%p, %lu) : %u\n", hMem, size, uTotalSize);
119   #endif
120 
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;
125 
126     pMemMng->uCurAllocated += total;
127     if (pMemMng->uMaxAllocated < pMemMng->uCurAllocated)
128     {
129         pMemMng->uMaxAllocated = pMemMng->uCurAllocated;
130     }
131 
132     return (void*)((TI_UINT8 *)pMemBlock + sizeof(TMemBlock));
133 }
134 
135 
136 /****************************************************************************************
137  *                        os_memoryCAlloc()
138  ****************************************************************************************
139 DESCRIPTION:    Allocates an array in memory with elements initialized to 0.
140 
141 ARGUMENTS:      OsContext   -   our adapter context.
142                 Number      -   Number of elements
143                 Size        -   Length in bytes of each element
144 
145 RETURN:         None
146 
147 NOTES:
148 *****************************************************************************************/
mem_Calloc(TI_HANDLE hMem,TI_UINT32 number,TI_UINT32 size)149 void* mem_Calloc (TI_HANDLE hMem, TI_UINT32 number, TI_UINT32 size)
150 {
151     TMemMng *pMemMng = (TMemMng *)hMem;
152     void *ptr;
153     TI_UINT32 total;
154 
155     total = number * size;
156 
157  #ifdef TI_MEM_ALLOC_TRACE
158     os_printf ("os_memoryCAlloc(%u, %u) : %u\n", number, size, total);
159  #endif
160 
161     ptr = mem_Alloc (hMem, total);
162 
163     if (ptr != NULL)
164     {
165         os_memorySet (pMemMng->hOs, ptr, 0, total);
166     }
167 
168     return ptr;
169 }
170 
171 
172 
173 /****************************************************************************************
174  *                        os_memoryFree()
175  ****************************************************************************************
176 DESCRIPTION:    This function releases a block of memory previously allocated with the
177                 os_memoryAlloc function.
178 
179 
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
185                                 os_memoryAlloc.
186 
187 RETURN:         None
188 
189 NOTES:
190 *****************************************************************************************/
mem_Free(TI_HANDLE hMem,void * ptr,TI_UINT32 size)191 void mem_Free (TI_HANDLE hMem, void* ptr, TI_UINT32 size)
192 {
193     TMemMng *pMemMng = (TMemMng *)hMem;
194     TMemBlock *pMemBlock = (TMemBlock *)((TI_UINT8 *)ptr - sizeof(TMemBlock));
195 
196   #ifdef TI_MEM_ALLOC_TRACE
197     os_printf ("os_memoryFree(%p, %u)\n", ptr, size);
198   #endif
199 
200     if (pMemBlock->signature != MEM_BLOCK_START)
201     {
202       #ifdef TI_MEM_ALLOC_TRACE
203         os_printf ("os_memoryFree: memory block signature is incorrect - 0x%x\n", pMemBlock->signature);
204       #endif
205     }
206 
207     else
208     {
209         *(TI_UINT8 *)(&pMemBlock->signature) = '~';
210         if (*(TI_UINT32 *)((TI_UINT8 *)pMemBlock + pMemBlock->size + sizeof(TMemBlock)) != MEM_BLOCK_END)
211         {
212           #ifdef TI_MEM_ALLOC_TRACE
213             os_printf ("os_memoryFree: memory block corruption, size - %u\n", pMemBlock->size);
214           #endif
215         }
216 
217         os_memoryFree (pMemMng->hOs, pMemBlock, pMemBlock->signature + sizeof(TMemBlock) + sizeof(TI_UINT32));
218 
219         pMemMng->uCurAllocated -= size + sizeof(TMemBlock) + sizeof(TI_UINT32);
220 
221         if ((int)pMemMng->uCurAllocated < 0)
222         {
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);
226           #endif
227             pMemMng->uCurAllocated = 0;
228         }
229     }
230 }
231