Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
Array.c
Package: tb_i86_wr_1019_1102. [view]
Upload User: super_houu
Upload Date: 2008-09-21
Package Size: 4099k
Code Size: 20k
Category:
DVD
Development Platform:
Others
- /****************************************************************************************
- * Copyright (c) 2002 ZORAN Corporation, All Rights Reserved
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ZORAN CORPORATION
- *
- * File: $Workfile: Array.c $
- *
- * Description: Implementation of a generic Array.
- * ============
- *
- *
- * Log:
- * ====
- * $Revision: 5 $
- * Last Modified by $Author: Frankm $ at $Modtime: 03-04-17 15:33 $
- ****************************************************************************************
- * Updates:
- ****************************************************************************************
- * $Log: /SourceCode/I64_Common/I64_Reference/Playcore/DataStructures/Array.c $
- *
- * 5 03-04-17 15:36 Frankm
- * The Address doesn't add during the for loop.
- *
- * 4 03-03-20 10:43 Jerryc
- * support array with size > (WORD - 1)
- *
- * 3 03-01-30 23:16 Leslie
- * Add new function ArrayInitBound
- *
- * 13 27/05/02 18:50 Nirm
- * - Cleaned-up compilation-warnings.
- *
- * 12 23/05/02 14:09 Ettim
- * Adding support for an absolute 32 bit address on the scratch pad.
- *
- * 11 23/04/02 9:28 Nirm
- * - Added dependency in "Config.h".
- *
- * 10 25/02/02 21:07 Nirm
- * - Eliminated Array_freeExtra() - Cannot be accurately supported.
- *
- * 9 21/02/02 14:11 Nirm
- * Fixed a debugging message.
- *
- * 8 10/02/02 13:59 Nirm
- * Corrected Upper-Bound arithmetic.
- *
- * 7 6/02/02 16:05 Nirm
- * Fixed the return value of Array_getSize().
- *
- * 6 4/02/02 12:53 Nirm
- * Added Array_freeExtra().
- *
- * 5 21/01/02 22:03 Nirm
- * Fixed wrong offset in External-Memory accesses.
- *
- * 4 17/01/02 20:46 Nirm
- * Removed the byte-padding of each Element, to reduce memory wasting.
- *
- * 3 9/01/02 14:48 Nirm
- * Corrected Include-Paths.
- *
- * 2 26/12/01 17:41 Nirm
- * First working version.
- *
- * 1 26/12/01 14:57 Nirm
- ****************************************************************************************/
- /////////////////////////////////////////////////////////////////////////////
- // Array.c - Implementation of a generic Array, whose elements are structures
- // of a configurable fixed size.
- // All elements reside in the External-Memory.
- //
- // Author: Nir Milstein
- #include "Config.h" // Global Configuration - do not remove!
- #ifdef _DEBUG
- #undef IFTRACE
- #define IFTRACE if (gTraceCore)
- #include "DebugDbgMain.h"
- #endif //_DEBUG
- #include "PlaycoreDataStructuresArray.h"
- #include "PlaycoreScPadScMgr.h"
- /////////////////////////////////////////////////////////////////////////////
- // Constants
- #if defined(MANUAL_DIRECTORY_EXPLORER)
- #define ARRAY_POOL_SIZE 6
- #elif defined(DVD_VR_SUPPORT)
- #define ARRAY_POOL_SIZE 8
- #else
- #define ARRAY_POOL_SIZE 5
- #endif
- #define FREE_MEMORY 0x01
- #define USE_EXTENDED_ADDRESS 0x02
- /////////////////////////////////////////////////////////////////////////////
- // Structures
- typedef struct Array_TAG
- {
- UINT32 dwBaseAddress;
- UINT16 cbElementSize;
- UINT16 uCapacity;
- UINT16 uUpperBound;
- UINT8 ucFlags;
- } Array;
- #define NULL_ARRAY 0xFFFFFFFFUL
- /////////////////////////////////////////////////////////////////////////////
- // Globals
- static Array g_aArrayPool[ARRAY_POOL_SIZE];
- static BOOL g_bArrayPoolInitialized= FALSE;
- /////////////////////////////////////////////////////////////////////////////
- // Generic Array Interface Implementation
- /////////////////////////////////////////////////////////////////////////////
- // Array_construct()
- //
- // Description: Constructs a new Array.
- //
- // Input: i_uElementsCnt - The size of the Array to construct
- // i_cbElementSize - The size, in Bytes, of each Element of the
- // Array.
- // Output: None
- // In/Out: io_pExternalMemoryPoolAddress - Points to a WORD, which
- // specifies an External-Memory address, from which to allocate
- // the Array. May be NULL.
- //
- // Return: A 32-bit DWORD, which is Handle to the newly constructed Array.
- // a NULL Handle is returned if the construction fails.
- //
- // Remarks:
- // - The Array-Handle returned must be saved by the user, and is required
- // for any operation or query performed on this Array.
- // - If a NULL pointer to an External-Memory Pool Address is supplied,
- // the memory for the Array is automatically allocated from the
- // External-Memory by the Array constructor, and freed by the destructor.
- UINT32 Array_construct(UINT16 i_uElementsCnt, UINT16 i_cbElementSize,
- WORD *io_pExternalMemoryPoolAddress)
- {
- UINT8 uArrayPoolSlot;
- UINT16 uArraySizeInContainers;
- Array *pNewArray= NULL;
- // Initialize the Array-Pool, if needed
- if (!g_bArrayPoolInitialized) {
- int i;
- memset(g_aArrayPool, 0, sizeof(g_aArrayPool));
- g_bArrayPoolInitialized= TRUE;
- //MikeX: Change the dwBaseAddress Null-Handler value
- for(i=0;i<ARRAY_POOL_SIZE;i++)
- g_aArrayPool[i].dwBaseAddress=NULL_ARRAY;
- }
- // Iterate over the Array-Pool, trying to find an available slot
- for (uArrayPoolSlot=0; uArrayPoolSlot < ARRAY_POOL_SIZE; uArrayPoolSlot++) {
- //if (0 == g_aArrayPool[uArrayPoolSlot].dwBaseAddress)
- if (NULL_ARRAY == g_aArrayPool[uArrayPoolSlot].dwBaseAddress)//MikeX: Change the dwBaseAddress Null-Handler value
- break;
- }
- // Check if an available slot was found
- if (ARRAY_POOL_SIZE == uArrayPoolSlot) {
- tr_printf(("WARNING: Array_construct() Failed [1]: The Array-Pool has been exhausted.n"));
- return (UINT32)NULL;
- }
- // Update the new Array
- pNewArray= &g_aArrayPool[uArrayPoolSlot];
- pNewArray->cbElementSize= i_cbElementSize;
- pNewArray->uCapacity= i_uElementsCnt;
- // Calculate the Array's size in External-Memory Containers
- uArraySizeInContainers= CONTAINER_COUNT(i_uElementsCnt * i_cbElementSize);
- // If needed, allocate External-Memory for the Array
- if (NULL == io_pExternalMemoryPoolAddress) {
- pNewArray->dwBaseAddress= (DWORD)sc_Malloc(uArraySizeInContainers);
- // Verify that the Array was properly allocated
- if ((DWORD)NULL_HANDLE == pNewArray->dwBaseAddress) {
- tr_printf(("WARNING: Array_construct() Failed [2]: Low system resources.n"));
- //pNewArray->dwBaseAddress= 0; //MikeX: Change the dwBaseAddress Null-Handler value
- pNewArray->dwBaseAddress= NULL_ARRAY;
- return (UINT32)NULL;
- }
- pNewArray->ucFlags |= FREE_MEMORY;
- }
- else {
- pNewArray->dwBaseAddress= (*io_pExternalMemoryPoolAddress);
- // Increment the Memory-Pool Address by the amount of Containers allocated for
- // the Array.
- *io_pExternalMemoryPoolAddress += uArraySizeInContainers;
- pNewArray->ucFlags &= (~FREE_MEMORY);
- }
- //not using extended address
- pNewArray->ucFlags &= (~USE_EXTENDED_ADDRESS);
- pNewArray->uUpperBound= 0;
- return (UINT32)pNewArray;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_constructEx()
- //
- // Description: Constructs a new Array, using an Extended address.
- //
- // Input: i_uElementsCnt - The size of the Array to construct
- // i_cbElementSize - The size, in Bytes, of each Element of the
- // Array.
- // Output: None
- // In/Out: io_pExternalMemoryPoolAddress - Points to a DWORD, which
- // specifies an External-Memory address, from which to allocate
- // the Array. May not be NULL.
- //
- // Return: A 32-bit DWORD, which is Handle to the newly constructed Array.
- // a NULL Handle is returned if the construction fails.
- //
- // Remarks:
- // - The Array-Handle returned must be saved by the user, and is required
- // for any operation or query performed on this Array.
- UINT32 Array_constructEx(UINT16 i_uElementsCnt, UINT16 i_cbElementSize,
- DWORD *io_pExternalMemoryPoolAddress)
- {
- UINT8 uArrayPoolSlot;
- UINT32 uArraySizeInContainers;
- Array *pNewArray= NULL;
- // Initialize the Array-Pool, if needed
- if (! g_bArrayPoolInitialized) {
- int i;
- memset(g_aArrayPool, 0, sizeof(g_aArrayPool));
- g_bArrayPoolInitialized= TRUE;
- //MikeX: Change the dwBaseAddress Null-Handler value
- for(i=0;i<ARRAY_POOL_SIZE;i++)
- g_aArrayPool[i].dwBaseAddress=NULL_ARRAY;
- }
- // Iterate over the Array-Pool, trying to find an available slot
- for (uArrayPoolSlot=0; uArrayPoolSlot < ARRAY_POOL_SIZE; uArrayPoolSlot++) {
- if (NULL_ARRAY == g_aArrayPool[uArrayPoolSlot].dwBaseAddress)//MikeX: Change the dwBaseAddress Null-Handler value
- break;
- }
- // Check if an available slot was found
- if (ARRAY_POOL_SIZE == uArrayPoolSlot) {
- tr_printf(("WARNING: Array_constructEx() Failed [1]: The Array-Pool has been exhausted.n"));
- return (UINT32)NULL;
- }
- // Update the new Array
- pNewArray= &g_aArrayPool[uArrayPoolSlot];
- pNewArray->cbElementSize= i_cbElementSize;
- pNewArray->uCapacity= i_uElementsCnt;
- // Calculate the Array's size in External-Memory Containers
- uArraySizeInContainers= CONTAINER_COUNT((UINT32)i_uElementsCnt * (UINT32)i_cbElementSize);
- // memory address MUST be given
- if (NULL == io_pExternalMemoryPoolAddress) {
- #ifdef _DEBUG
- tr_printf(("WARNING: Array_constructEx() Failed [2]: Invalid memory address.n"));
- //pNewArray->dwBaseAddress= 0;
- pNewArray->dwBaseAddress= NULL_ARRAY;//MikeX: Change the dwBaseAddress Null-Handler value
- return (UINT32)NULL;
- #endif
- }
- else {
- pNewArray->dwBaseAddress= (*io_pExternalMemoryPoolAddress);
- // Increment the Memory-Pool Address by the amount of Containers allocated for
- // the Array.
- *io_pExternalMemoryPoolAddress += uArraySizeInContainers;
- pNewArray->ucFlags &= (~FREE_MEMORY);
- }
- //using extended address
- pNewArray->ucFlags |= USE_EXTENDED_ADDRESS;
- pNewArray->uUpperBound= 0;
- return (UINT32)pNewArray;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_destruct()
- //
- // Description: Destructs an existing Array, that was previously created using
- // a call to Array_construct().
- //
- // Input: hArray - The Array-Handle associated with the Array to destruct.
- // Output: None
- // In/Out: None
- // Return: None
- //
- // Remarks:
- // If the External-Memory used for the Array was automatically allocated
- // by the constructor, then it is freed.
- // Otherwise, it is the user's responsibility to free any External-Memory
- // Pool that was supplied to the constructor.
- void Array_destruct(UINT32 hArray)
- {
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_destruct() Failed [1]: Invalid Array handle supplied.n"));
- return;
- }
- // Free the associated External-Memory, if it was allocated dynamically
- if (pArray->ucFlags & FREE_MEMORY) {
- sc_Free((WORD)pArray->dwBaseAddress,
- CONTAINER_COUNT(pArray->uCapacity * pArray->cbElementSize));
- }
- // Free the Array-Pool slot
- pArray->dwBaseAddress= NULL_ARRAY;//MikeX: Change the dwBaseAddress Null-Handler value
- return;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_freeExtra()
- //
- // Description: Frees any unused memory, provided that the array was
- // automatically allocated at construction time.
- //
- // Input: hArray - The Array-Handle associated with the Array to be
- // shrinked.
- // Output: None
- // In/Out: None
- // Return: None
- //
- // Remarks: None
- #pragma argsused
- void Array_freeExtra(UINT32 hArray)
- {
- /*
- UINT16 uUpperBound;
- UINT16 uUnusedMemoryInCont;
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_freeExtra() Failed [1]: Invalid Array handle supplied.n"));
- return;
- }
- // Verify that the Array was dynamically allocated
- if (0 == (pArray->uUpperBound & 0x8000)) {
- dbg_printf(("WARNING: Array_freeExtra() Failed [2]: Statically allocated Array.n"));
- return;
- }
- uUpperBound= (pArray->uUpperBound & 0x7FFF);
- // Check if there's anything to free
- if (pArray->uCapacity <= uUpperBound)
- return;
- // Compute the offset of the first unused Element
- uUpperBound= CONTAINER_COUNT(uUpperBound * pArray->cbElementSize);
- // Compute the amount of unused Memory
- uUnusedMemoryInCont= (CONTAINER_COUNT(pArray->uCapacity * pArray->cbElementSize) -
- uUpperBound);
- // Actually free the unused memory
- uUnusedMemoryInCont= sc_Free((pArray->wBaseAddress + uUpperBound), uUnusedMemoryInCont);
- // Adjust the new Capacity
- pArray->uCapacity= ((uUnusedMemoryInCont * BYTE_PER_CONTAINER) / pArray->cbElementSize);
- */
- return;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_getSize()
- //
- // Description: Returns the Size of an Array (the number of Elements it holds).
- //
- // Input: hArray - The Array-Handle associated with the Array whose size
- // is requested.
- // Output: None
- // In/Out: None
- // Return: The size of the Array.
- //
- // Remarks:
- // The size of an Array was determined during its construction, and cannot
- // be changed.
- UINT16 Array_getSize(UINT32 hArray)
- {
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_getSize() Failed [1]: Invalid Array handle supplied.n"));
- return 0;
- }
- // Return the Array's size
- //return (pArray->uUpperBound & 0x7FFF);
- return (pArray->uUpperBound);
- }
- void ArrayInitBound(UINT32 hArray,UINT16 num)
- {
- Array *pArray= (Array *)hArray;
- pArray->uUpperBound = num;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_clear()
- //
- // Description: Clears the contents of an Array to Zero.
- //
- // Input: hArray - The Array-Handle associated with the Array to clear.
- // Output: None
- // In/Out: None
- // Return: None
- //
- // Remarks:
- // This method clears an Array's contents to Zero.
- void Array_clear(UINT32 hArray)
- {
- UINT16 uTotalContainersCnt;
- Array *pArray= (Array *)hArray;
- Sc_cont scContainer;
- UINT32 dwAddress = pArray->dwBaseAddress;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_clear() Failed [1]: Invalid Array handle supplied.n"));
- return;
- }
- memset(&scContainer, 0, sizeof(scContainer));
- // Calculate the total number of External-Memory containers that the Array occupies
- uTotalContainersCnt= CONTAINER_COUNT(pArray->uCapacity * pArray->cbElementSize);
- // Clear the associated External-Memory
- for (; uTotalContainersCnt > 0; uTotalContainersCnt--)
- {
- if (pArray->ucFlags & USE_EXTENDED_ADDRESS)
- sc_Write32(dwAddress++, 1, &scContainer);
- else
- sc_Write((WORD)dwAddress++, 1, &scContainer);
- }
- return;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_getAt()
- //
- // Description: Retrieves an Array Element at a specific position.
- //
- // Input: hArray - The Array-Handle associated with the Array to query.
- // uIndex - The Index of the requested Element.
- // Output: o_pElementContainer - Points to a buffer to receive the requested
- // Element upon return.
- // In/Out: None
- // Return: TRUE if the requested Element was successfully retrieved;
- // FALSE otherwise.
- //
- // Remarks:
- // - This method may fail if the supplied Array-Handle is invalid, or if the
- // supplied Index is out of the valid range.
- // - In case of failure, the supplied container is not modified.
- // - The supplied container must be at large enough to accomodate for the
- // Element; it should be at least the size of a single Element, as reported
- // during construction of the Array.
- BOOL Array_getAt(UINT32 hArray, UINT16 uIndex, BYTE *o_pElementContainer)
- {
- DWORD dwElementAddress;
- WORD wElementOffset;
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_getAt() Failed [1]: Invalid Array handle supplied.n"));
- return FALSE;
- }
- // Check range validity
- if (uIndex >= pArray->uUpperBound) {
- tr_printf(("WARNING: Array_getAt() Failed [2]: Supplied Index out of range.n"));
- return FALSE;
- }
- // Calculate the Address of the requested Element, and the Byte-Offset from that
- // Address.
- dwElementAddress= (pArray->dwBaseAddress + (DWORD)CONTAINER_NORM((DWORD)uIndex * pArray->cbElementSize));
- wElementOffset= (WORD)CONTAINER_MOD((DWORD)uIndex * pArray->cbElementSize);
- // Retrieve the Element
- if (pArray->ucFlags & USE_EXTENDED_ADDRESS)
- sc_GetBytes32(dwElementAddress, wElementOffset, pArray->cbElementSize, o_pElementContainer);
- else
- sc_GetBytes((WORD)dwElementAddress, wElementOffset, pArray->cbElementSize, o_pElementContainer);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Array_setAt()
- //
- // Description: Assigns an Array Element at a specific position.
- //
- // Input: hArray - The Array-Handle associated with the Array to assign.
- // uIndex - The Index of the Element to be assigned.
- // i_pElement - Points to a constant buffer, whose contents is
- // the Element to assign.
- // Output: None
- // In/Out: None
- // Return: TRUE if the requested Element was successfully assigned;
- // FALSE otherwise.
- //
- // Remarks:
- // - This method may fail if the supplied Array-Handle is invalid, or if the
- // supplied Index is out of the valid range.
- // - In case of failure, no Array element is assigned.
- // - The method copies an amount of Bytes equal to the Element-size, as
- // reported during construction of the Array.
- BOOL Array_setAt(UINT32 hArray, UINT16 uIndex, const BYTE *i_pElement)
- {
- DWORD dwElementAddress;
- WORD wElementOffset;
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_setAt() Failed [1]: Invalid Array handle supplied.n"));
- return FALSE;
- }
- // Check range validity
- if (uIndex >= pArray->uCapacity) {
- tr_printf(("WARNING: Array_setAt() Failed [2]: Supplied Index out of range.n"));
- return FALSE;
- }
- // Calculate the Address of the requested Element
- dwElementAddress= (pArray->dwBaseAddress + (DWORD)CONTAINER_NORM((DWORD)uIndex * pArray->cbElementSize));
- wElementOffset= (WORD)CONTAINER_MOD((DWORD)uIndex * pArray->cbElementSize);
- // Store the Element
- if (pArray->ucFlags & USE_EXTENDED_ADDRESS)
- sc_SetBytes32(dwElementAddress, wElementOffset, pArray->cbElementSize, (BYTE*)i_pElement);
- else
- sc_SetBytes((WORD)dwElementAddress, wElementOffset, pArray->cbElementSize, (BYTE*)i_pElement);
- // Update the Upper-Bound, if necessary
- if (uIndex >= pArray->uUpperBound) {
- pArray->uUpperBound= (uIndex+1);
- }
- return TRUE;
- }
- #ifdef DVD_VR_SUPPORT
- /////////////////////////////////////////////////////////////////////////////
- // Generic Array Interface Implementation
- /////////////////////////////////////////////////////////////////////////////
- // Array_construct()
- //
- // Description: Reconstructs a Array.
- //
- // Input: hArray - The Array-Handle associated with the Array to assign.
- // i_uNewElementsCnt - The new size of array to construct.
- // Output: None
- //
- // Return: A 32-bit DWORD, which is the size of the new array.
- //
- // Remarks:
- // - Change the array size. Only for extend memory.
- UINT32 Array_reconstruct(UINT32 hArray, UINT16 i_uNewElementsCnt)
- {
- Array *pArray= (Array *)hArray;
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_reconstruct() Failed [1]: Invalid Array handle supplied.n"));
- return FALSE;
- }
- pArray->uCapacity = i_uNewElementsCnt;
- return (DWORD) CONTAINER_COUNT((UINT32)pArray->uCapacity * (UINT32)pArray->cbElementSize);
- }
- UINT16 Array_getCapacity(UINT32 hArray)
- {
- Array *pArray= (Array *)hArray;
- // Verify validity of the supplied Array Handle
- if (NULL == pArray) {
- tr_printf(("FATAL: Array_getCapacity() Failed [1]: Invalid Array handle supplied.n"));
- return 0;
- }
- return (pArray->uCapacity);
- }
- #endif//DVD_VR_SUPPORT