1/*
2 * Copyright (c) 2010, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * *  Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 *
12 * *  Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * *  Neither the name of Texas Instruments Incorporated nor the names of
17 *    its contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34*   @file  timm_osal_memory.c
35*   This file contains methods that provides the functionality
36*   for allocating/deallocating memory.
37*
38*  @path \
39*
40*/
41/* -------------------------------------------------------------------------- */
42/* =========================================================================
43 *!
44 *! Revision History
45 *! ===================================
46 *!23-Oct-2008 Maiya ShreeHarsha: Linux specific changes
47 *!0.1: Created the first draft version, ksrini@ti.com
48 * ========================================================================= */
49
50/******************************************************************************
51* Includes
52******************************************************************************/
53
54#include <string.h>
55#include <malloc.h>
56
57#ifdef __KERNEL__
58#include <linux/types.h>
59#else
60#include <stdint.h>
61#endif
62
63
64#include "timm_osal_types.h"
65#include "timm_osal_trace.h"
66#include "timm_osal_error.h"
67#include "timm_osal_memory.h"
68
69
70
71static TIMM_OSAL_U32 gMallocCounter = 0;
72
73/******************************************************************************
74* Function Prototypes
75******************************************************************************/
76
77/* ========================================================================== */
78/**
79* @fn TIMM_OSAL_createMemoryPool function
80*
81* @see
82*/
83/* ========================================================================== */
84TIMM_OSAL_ERRORTYPE TIMM_OSAL_CreateMemoryPool(void)
85{
86	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_NONE;
87	return bReturnStatus;
88}
89
90
91
92/* ========================================================================== */
93/**
94* @fn TIMM_OSAL_DeleteMemoryPool function
95*
96* @see
97*/
98/* ========================================================================== */
99
100TIMM_OSAL_ERRORTYPE TIMM_OSAL_DeleteMemoryPool(void)
101{
102	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_NONE;
103	return bReturnStatus;
104
105}
106
107
108
109/* ========================================================================== */
110/**
111* @fn TIMM_OSAL_Malloc function
112*
113* @see
114*/
115/* ========================================================================== */
116TIMM_OSAL_PTR TIMM_OSAL_Malloc(TIMM_OSAL_U32 size,
117    TIMM_OSAL_BOOL bBlockContiguous,
118    TIMM_OSAL_U32 unBlockAlignment, TIMMOSAL_MEM_SEGMENTID tMemSegId)
119{
120
121	TIMM_OSAL_PTR pData = TIMM_OSAL_NULL;
122
123#ifdef HAVE_MEMALIGN
124	if (0 == unBlockAlignment)
125	{
126		pData = malloc((size_t) size);
127	} else
128	{
129		pData = memalign((size_t) unBlockAlignment, (size_t) size);
130	}
131#else
132	if (0 != unBlockAlignment)
133	{
134		TIMM_OSAL_Warning
135		    ("Memory Allocation:Not done for specified nBufferAlignment. Alignment of 0 will be used");
136
137	}
138	pData = malloc((size_t) size);	/*size_t is long long */
139#endif
140	if (TIMM_OSAL_NULL == pData)
141	{
142		TIMM_OSAL_Error("Malloc failed!!!");
143	} else
144	{
145		/* Memory Allocation was successfull */
146		gMallocCounter++;
147	}
148
149
150	return pData;
151}
152
153/* ========================================================================== */
154/**
155* @fn TIMM_OSAL_Free function ....
156*
157* @see
158*/
159/* ========================================================================== */
160
161void TIMM_OSAL_Free(TIMM_OSAL_PTR pData)
162{
163	if (TIMM_OSAL_NULL == pData)
164	{
165		/*TIMM_OSAL_Warning("TIMM_OSAL_Free called on NULL pointer"); */
166		goto EXIT;
167	}
168
169	free(pData);
170	pData = NULL;
171	gMallocCounter--;
172      EXIT:
173	return;
174}
175
176/* ========================================================================== */
177/**
178* @fn TIMM_OSAL_Memset function ....
179*
180* @see
181*/
182/* ========================================================================== */
183
184TIMM_OSAL_ERRORTYPE TIMM_OSAL_Memset(TIMM_OSAL_PTR pBuffer,
185    TIMM_OSAL_U8 uValue, TIMM_OSAL_U32 uSize)
186{
187	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
188
189	memset((void *)pBuffer, (int)uValue, (size_t) uSize);
190	bReturnStatus = TIMM_OSAL_ERR_NONE;
191
192	return bReturnStatus;
193}
194
195/* ========================================================================== */
196/**
197* @fn TIMM_OSAL_Memcmp function ....
198*
199* @see
200*/
201/* ========================================================================== */
202
203TIMM_OSAL_S32 TIMM_OSAL_Memcmp(TIMM_OSAL_PTR pBuffer1, TIMM_OSAL_PTR pBuffer2,
204    TIMM_OSAL_U32 uSize)
205{
206	TIMM_OSAL_S32 result = memcmp(pBuffer1, pBuffer2, uSize);
207
208	if (result > 0)
209	{
210		return 1;
211	} else if (result < 0)
212	{
213		return -1;
214	}
215
216	return 0;
217}
218
219/* ========================================================================== */
220/**
221* @fn TIMM_OSAL_Memcpy function ....
222*
223* @see
224*/
225/* ========================================================================== */
226
227TIMM_OSAL_ERRORTYPE TIMM_OSAL_Memcpy(TIMM_OSAL_PTR pBufDst,
228    TIMM_OSAL_PTR pBufSrc, TIMM_OSAL_U32 uSize)
229{
230	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
231
232	memcpy(pBufDst, pBufSrc, uSize);
233	bReturnStatus = TIMM_OSAL_ERR_NONE;
234
235	return bReturnStatus;
236}
237
238/* ========================================================================== */
239/**
240* @fn TIMM_OSAL_GetMemCounter function ....
241*
242* @see
243*/
244/* ========================================================================== */
245
246TIMM_OSAL_U32 TIMM_OSAL_GetMemCounter(void)
247{
248
249	return gMallocCounter;
250}
251