1 /*
2 * @file Memory.c
3 *
4 * @brief Linux kernel Memory interface implementation.
5 *
6 * This abstracts the Memory management interface in the kernel
7 * code. Allocation, Freeing-up, copy and address translate are
8 * supported for the kernel memory management.
9 *
10 *
11 * @ver 02.00.00.46_alpha1
12 *
13 * ============================================================================
14 *
15 * Copyright (c) 2008-2009, Texas Instruments Incorporated
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
24 * * Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * * Neither the name of Texas Instruments Incorporated nor the names of
29 * its contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
39 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * Contact information for paper mail:
44 * Texas Instruments
45 * Post Office Box 655303
46 * Dallas, Texas 75265
47 * Contact information:
48 * http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
49 * DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
50 * ============================================================================
51 *
52 */
55 /* Standard headers */
56 #include <ti/syslink/Std.h>
58 /* OSAL & Utils headers */
59 #include <ti/syslink/utils/Memory.h>
60 #include <ti/syslink/utils/MemoryOS.h>
61 #include <ti/syslink/utils/IHeap.h>
62 #include <ti/syslink/utils/Trace.h>
65 #if defined (__cplusplus)
66 extern "C" {
67 #endif
70 /*!
71 * @def Memory_MODULEID
72 * @brief Module ID for Memory OSAL module.
73 */
74 #define Memory_MODULEID (UInt16) 0xC97E
76 /* =============================================================================
77 * All success and failure codes for the module. These are used only in the
78 * implementation, but are not part of the interface.
79 * =============================================================================
80 */
81 /*!
82 * @def Memory_STATUSCODEBASE
83 * @brief Stauts code base for MEMORY module.
84 */
85 #define Memory_STATUSCODEBASE (Memory_MODULEID << 12u)
87 /*!
88 * @def Memory_MAKE_FAILURE
89 * @brief Convert to failure code.
90 */
91 #define Memory_MAKE_FAILURE(x) ((Int) (0x80000000 \
92 + (Memory_STATUSCODEBASE + (x))))
93 /*!
94 * @def Memory_MAKE_SUCCESS
95 * @brief Convert to success code.
96 */
97 #define Memory_MAKE_SUCCESS(x) (Memory_STATUSCODEBASE + (x))
99 /*!
100 * @def Memory_E_MEMORY
101 * @brief Indicates Memory alloc/free failure.
102 */
103 #define Memory_E_MEMORY Memory_MAKE_FAILURE(1)
105 /*!
106 * @def Memory_E_INVALIDARG
107 * @brief Invalid argument provided
108 */
109 #define Memory_E_INVALIDARG Memory_MAKE_FAILURE(2)
111 /*!
112 * @def Memory_S_SUCCESS
113 * @brief Operation successfully completed
114 */
115 #define Memory_S_SUCCESS Memory_MAKE_SUCCESS(0)
118 /* Allocates the specified number of bytes. */
119 Ptr
120 Memory_alloc (IHeap_Handle heap, SizeT size, SizeT align, Ptr eb)
121 {
122 Ptr buffer = NULL;
124 GT_4trace (curTrace, GT_ENTER, "Memory_alloc", heap, size, align, eb);
126 /* check whether the right paramaters are passed or not.*/
127 GT_assert (curTrace, (size > 0));
128 (Void) eb; /* Not used. */
130 if (heap == NULL) {
131 /* Call the kernel API for memory allocation */
132 buffer = MemoryOS_alloc (size, align, 0);
133 #if !defined(SYSLINK_BUILD_OPTIMIZE)
134 if (buffer == NULL) {
135 GT_setFailureReason (curTrace,
136 GT_4CLASS,
137 "Memory_alloc",
138 Memory_E_MEMORY,
139 "Failed to allocate memory!");
140 }
141 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
142 }
143 else {
144 /* if align == 0, use default alignment */
145 if (align == 0) {
146 align = Memory_getMaxDefaultTypeAlign ();
147 }
149 buffer = IHeap_alloc (heap, size, align);
150 #if !defined(SYSLINK_BUILD_OPTIMIZE)
151 if (buffer == NULL) {
152 /*! @retval NULL Heap_alloc failed */
153 GT_setFailureReason (curTrace,
154 GT_4CLASS,
155 "Memory_alloc",
156 Memory_E_MEMORY,
157 "IHeap_alloc failed!");
158 }
159 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
160 }
162 GT_1trace (curTrace, GT_LEAVE, "Memory_alloc", buffer);
164 return buffer;
165 }
168 /* Allocates the specified number of bytes and memory is set to zero. */
169 Ptr
170 Memory_calloc (IHeap_Handle heap, SizeT size, SizeT align, Ptr eb)
171 {
172 Ptr buffer = NULL;
174 GT_4trace (curTrace, GT_ENTER, "Memory_calloc", heap, size, align, eb);
176 /* Check whether the right paramaters are passed or not.*/
177 GT_assert (curTrace, (size > 0));
178 (Void) eb; /* Not used. */
180 if (heap == NULL) {
181 buffer = MemoryOS_calloc (size, align, 0);
182 #if !defined(SYSLINK_BUILD_OPTIMIZE)
183 if (buffer == NULL) {
184 /*! @retval NULL Failed to allocate memory */
185 GT_setFailureReason (curTrace,
186 GT_4CLASS,
187 "Memory_calloc",
188 Memory_E_MEMORY,
189 "Failed to allocate memory!");
190 }
191 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
192 }
193 else {
194 buffer = Memory_valloc (heap, size, align, 0u, eb);
195 #if !defined(SYSLINK_BUILD_OPTIMIZE)
196 if (buffer == NULL) {
197 /*! @retval NULL Memory_valloc failed */
198 GT_setFailureReason (curTrace,
199 GT_4CLASS,
200 "Memory_calloc",
201 Memory_E_MEMORY,
202 "Memory_valloc failed!");
203 }
204 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
205 }
207 GT_0trace (curTrace, GT_LEAVE, "Memory_calloc");
209 /*! @retval Pointer Success: Pointer to allocated buffer */
210 return buffer;
211 }
214 /* Frees up the specified chunk of memory. */
215 Void
216 Memory_free (IHeap_Handle heap, Ptr block, SizeT size)
217 {
218 GT_3trace (curTrace, GT_ENTER, "Memory_free", heap, block, size);
220 GT_assert (curTrace, (block != NULL));
221 GT_assert (curTrace, (size > 0));
223 if (heap == NULL) {
224 MemoryOS_free (block, size, 0);
225 }
226 else {
227 IHeap_free (heap, block, size);
228 }
230 GT_0trace (curTrace, GT_LEAVE, "Memory_free");
231 }
234 /* Function to obtain statistics from a heap. */
235 Void
236 Memory_getStats (IHeap_Handle heap, Memory_Stats * stats)
237 {
238 GT_2trace (curTrace, GT_ENTER, "Memory_getStats", heap, stats);
240 GT_assert (curTrace, (heap != NULL));
241 GT_assert (curTrace, (stats!= 0));
243 /* Nothing to be done if heap is NULL. */
244 if (heap != NULL) {
245 IHeap_getStats (heap, stats);
246 }
248 GT_0trace (curTrace, GT_LEAVE, "Memory_getStats");
249 }
252 /* Function to test for a particular IHeap quality */
253 Bool
254 Memory_query (IHeap_Handle heap, Int qual)
255 {
256 Bool flag = FALSE;
258 GT_2trace (curTrace, GT_ENTER, "Memory_query", heap, qual);
260 GT_assert (curTrace, (heap != NULL));
262 switch (qual) {
263 case Memory_Q_BLOCKING:
264 if (heap != NULL) {
265 flag = IHeap_isBlocking (heap);
266 }
267 else {
268 /* Assume blocking alloc for OS-specific alloc. */
269 flag = TRUE;
270 }
271 break;
273 default:
274 break;
275 }
277 GT_1trace (curTrace, GT_LEAVE, "Memory_query", flag);
279 return flag;
280 }
283 /* Function to return the largest alignment required by the target */
284 UInt32
285 Memory_getMaxDefaultTypeAlign (Void)
286 {
288 /*
289 * Alignment has been hard coded to 4
290 * This has been done to ensure that if
291 * shared region is used for structures that
292 * it aligns for bus width for integers
293 */
295 return 4;
296 }
299 /* Function to allocate the specified number of bytes and memory is set to
300 * the specified value.
301 */
302 Ptr
303 Memory_valloc (IHeap_Handle heap, SizeT size, SizeT align, Char value, Ptr eb)
304 {
305 Ptr buffer = NULL;
307 GT_4trace (curTrace, GT_ENTER, "Memory_valloc", heap, size, align, eb);
309 /* Check whether the right paramaters are passed or not.*/
310 GT_assert (curTrace, (size > 0));
311 (Void) eb; /* Not used. */
313 if (heap == NULL) {
314 buffer = MemoryOS_alloc (size, align, 0);
315 #if !defined(SYSLINK_BUILD_OPTIMIZE)
316 if (buffer == NULL) {
317 /*! @retval NULL Failed to allocate memory */
318 GT_setFailureReason (curTrace,
319 GT_4CLASS,
320 "Memory_valloc",
321 Memory_E_MEMORY,
322 "Failed to allocate memory!");
323 }
324 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
325 }
326 else {
327 buffer = IHeap_alloc (heap, size, align);
328 #if !defined(SYSLINK_BUILD_OPTIMIZE)
329 if (buffer == NULL) {
330 /*! @retval NULL Heap_alloc failed */
331 GT_setFailureReason (curTrace,
332 GT_4CLASS,
333 "Memory_valloc",
334 Memory_E_MEMORY,
335 "IHeap_alloc failed!");
336 }
337 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
338 }
340 #if !defined(SYSLINK_BUILD_OPTIMIZE)
341 if (buffer != NULL) {
342 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
343 buffer = Memory_set (buffer, value, size);
344 #if !defined(SYSLINK_BUILD_OPTIMIZE)
345 if (buffer == NULL) {
346 /*! @retval NULL Memory_set to 0 failed */
347 GT_setFailureReason (curTrace,
348 GT_4CLASS,
349 "Memory_valloc",
350 Memory_E_MEMORY,
351 "Memory_set to 0 failed!");
352 }
353 }
354 #endif /* #if !defined(SYSLINK_BUILD_OPTIMIZE) */
356 GT_0trace (curTrace, GT_LEAVE, "Memory_valloc");
358 /*! @retval Pointer Success: Pointer to allocated buffer */
359 return buffer;
360 }
363 /* Function to translate an address. */
364 Ptr
365 Memory_translate (Ptr srcAddr, Memory_XltFlags flags)
366 {
367 Ptr buf = NULL;
369 GT_2trace (curTrace, GT_ENTER, "Memory_tranlate", srcAddr, flags);
371 buf = MemoryOS_translate (srcAddr, flags);
373 GT_1trace (curTrace, GT_LEAVE, "Memory_tranlate", buf);
375 return buf;
376 }
379 #if defined (__cplusplus)
380 }
381 #endif /* defined (_cplusplus)*/