/*****************************************************************************
Copyright (c) 2000-2003 Analog Devices.  All Rights Reserved.

Developed by Analog Devices Australia - Unit 3, 97 Lewis Road,
Wantirna, Victoria, Australia, 3152.  Email: ada.info@analog.com

THIS SOFTWARE IS PROPRIETARY & CONFIDENTIAL.  By using this module you
agree to the terms of the associated Analog Devices License Agreement.
******************************************************************************

$RCSfile: JPEG_MemAlloc.c,v $
$Revision: 1.1 $
$Date: 2008/03/17 14:26:45 $

Project:	JPEG IMAGE CODEC
Title:		JPEG Memory Management
Author(s):	D. George
Revised by: 

Description:
Module for Memory allocation and de-allocation.

References:
	
******************************************************************************
Tab Setting:			4
Target Processor:		Blackfin
Target Tools Revision:	ccblkfn		C/C++ compiler					6.3.0.0
						easmblkfn	BlackFin assembler				2.2.4.1
						elfar		ELF Librarian/Archive Utility	4.4.1.2
						linker		Linker							2.8.8.2
******************************************************************************

Modification History:
====================
$Log: JPEG_MemAlloc.c,v $
Revision 1.1  2008/03/17 14:26:45  gstephan
Updates for SDK 3.00

Revision 1.6  2004/09/06 05:47:13  sto
minor changes to mem_alloc implementation to allow the standard heap to be non-cached

Revision 1.5  2004/08/30 02:17:19  sto
Checkin of all decoder related files from Conrad Jakob

Revision 1.3  2004/05/27 22:42:51  sto
debugged for encoder operation

Revision 1.2  2004/05/20 01:06:03  sto
Checkin of Ver 0.3 delivered by Adrian Sarumpaet

Revision 1.2  2003/11/06 01:01:01  A. Sarumpaet
JPEG_MemAlloc_DELETE can now be passed NULL with no effect.

Revision 1.1  2003/11/06 01:01:01  D. George
Moved to standard template.

Revision 1.0  2003/10/15 01:01:01  D. George
Initial code

******************************************************************************/

#include <stdlib.h>				// heap_malloc()
#include "JPEG_memalloc.h"		// User defined typedefs

/****************************************************************************
 * 		Function Declarations												*
 ****************************************************************************/

/*
*******************************************************************************
** Function:		JPEG_MemAlloc_NEW
**
** Description:		This function must be used to allocate memory. When 
**	it is not required any more, the user must call JPEG_MemAlloc_DELETE to 
**	release the data buffer memory. NULL is returned if there is not enough 
**	memory available.  Please note that all memory allocated by this 
**	function must be aligned to 4 byte boundaries (note that malloc returns 
**	8 byte aligned memory).
**
** Arguments:			
**
**	N_element [IN]	The number of elements that the requested area of memory 
**					will store.
**	size [IN]		The number of bytes occupied by each element.
**	attr [IN]		One of MEM_TYPE_DATA,  MEM_TYPE_TABLE and MEM_TYPE_OBJECT 
**					that identifies the type of memory being allocated.  Note 
**					that these parameters are pre-defined.  The user cannot 
**					modify them.
**
** Outputs:
**
** Return value:	Pointer to a memory object handle that contains a pointer 
**					to the (nobj x size) bytes uninitialised memory space.  
**					NULL upon failure.
*******************************************************************************
*/
MemObjHandle *JPEG_MemAlloc_NEW (int N_element, int size, Mem_type attr)
{
	MemObjHandle	*MemObj = NULL;
	int				length = N_element * size;
	int				idx;
	int				userid;

	
// userid corresponds to the which heap is used as defined in heaptab.s / jpeg_libtest.ldf
// 0 = regular heap, slow (L3) memory
// 1 = user defined heap, standard (L2) memory (using L3 for the BF533)
// 2 = user defined heap, fast (L1) memory 

	MemObj = (MemObjHandle *)heap_malloc(1, sizeof(MemObjHandle));
	if (MemObj == NULL)
        return NULL;

	switch (attr)
	{
		case MEM_TYPE_DATA:
		{
			if (length > 2048)
			{
				userid = 1;
				break;
			}
			else
			{
				userid = 2;
				break;
			}
		}
		case MEM_TYPE_TABLE:
			userid = 2;		break;
		case MEM_TYPE_OBJECT:
			userid = 2;		break;
		default:
			return NULL;
	}

// for loop allocates slower memory if fast memory is unavailable
// this requires the userid's in heaptab.s to be ordered from slow to fast
	for (userid; userid >= 0; userid--)
	{
		idx = heap_lookup(userid);
		MemObj->MemoryAllocated = heap_malloc(idx, length);

		if (MemObj->MemoryAllocated != NULL)
			return MemObj;
	}

// memory not successfully allocated
// deallocate object
	heap_free(0, MemObj);	// idx is ignored by heap free
	return NULL;
}

/*
*******************************************************************************
** Function:		JPEG_MemAlloc_DELETE
**
** Description:		This function frees the data buffer memory object that was 
**					allocated after calling the function JPEG_MemAlloc_NEW.
**
** Arguments:			
**
**	mem_obj [IN]	Handle to memory object instance.  If NULL is passed,
**                  function does nothing.
**
** Outputs:
**
** Return value:	None.
*******************************************************************************
*/
void JPEG_MemAlloc_DELETE (MemObjHandle *mem_obj)
{
    if(mem_obj==NULL)
    {
        return;
    }
    
    // delete memory block allocated
	heap_free(0, mem_obj->MemoryAllocated);	// idx is ignored by heap free

    // delete memory object
	heap_free(0, mem_obj);	// idx is ignored by heap free
}

/*
*******************************************************************************
** Function:		JPEG_MemAlloc_ADDRESS
**
** Description:		This function must return a pointer to the data buffer memory 
**					that was allocated within the memory object mem_obj.
**
** Arguments:			
**
**	mem_obj [IN]	Handle to memory object instance.
**
** Outputs:
**
** Return value:	Pointer to an uninitialised memory space containing
**					(N_element x size) bytes that was allocated by 
**					JPEG_MemAlloc_NEW.
*******************************************************************************
*/
void *JPEG_MemAlloc_ADDRESS (MemObjHandle *mem_obj)
{
	return mem_obj->MemoryAllocated;
}
