📄 rpool.c
字号:
if(!IS_LAST_NODE(location) && ( i == ((int)numOfBlocks-1))){
elem=rpoolGetNode(raH,location);
if ((res = memcmp((void *)&(elem->data),(char*)src+i*elemSize ,
(size%elemSize ? size%elemSize : elemSize))
))
return(res);
}
if (IS_LAST_NODE(location)&&((size-i*elemSize)>0)) return (-1);
return (res);
}
/* Compares two allocated elements from pool */
/* Returns int that is less , equal greater then 0 */
/* if ptr1 is less, equal or greater than ptr2*/
int
rpoolCompareInternal(
IN HRPOOL pool,
IN HRPOOLELEM ptr1,
IN HRPOOLELEM ptr2,
IN int size
)
{
HRA raH=(HRA)pool;
RvSize_t elemSize, numOfBlocks;
int i, location1, location2, res=RV_ERROR_UNKNOWN;
rpoolElem *elem1,*elem2;
if ( (! pool ) || (! size) ) return RV_ERROR_UNKNOWN;
location1=RPOOL_LOCATION(ptr1);
location2=RPOOL_LOCATION(ptr2);
if (INVALID_LOCATION(raH,location1)) return COMPARE_ERROR;
if (INVALID_LOCATION(raH,location2)) return COMPARE_ERROR;
elemSize = RPOOL_ELEM_SIZE(raElemSize(raH));
numOfBlocks = RPOOL_BLOCKS(size,elemSize);
for (i=0; (i<(int)numOfBlocks)&&!IS_LAST_NODE(location1)&&!IS_LAST_NODE(location2) ; i++){
elem1=rpoolGetNode(raH,location1);
elem2=rpoolGetNode(raH,location2);
if (( res = memcmp( (void *)&(elem1->data), (void *)&(elem2->data), RvMin(elemSize,(RvSize_t)size))))
return (res);
location1=elem1->nextBlock;
location2=elem2->nextBlock;
size-=elemSize;
}
if ((!IS_LAST_NODE(location1) &&!IS_LAST_NODE(location2)) ||
( IS_LAST_NODE(location1) && IS_LAST_NODE(location2)) ||
( i==(int)numOfBlocks) )
return (res);
if( IS_LAST_NODE(location1))
return (-1);
else
return (1);
}
/* Moves from ptr1 to ptr2 */
/* Returns pointer to new ptr2 location */
HRPOOLELEM
rpoolMove(
IN HRPOOL pool,
IN HRPOOLELEM ptr1,
OUT HRPOOLELEM* ptr2
)
{
int location;
if ( ! pool ) return NULL;
location = RPOOL_LOCATION(ptr1);
if (INVALID_LOCATION(pool,location)) return NULL;
(*ptr2) = RETURNED_VALUE(location);
return (*ptr2);
}
#if 0
/* Display Functions */
void
rpoolPrint(
IN HRPOOL pool
)
{
HRA raH = (HRA)pool;
int location;
int maxNumOfElem;
int length;
rpoolElem *elem;
int elemSize;
char tmp[128];
strcpy(tmp, "");
if( ! pool ) return ;
maxNumOfElem=raMaxSize(raH);
elemSize = (int)(raElemSize(raH) - sizeof(rpoolElem) + sizeof(char));
for(location=0;location<maxNumOfElem;location++) {
elem = rpoolGetNode(raH,location);
length = IS_LAST_NODE(elem->nextBlock) ? (GET_LAST_NODE_SIZE(elem->nextBlock)) : elemSize ;
if ( elem->data ){
memcpy(tmp, (char *)&(elem->data), elemSize);
tmp[length]='\0';
}
printf("\n%d: alloc[%d] next[%d] data[%s]",
location, raElemIsVacant(raH, location),
elem->nextBlock, nprn(tmp));
strcpy(tmp, "");
}
}
#endif
RvStatus rpoolStatistics(
/* Get pool statistics (space is in bytes) */
IN HRPOOL pool,
OUT RvInt32* poolSize, /* max size of pool */
OUT RvInt32* maxUsedSpace, /* maximum space allocated in the past */
OUT RvInt32* allocatedSpace /* currently allocated space */
)
{
HRA raH = (HRA)pool;
int elemSize;
if (pool == NULL)
return RV_ERROR_NULLPTR;
elemSize = RPOOL_ELEM_SIZE(raElemSize(raH));
if (poolSize != NULL)
*poolSize = raMaxSize(raH)*elemSize;
if (maxUsedSpace != NULL)
*maxUsedSpace = raMaxUsage(raH)*elemSize;
if (allocatedSpace != NULL)
*allocatedSpace = raCurSize(raH)*elemSize;
return RV_OK;
}
int rpoolChunkSize(
IN HRPOOL pool,
IN HRPOOLELEM ptr
)
{
HRA raH = (HRA)pool;
rpoolElem *elem;
int elemSize;
int location;
int size = 0;
if( ! pool ) return RV_ERROR_UNKNOWN;
location = RPOOL_LOCATION( ptr);
if (INVALID_LOCATION(raH,location)) return RV_ERROR_UNKNOWN;
elemSize = RPOOL_ELEM_SIZE(raElemSize(raH));
while(!IS_LAST_NODE(location) ){
elem=rpoolGetNode(raH,location);
size += (IS_LAST_NODE(elem->nextBlock) ? (GET_LAST_NODE_SIZE(elem->nextBlock)) : elemSize);
location = elem->nextBlock;
}
return (size);
}
/* Inter RPOOLs operations */
/* Makes copy from poolSrc-ptrSsrc to poolDest-ptrDest only if element sizes are equal */
/* Otherwise return NULL */
/* Returns pointer to dest */
HRPOOLELEM
rpoolCopyPoolToPool(
IN HRPOOL poolSrc,
IN HRPOOL poolDest,
IN HRPOOLELEM ptrSrc,
IN HRPOOLELEM ptrDest,
IN int size
)
{
int src, dest, srcSize, destSize;
RvSize_t elemSize;
HRA raSrc = (HRA)poolSrc;
HRA raDest = (HRA)poolDest;
int totalSize = 0;
rpoolElem *elemSrc, *elemDest;
if (( ! poolSrc ) || ( ! poolDest) || ( ! size))
return NULL;
srcSize = raElemSize(raSrc);
destSize = raElemSize(raDest);
if( srcSize != destSize ) return NULL;
elemSize = RPOOL_ELEM_SIZE(srcSize);
src = RPOOL_LOCATION(ptrSrc);
dest = RPOOL_LOCATION(ptrSrc);
if (INVALID_LOCATION(raSrc,src)) return NULL;
if (INVALID_LOCATION(raDest,dest)) return NULL;
while (!IS_LAST_NODE(src) && !IS_LAST_NODE(dest) && (totalSize < size )) {
elemSrc = rpoolGetNode(raSrc, src);
elemDest = rpoolGetNode(raDest, dest);
memcpy((void *)&(elemDest->data), (void *)&(elemSrc->data), elemSize);
dest = elemDest->nextBlock;
src = elemSrc->nextBlock;
totalSize += elemSize;
}
return (ptrDest);
}
/* Compares two locations at different pools , only if element size at both are equal*/
/* Returns the value less, equal , more than 0 if Src smaller , equal , greater than */
/* Dest respectevly */
int
rpoolComparePoolToPool(
IN HRPOOL poolSrc,
IN HRPOOL poolDest,
IN HRPOOLELEM ptrSrc,
IN HRPOOLELEM ptrDest,
IN int size
)
{
int src, dest, srcSize, destSize, res=RV_ERROR_UNKNOWN;
RvSize_t elemSize;
HRA raSrc = (HRA)poolSrc;
HRA raDest = (HRA)poolDest;
int totalSize = 0;
rpoolElem *elemSrc, *elemDest;
if(ptrDest);
if (( ! poolSrc ) || ( ! poolDest) || ( ! size))
return RV_ERROR_UNKNOWN;
srcSize = raElemSize(raSrc);
destSize = raElemSize(raDest);
if( srcSize != destSize ) return RV_ERROR_UNKNOWN;
elemSize = RPOOL_ELEM_SIZE(srcSize);
src = RPOOL_LOCATION(ptrSrc);
dest = RPOOL_LOCATION(ptrSrc);
if (INVALID_LOCATION(raSrc,src)) return COMPARE_ERROR;
if (INVALID_LOCATION(raDest,dest)) return COMPARE_ERROR;
while ( !IS_LAST_NODE(src) && !IS_LAST_NODE(dest) && (totalSize < size )) {
elemSrc = rpoolGetNode(raSrc, src);
elemDest = rpoolGetNode(raSrc, dest);
if (( res = memcmp((void *)&(elemSrc->data), (void *)&(elemDest->data), elemSize) ))
return (res);
dest = elemDest->nextBlock;
src = elemSrc->nextBlock;
totalSize += elemSize;
}
if ((!IS_LAST_NODE(src) &&!IS_LAST_NODE(dest)) ||
( IS_LAST_NODE(src) && IS_LAST_NODE(dest)) ||
( totalSize==size) )
return (res);
if( IS_LAST_NODE(src) )
return (-1);
else
return (1);
}
/***************************************************************************************
* rpoolGetPtr
*
* purpose: To get an hrpool message and offset and return a real pointer to that location
* and the length until the end of the contigous part.
* If the offset reaches or exceeds the end of the element the length is returned as -1
*
* Input: pool - the pool in question
* element - the element on which we are working
* offset - the offset in bytes into the element
*
* Output: pointer - A real pointer to the place calculated by the offset.
*
* returned value: Length - The size of the contigous area from the pointer to the end
* of the current segment of the element.
* 0 - The offset is out of the RPOOL element's range
* Negative value - Failure.
****************************************************************************************/
int
rpoolGetPtr(IN HRPOOL pool,
IN HRPOOLELEM element,
IN int offset,
OUT void** pointer)
{
HRA raH = (HRA)pool;
int elemSize;
int length = 0;
int location, i, shiftBlocks;
rpoolElem *elem;
RvBool found = RV_FALSE;
#ifdef RV_NULLCHECK
/* check that the inputs were legal */
if ((pool == NULL) || (element == NULL))
return RV_ERROR_NULLPTR;
#endif
location = RPOOL_LOCATION(element);
if (INVALID_LOCATION(raH,location))
return -1;
/* calculate the size of each part of the element */
elemSize = RPOOL_ELEM_SIZE(raElemSize(raH));
/* calculate the part in which the offset falls */
shiftBlocks = RPOOL_BLOCKS(offset+1, elemSize);
/* go over the parts until we reach the part in which the offset is */
for (i = 1; (!found) && (i <= shiftBlocks); i++, location = elem->nextBlock)
{
if (IS_LAST_NODE(location))
break; /* We've reached the end */
/* get the current part entry */
elem = rpoolGetNode(raH, location);
/* if we reached the required part, calculate the pointer and length */
if (i == shiftBlocks)
{
/* Assume the message occupies all the block and maybe more */
if (IS_LAST_NODE(elem->nextBlock))
length = GET_LAST_NODE_SIZE(elem->nextBlock);
else
length = elemSize;
/* the contigous area is from the offset in this block to the last byte */
length -= (offset % elemSize);
/* The pointer to the contigous area is the beginning of the block plus
the offset within the block */
if (pointer)
*pointer = (void *)( (char *)&(elem->data) + (offset % elemSize) );
found = RV_TRUE;
}
}
return length;
}
#ifdef __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -