?? mem.c
字號(hào):
/** ###################################################################
**
** (c) Freescale Semiconductor
** 2004 All Rights Reserved
**
**
** (c) Copyright UNIS, spol. s r.o. 1997-2004
** UNIS, spol. s r.o.
** Jundrovska 33
** 624 00 Brno
** Czech Republic
** http : www.processorexpert.com
** mail : info@processorexpert.com
** ###################################################################*/
/* File: memory.c */
#include "port.h"
#include "mem.h"
#include "arch.h"
#include "assert.h"
#include <string.h>
#include <stdio.h>
#ifdef MEM_THREADED_OS
#include "pthread.h"
#endif
/*******************************************************
* memory Package
*******************************************************/
#define ADDRESSING_8
bool bMemInitialized = false;
mem_sPool InternalMemoryPool;
mem_sPool ExternalMemoryPool;
mem_sState InitialState;
static void Initialize (void);
/*******************************************************
*
* Method: memMallocIM
*
* Description: This function allocates dynamic memory
* of the specified size from the memory
* partitions. The function memMallocIM
* first tries to allocate the memory from
* the internal memory partition. However,
* if not enough space exists in the internal
* memory partition to satisfy the dynamic
* memory allocation, memMallocIM then tries
* the allocation from the external memory partition.
*
* Arguments:
* size - the size of the memory buffer to be
* allocated in internal memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memMallocIM returns NULL
*
*******************************************************/
void * memMallocIM (size_t size)
{
void * pMem;
pMem=memMalloc(&InternalMemoryPool, size);
if(pMem == NULL){
pMem=memMalloc(&ExternalMemoryPool, size);
}
return pMem;
}
/*******************************************************
*
* Method: memCallocIM
*
* Description: This function dynamically allocates an array
* with elements initialized to zero.
* The memCallocIM function first tries to
* reallocate the memory from the internal
* memory partition. However,
* if not enough space exists in the internal
* memory partition to satisfy the dynamic
* memory allocation, memMallocIM then tries
* the allocation from the external memory partition.
*
* Arguments:
* num - the number of elements
* size - the size of the memory buffer to be
* allocated in internal memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memCallocIM returns NULL
*
*******************************************************/
void * memCallocIM (size_t num, size_t size)
{
void * pMem;
pMem=memCalloc(&InternalMemoryPool, num, size);
if(pMem == NULL){
pMem=memCalloc(&ExternalMemoryPool, num, size);
}
return pMem;
}
/*******************************************************
*
* Method: memReallocIM
*
* Description: This function dynamically reallocates and
* resizes a memory buffer to the specified
* size from the memory partitions. The function
* memReallocIM first tries to reallocate
* the memory from the internal memory partition.
* However, if not enough space exists in
* the internal memory partition to satisfy
* the dynamic memory allocation, memReallocIM
* then tries the allocation from the external
* memory partition
*
* Arguments:
* memblock - the address of the existing memory block
* to reallocate
* size - the new size of the memory buffer to be
* allocated in internal memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memReallocIM returns NULL
*
*******************************************************/
void * memReallocIM ( void *memblock, size_t size )
{
void * pMem;
pMem=memRealloc(&InternalMemoryPool, memblock, size);
if(pMem == NULL){
pMem=memRealloc(&ExternalMemoryPool, memblock, size);
}
return pMem;
}
/*******************************************************
*
* Method: memMallocAlignedIM
*
* Description: This function allocates dynamic memory
* of the specified size from the memory
* partitions and aligns the memory properly
* to use modulo addressing. The function
* memMallocAlignedIM first tries to allocate
* the memory, properly aligned, from
* the internal memory partition. However,
* if not enough space exists in the internal
* memory partition to satisfy the dynamic
* memory allocation, or if the memory request
* cannot be properly aligned, memMallocAlignedIM
* then tries the allocation from the external
* memory partition. If memMallocAlignedIM
* cannot allocate an aligned buffer from
* either partition, it calls memMallocIM
* to try to allocate a buffer of the correct
* size without the proper alignment.
*
* Arguments:
* size - the new size of the memory buffer to be
* allocated in internal memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memMallocAlignedIM returns NULL
*
*******************************************************/
void * memMallocAlignedIM (size_t size)
{
void * pMem;
pMem=memMallocAligned(&InternalMemoryPool, size);
if(pMem == NULL){
pMem=memMallocAligned(&ExternalMemoryPool, size);
if(pMem == NULL){
pMem=memMallocIM(size);
}
}
return pMem;
}
/*******************************************************
*
* Method: memFreeIM
*
* Description: This function deallocates a memory block
* in internal memory that previously had
* been dynamically allocated with
* the routine memMallocIM.
*
* Arguments:
* memblock - pointer to previously allocated memory
*
* Return: None
*
*******************************************************/
void memFreeIM (void * memblock)
{
if(memIsIM(memblock)){
memFree(&InternalMemoryPool, memblock);
} else {
memFree(&ExternalMemoryPool, memblock);
}
}
/*******************************************************
*
* Method: memMallocEM
*
* Description: This function allocates dynamic memory
* of the specified size from the memory
* partitions. The function memMallocEM
* first tries to allocate the memory from
* the external memory partition. However,
* if not enough space exists in the external
* memory partition to satisfy the dynamic
* memory allocation, memMallocEM then tries
* the allocation from the internal memory partition.
*
* Arguments:
* size - the size of the memory buffer to be
* allocated in external memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memMallocIM returns NULL
*
*******************************************************/
void * memMallocEM (size_t size)
{
void * pMem;
pMem = memMalloc (&ExternalMemoryPool, size);
if (pMem == NULL){
pMem = memMalloc (&InternalMemoryPool, size);
}
return pMem;
}
/*******************************************************
*
* Method: memCallocEM
*
* Description: This function dynamically allocates an array
* with elements initialized to zero.
* The memCallocEM function first tries to
* reallocate the memory from the external
* memory partition. However,
* if not enough space exists in the external
* memory partition to satisfy the dynamic
* memory allocation, memCallocEM then tries
* the allocation from the internal memory partition.
*
* Arguments:
* num - the number of elements
* size - the size of the memory buffer to be
* allocated in external memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memCallocIM returns NULL
*
*******************************************************/
void * memCallocEM (size_t num, size_t size)
{
void * pMem;
pMem = memCalloc (&ExternalMemoryPool, num, size);
if (pMem == NULL) {
pMem = memCalloc (&InternalMemoryPool, num, size);
}
return pMem;
}
/*******************************************************
*
* Method: memReallocEM
*
* Description: This function dynamically reallocates and
* resizes a memory buffer to the specified
* size from the memory partitions. The function
* memReallocEM first tries to reallocate
* the memory from the external memory partition.
* However, if not enough space exists in
* the external memory partition to satisfy
* the dynamic memory allocation, memReallocEM
* then tries the allocation from the internal
* memory partition
*
* Arguments:
* memblock - the address of the existing memory block
* to reallocate
* size - the new size of the memory buffer to be
* allocated in internal memory
*
* Return: If memory is insufficient to satisfy the request
* from either partition, memReallocIM returns NULL
*
*******************************************************/
void * memReallocEM ( void *memblock, size_t size )
{
void * pMem;
pMem = memRealloc (&ExternalMemoryPool, memblock, size);
if (pMem == NULL) {
pMem = memRealloc (&InternalMemoryPool, memblock, size);
}
return pMem;
}
/*******************************************************
*
* Method: memMallocAlignedEM
*
* Description: This function allocates dynamic memory
* of the specified size from the memory
* partitions and aligns the memory properly
* to use modulo addressing. The function
* memMallocAlignedEM first tries to allocate
* the memory, properly aligned, from
* the external memory partition. However,
* if not enough space exists in the external
* memory partition to satisfy the dynamic
* memory allocation, or if the memory request
* cannot be properly aligned, memMallocAlignedEM
* then tries the allocation from the internal
* memory partition. If memMallocAlignedEM
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -