📄 cstream.cpp
字号:
/*****************************************************************************
******************************************************************************
** **
** Copyright (c) 2002 Videon Central, Inc. **
** All rights reserved. **
** **
** The computer program contained herein contains proprietary information **
** which is the property of Videon Central, Inc. The program may be used **
** and/or copied only with the written permission of Videon Central, Inc. **
** or in accordance with the terms and conditions stipulated in the **
** agreement/contract under which the programs have been supplied. **
** **
******************************************************************************
*****************************************************************************/
/**
* @file
* cStream.cpp
*
* Stream interface source file.
*
* @author
* Jim Condon
*
* @remark
* Code still needs to be added for multiple manager support.
*
* @date 2002.09.25 JAC - Initial file entry.
* @date 2002.10.17 JCA - Cleaned up, added comments. Added cPayloadBase code.
* Converted to vcitypes.
*/
#include "vdvd_types.h"
#include "cStream.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#if DEBUG_CSTREAM
#include <stdio.h>
FILE *fout;
char *csfilename = "payloadlog.txt";
#endif
/**
* Initialize function.
*
* @param pvPayloadBuffer - memory for payload buffers
* @param ulPayloadCnt - number of payloads
* @param ulPayloadSize - size of each payload
*
* @retval None.
*
* @verified Yes.
*
* @date 04/08/2004 ccoble Create a memory pool to be used for the pointers
* to the payloads, rather than calling OS_MemAlloc
* each time we get a payload.
*
* @data 04/28/2004 rbehe Modified to return a value.
*/
ULONG cPayloadManager::Initialize(PVOID pvPayloadBuffer, ULONG ulPayloadCnt, ULONG ulPayloadSize, char *sName)
{
ULONG i;
ULONG ulBufferSize; /* Size of memory buffer used for memory pool */
/* Initialize the private data of the payload manager. */
ulNumberOfPayloads = ulPayloadCnt;
ulSizeOfPayload = ulPayloadSize;
pvBuffer = pvPayloadBuffer;
/* Allocate a payload base object and a data buffer for each payload */
tPayloadBases = (cPayloadBase *)OS_MemAlloc(sizeof(cPayloadBase) * ulNumberOfPayloads);
if (tPayloadBases == NULL)
{
DbgPrint(( "%s tPayloadBases allocation failed \n\n\n\n", __FUNCTION__ ));
goto err_out;
}
/* Allocate memory to be used by the memory pool and create the memory pool. */
ulBufferSize = sizeof(cPayload) * ulNumberOfPayloads * CSTREAM_MAX_REF_COUNT;
pvMemPoolBuf = OS_MemAlloc(ulBufferSize);
memPool = OS_CreateMemPool(pvMemPoolBuf, ulBufferSize, sizeof(cPayload));
if (memPool == NULL)
{
DbgPrint( ( "%s memPool creation failed \n\n\n\n", __FUNCTION__ ) );
goto err_out;
}
/* Initialize the payload bases */
for (i = 0; i < ulNumberOfPayloads; i++)
{
tPayloadBases[i].ulManagerId = ulManagerId;
tPayloadBases[i].memPool = memPool;
tPayloadBases[i].ulReferenceCount = 0;
tPayloadBases[i].pvDataPtr = (PVOID)( (ULONG)pvBuffer + (i * ulSizeOfPayload) );
tPayloadBases[i].ulMaxDataSize = ulSizeOfPayload;
}
#if DEBUG_CSTREAM /* initialize the payload base records and the uniqueIDs */
for (i = 0; i < ulNumberOfPayloads; i++)
{
for (int j=0; j<MAXREFS; j++)
{
tPayloadBases[i].record[j].idnum = 0;
strncpy(tPayloadBases[i].record[j].name, "NONE", MAXIDLEN);
}
}
uniqueID = 0;
fout = fopen(csfilename, "w");
#endif
/* Copy the stream name */
if (sName != NULL)
{
strncpy(cName, sName, CSTREAM_MAX_NAME_SIZE);
cName[CSTREAM_MAX_NAME_SIZE - 1] = 0;
}
return (OS_OK);
err_out:
if (pvMemPoolBuf != NULL)
{
OS_MemFree(pvMemPoolBuf);
pvMemPoolBuf = NULL;
}
if (memPool != NULL)
{
OS_DeleteMemPool(memPool);
memPool = NULL;
}
if (tPayloadBases != NULL)
{
OS_MemFree(tPayloadBases);
tPayloadBases = NULL;
}
return ((ULONG)OS_FAILURE);
}
/**
* GetPayload function.
*
* @param None.
*
* @retval A pointer to a GetPayload object if successful, NULL if not successful
*
* @verified Yes.
*
* @date 04/08/2004 ccoble Use memory pool, rather than allocating memory.
*/
#if DEBUG_CSTREAM
cPayload *cPayloadManager::GetPayload( char* file, int line )
#else
cPayload *cPayloadManager::GetPayload(void)
#endif
{
cPayload *pPayloadPtr = NULL;
ULONG ulPayloadId = 0;
/*
* @date 4/6/2004 ccoble Make sure payload manager has been initialized.
*/
if (tPayloadBases == NULL)
{
return (NULL);
}
/* Find an empty payload base i.e. a payload base with a reference count of zero */
while (tPayloadBases[ulPayloadId].ulReferenceCount != 0)
{
ulPayloadId++;
if (ulPayloadId == ulNumberOfPayloads)
{
/* No empty payload base was found */
return (NULL);
}
}
/*
* Allocate a new payload object and assign it the empty payload base.
*/
pPayloadPtr = (cPayload *)OS_MemPoolAlloc(memPool, sizeof(cPayload));
if (pPayloadPtr != NULL)
{
pPayloadPtr->pPayloadBase = &tPayloadBases[ulPayloadId];
/* An empty payload base was found so increment its reference count */
tPayloadBases[ulPayloadId].ulReferenceCount++;
/* Initialize the read and write pointers of the new payload object */
pPayloadPtr->rd_ptr = tPayloadBases[ulPayloadId].pvDataPtr;
pPayloadPtr->wr_ptr = tPayloadBases[ulPayloadId].pvDataPtr;
#if DEBUG_CSTREAM /* give the new payload a unique ID num and give its payload base record[0] the same num and the given name */
char tmpstr[MAXIDLEN];
snprintf(tmpstr, MAXIDLEN, "new - %s:%u", file, line);
strncpy(tPayloadBases[ulPayloadId].record[0].name, tmpstr, MAXIDLEN);
pPayloadPtr->idnum = tPayloadBases[ulPayloadId].record[0].idnum = this->GetUniqueID();
if ((pPayloadPtr->idnum > DEBUG_CSTREAM_LPRINT) && (pPayloadPtr->idnum < DEBUG_CSTREAM_HPRINT) && ((pPayloadPtr->idnum % DEBUG_CSTREAM_MPRINT) == 0))
{
globalPayloadManager->GetPayloadStatus();
}
#endif
}
/* Return a pointer to the new payload */
return (pPayloadPtr);
}
/**
* ReleasePayload function.
*
* @param pPayload - payload to be released
*
* @retval None.
*
* @verified Yes.
*/
void cPayloadManager::ReleasePayload( cPayload *pPayload )
{
/* Make sure the given payload pointer is not NULL */
if (pPayload != NULL)
{
DbgAssert(pPayload->pPayloadBase->pvDataPtr >= pvBuffer);
DbgAssert((ULONG)pPayload->pPayloadBase->pvDataPtr < ((ULONG)pvBuffer + (ulNumberOfPayloads * ulSizeOfPayload)));
DbgAssert(pPayload->pPayloadBase != NULL);
DbgAssert(pPayload->pPayloadBase->ulReferenceCount != 0);
/* Decrement the reference count of the payload */
pPayload->pPayloadBase->ulReferenceCount--;
#if DEBUG_CSTREAM
/* search for matching ID num and clear it */
int j;
for (j=0; j<MAXREFS; j++)
{
if (pPayload->pPayloadBase->record[j].idnum == pPayload->idnum)
{
pPayload->pPayloadBase->record[j].idnum = 0;
strncpy(pPayload->pPayloadBase->record[j].name, "NONE", MAXIDLEN);
break;
}
}
if (j == MAXREFS)
{
printf("dbg err: couldn't find record with idnum: %u\n", pPayload->idnum);
}
#endif
/* This payload object is no longer needed so free it */
OS_MemPoolFree(memPool, pPayload);
}
}
/************************************************************************/
/************************************************************************/
/** **/
/************************************************************************/
/************************************************************************/
ULONG cPayloadManager::GetPayloadStatus( void )
{
ULONG i;
ULONG ulRet = 0;
#if DEBUG_CSTREAM
/* print out info on all payload bases and their current references */
printf("\nGetPayloadStatus (DEBUG) - Active Payloads:\n");
fprintf( fout, "\nGetPayloadStatus (DEBUG) - Active Payloads:\n");
for (i = 0; i < ulNumberOfPayloads; i++)
{
printf("payloadBase: %2i -- refcount: %2i\n", i, tPayloadBases[i].ulReferenceCount);
fprintf( fout, "payloadBase: %2i -- refcount: %2i\n", i, tPayloadBases[i].ulReferenceCount);
for (int j=0; j<MAXREFS; j++)
{
if (tPayloadBases[i].record[j].idnum != 0)
{
printf(" id# %-6u - %s\n", tPayloadBases[i].record[j].idnum, tPayloadBases[i].record[j].name);
fprintf( fout, " id# %-6u - %s\n", tPayloadBases[i].record[j].idnum, tPayloadBases[i].record[j].name);
OS_TaskDelay(1);
}
}
}
#endif
/* Count the number of free payloads */
for (i = 0; i < ulNumberOfPayloads; i++)
{
if (tPayloadBases[i].ulReferenceCount)
{
ulRet++;
}
}
/* return a percentage */
return ((ulRet * 100) / ulNumberOfPayloads);
} /* end cPayloadManager::GetPayloadStatus() */
/*
* C API
****************************************************************************/
PAYLOADMGR_HANDLE CreatePayloadMgr(PVOID pBuffer, ULONG ulNumberOfPayloads, ULONG ulSizeOfPayload)
{
cPayloadManager *pmgr = new cPayloadManager;
if (NULL != pmgr)
{
if (pmgr->Initialize(pBuffer, ulNumberOfPayloads, ulSizeOfPayload, NULL) != OS_OK)
{
delete pmgr;
pmgr = NULL;
}
}
return (pmgr);
}
void DeletePayloadMgr(PAYLOADMGR_HANDLE pmgr)
{
if (NULL != pmgr)
{
DbgPrint(("DeletePayloadMgr: Payloads active at deletion: %u%%\n",
((cPayloadManager *)pmgr)->GetPayloadStatus()));
delete ((cPayloadManager *)pmgr);
#if DEBUG_CSTREAM
fclose(fout);
#endif
}
}
STREAM_HANDLE CreateStream(PVOID pBuffer, ULONG ulBuffers, ULONG ulBuffersSize)
{
/* TODO... needs implemented */
return (NULL);
}
void DeleteStream(STREAM_HANDLE pstream)
{
/* TODO... needs implemented */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -