⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rgn.c

📁 深圳市微逻辑电子有限公司 巨果&#8226 Kingmos&reg 系统核心
💻 C
📖 第 1 页 / 共 3 页
字号:
    InitializeCriticalSection( &csRgn );
	csRgn.lpcsName = "CS-RGN";

    __FreeIndex = 0;
    __AllocIndex = -1;
    __Count = 0;
    //memset( __lpMemoryBlock, 0, sizeof( __lpMemoryBlock ) );
	__lpMemoryBlock = malloc( sizeof( _MEM_BLOCK ) * BLOCK_SIZE );

	if( __lpMemoryBlock )
	{
		
		for( i = 0; i < (BLOCK_SIZE - 1); i++ )
		{
			__lpMemoryBlock[i].NextIndex = i + 1;
			__lpMemoryBlock[i].PrevIndex = i - 1;
		}
		//    rgnCount = i;
		
		__lpMemoryBlock[i].NextIndex = -1;
		__lpMemoryBlock[i].PrevIndex = i - 1;
		__FreeIndex = 0;
		__AllocIndex = -1;
		return TRUE;
	}
    return FALSE;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

void _DeInitialRgn( void )
{
    DeleteCriticalSection( &csRgn );
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

//#endif
static int __MaxCount=0;
void *__AllocMem( void )
{
//#ifdef __USE_MEM
    short int s;
	int iCount = 0;
	void * p = NULL;

	EnterCriticalSection( &csRgn );
    while( iCount < 20 )
	{
		if( __FreeIndex >= 0 )
		{
			s = __lpMemoryBlock[__FreeIndex].NextIndex;
			__lpMemoryBlock[__FreeIndex].NextIndex = __AllocIndex;
			__lpMemoryBlock[__FreeIndex].PrevIndex = -1;
			if( __AllocIndex >= 0 )
				__lpMemoryBlock[__AllocIndex].PrevIndex = __FreeIndex;
			__AllocIndex = __FreeIndex;
			__FreeIndex = s;
			__lpMemoryBlock[s].PrevIndex = -1;
			__Count++;
			if( __MaxCount < __Count )
				__MaxCount = __Count;
			p = &(__lpMemoryBlock[__AllocIndex].data);
			break;  // ok , i get it
		}
		else
		{
		    LeaveCriticalSection( &csRgn );

			Sleep(100);

			EnterCriticalSection( &csRgn );
			iCount++;
			RETAILMSG( 1, (TEXT("no enough rgn\r\n") ) );
		}
	}
	LeaveCriticalSection( &csRgn );
	return p;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

void __FreeMem( void * lpBlock )
{
//#ifdef __USE_MEM
    _MEM_BLOCK *p = (_MEM_BLOCK*)lpBlock;
    short index = p - __lpMemoryBlock;

    EnterCriticalSection( &csRgn );    

	ASSERT( index >= 0 && index < BLOCK_SIZE );
    if( __lpMemoryBlock[index].PrevIndex >= 0 )
        __lpMemoryBlock[__lpMemoryBlock[index].PrevIndex].NextIndex = __lpMemoryBlock[index].NextIndex;
    if( __lpMemoryBlock[index].NextIndex >= 0 )
        __lpMemoryBlock[__lpMemoryBlock[index].NextIndex].PrevIndex = __lpMemoryBlock[index].PrevIndex;
    if( index == __AllocIndex )
        __AllocIndex = __lpMemoryBlock[index].NextIndex;

    if( __FreeIndex >= 0 )
    {
        __lpMemoryBlock[__FreeIndex].PrevIndex = index;
        __lpMemoryBlock[index].NextIndex = __FreeIndex;
        __lpMemoryBlock[index].PrevIndex = -1;
    }
    else
    {
        __lpMemoryBlock[index].NextIndex = -1;
        __lpMemoryBlock[index].PrevIndex = -1;
    }
    __FreeIndex = index;
    __Count--;

    LeaveCriticalSection( &csRgn );
//#else
//    free( lpBlock );
//#endif
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

int __AppendNodes( _LPRGNDATA lpData, _LPRGNDATA lpSrc )
{
    _LPRECTNODE lpNode = lpSrc->lpNodeFirst;
    _LPRECTNODE lpNew, lpFirst = 0;

    while( lpNode )
    {
        if( (lpNew = __AllocRectNode()) != 0 )
        {
            lpNew->rect = lpNode->rect;
            lpNew->lpNext = lpFirst;
            lpFirst = lpNew;

            lpNode = lpNode->lpNext;
        }
        else
        {
            __FreeNodes( lpFirst );
            return ERROR;
        }
    }
    if( lpFirst )
        lpData->lpNodeFirst=__LinkNodesAfter( lpFirst, lpData->lpNodeFirst );
    return !ERROR;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

int __CalcRgnBox( _LPRECTNODE lpNodeFirst, LPRECT lpRect )
{
    _SET_LPRECT_ZERO( lpRect );
    for( ; lpNodeFirst != 0; lpNodeFirst = lpNodeFirst->lpNext )
        UnionRect( lpRect, lpRect, &(lpNodeFirst->rect) );
    return (!ERROR);
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

int __FixRects( _LPRECTNODE * lppNode, LPRECT lpDest, LPRECT lpRemove )
{
    _LPRECTNODE lpNodeFirst = 0;
    RECT rect;
    int nCount;
    _LPRECTNODE lpNew;

    *lppNode = 0;
    if( IntersectRect( &rect, lpDest, lpRemove ) )
    {   // check four rects is valid ? if yes, insert to dest
        nCount = 0;
        if( lpDest->top != rect.top )
        {
            if( (lpNew = __AllocRectNode()) != 0 )
            {
                SET_LPRECTNODE_VALUE( lpNew, lpDest->left, lpDest->top, lpDest->right, rect.top );
                lpNew->lpNext = lpNodeFirst;
                lpNodeFirst = lpNew;
                nCount++;
            }
            else
                goto _TESTMEM;
        }
        if( lpDest->bottom != rect.bottom )
        {
            if( (lpNew = __AllocRectNode()) != 0 )
            {
                SET_LPRECTNODE_VALUE( lpNew, lpDest->left, rect.bottom, lpDest->right, lpDest->bottom );
                lpNew->lpNext = lpNodeFirst;
                lpNodeFirst = lpNew;
                nCount++;
            }
            else
                goto _TESTMEM;
        }
        if( lpDest->left != rect.left )
        {
            if( (lpNew = __AllocRectNode()) != 0 )
            {
                SET_LPRECTNODE_VALUE( lpNew, lpDest->left, rect.top, rect.left, rect.bottom );
                lpNew->lpNext = lpNodeFirst;
                lpNodeFirst = lpNew;
                nCount++;
            }
            else
                goto _TESTMEM;
        }
        if( lpDest->right != rect.right )
        {
            if( (lpNew = __AllocRectNode()) != 0 )
            {
                SET_LPRECTNODE_VALUE( lpNew, rect.right, rect.top, lpDest->right, rect.bottom );
                lpNew->lpNext = lpNodeFirst;
                lpNodeFirst = lpNew;
                nCount++;
            }
            else
                goto _TESTMEM;
        }
        *lppNode = lpNodeFirst;
        if( nCount )
            return FIX_SPLIT_RECT;
        else
            return FIX_REMOVE_RECT;
    }
    return FIX_NO_INTERSECT;
_TESTMEM:
    __FreeNodes( lpNodeFirst );
    return FIX_NO_MEM;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

int __FreeNodes( _LPRECTNODE lpNodes )
{
     _LPRECTNODE lpTemp;

     while( lpNodes )
     {
         lpTemp = lpNodes->lpNext;
         __FreeMem( lpNodes );
         lpNodes = lpTemp;
     }
     return 1;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

_LPRECTNODE __LinkNodesAfter( _LPRECTNODE lpDest, _LPRECTNODE lpSrc )
{
    _LPRECTNODE p;
    if( lpDest )
    {
        p = lpDest;
        while( p->lpNext )
            p = p->lpNext;
        p->lpNext = lpSrc;
        return lpDest;
    }
    else if( lpSrc )
        return lpSrc;
    else
        return 0;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

int __SortAndUnite( _LPRGNDATA lpData )
{
    _LPRECTNODE *lppNode0PrevAdr, lpNode0;
    _LPRECTNODE lpNode1Prev, lpNode1;
    int cmpt, cmpl, flag;

// do unite
    SET_RECT_VALUE( lpData->rect, 0, 0, 0, 0 );
    lpNode0 = lpData->lpNodeFirst;
    if( lpNode0 == 0 )
    {
        lpData->count = 0;
        return 0;
    }
    else if( lpNode0->lpNext == 0 )
    {
        lpData->count = 1;
        lpData->rect = lpNode0->rect;
        return 1;
    }
    do{
        lpNode1 = lpNode0->lpNext;
        lpNode1Prev = lpNode0;
        while( lpNode1 )
        {   // compare rect
            cmpt = lpNode0->rect.top - lpNode1->rect.top;
            cmpl = lpNode0->rect.left - lpNode1->rect.left;
            flag = 0;
            if( cmpt == 0 )
            {
                if( lpNode0->rect.bottom == lpNode1->rect.bottom )
                {
                    if( lpNode0->rect.left == lpNode1->rect.right )
                    {   // union two rects                        //___________
                        lpNode0->rect.left = lpNode1->rect.left;  //|////|    |
                        flag =1;                                  //|////|    |
                    }                                             //+---------+
                    else if( lpNode0->rect.right == lpNode1->rect.left )
                    {   // union two rects                         //___________
                        lpNode0->rect.right = lpNode1->rect.right; //|    |////|
                        flag =1;                                   //|    |////|
                    }                                              //+----|----+
                }
            }
            else if( cmpl == 0 )
            {
                if( lpNode0->rect.right == lpNode1->rect.right )
                {
                    if( lpNode0->rect.top == lpNode1->rect.bottom )
                    {   // union two rects
                        lpNode0->rect.top = lpNode1->rect.top;
                        flag = 1;
                    }
                    else if( lpNode0->rect.bottom == lpNode1->rect.top )
                    {   // union two rects
                        lpNode0->rect.bottom = lpNode1->rect.bottom;
                        flag = 1;
                    }
                }
            }
            if( flag )
            {   // remove lpNode1 from table
                lpNode1Prev->lpNext = lpNode1->lpNext;
                __FreeMem( lpNode1 ); // free lpNode1
                lpNode1 = lpNode1Prev->lpNext;
                continue;
            }
            else
            {
                lpNode1Prev = lpNode1;
                lpNode1 = lpNode1->lpNext;
            }
        }
        lpNode0 = lpNode0->lpNext;
    }while( lpNode0 );
// do sort
    lpData->rect = lpData->lpNodeFirst->rect;
    lpData->count = 1;

    lpNode1Prev = lpData->lpNodeFirst;
    lpNode1 = lpNode1Prev->lpNext;
    while( lpNode1 )
    {
        lpData->count++;
        UNION_RECT( lpData->rect, lpNode1->rect );

        lpNode0 = lpData->lpNodeFirst;
        lppNode0PrevAdr = &lpData->lpNodeFirst;
        flag = 0;
        for( ; lpNode0 != lpNode1; lppNode0PrevAdr = &lpNode0->lpNext, lpNode0 = lpNode0->lpNext )
        {   //compare rect
            cmpt = lpNode0->rect.top - lpNode1->rect.top;
            cmpl = lpNode0->rect.left - lpNode1->rect.left;
            if( cmpt > 0 || ( cmpt == 0 && cmpl > 0 ) )
            {   // now, exchange rect
                lpNode1Prev->lpNext = lpNode1->lpNext; // remove lpNode1 from table
                lpNode1->lpNext = lpNode0; // insert lpNode1 to table
                *lppNode0PrevAdr = lpNode1;
                lpNode1 = lpNode1Prev->lpNext;
                flag = 1;
                break;
            }
        }
        if( flag == 0 )
        {
            lpNode1Prev = lpNode1;
            lpNode1 = lpNode1->lpNext;
        }
    }
    return 0;
}

// **************************************************
// 声明:
// 参数:
// 	无
// 返回值:
//	假入成功,返回TRUE;否则,返回FALSE
// 功能描述:
//	
// 引用: 
//	
// ************************************************

_LPRGNDATA _GetHRGNPtr( HRGN hrgn )
{
	_LPRGNDATA lprgn = (_LPRGNDATA)HANDLE_TO_PTR( hrgn );

	if( lprgn && (WORD)GET_OBJ_TYPE(lprgn) == OBJ_REGION )
		return lprgn;
	WARNMSG( 1, ("error: Invalid hrgn handle=0x%x\r\n", hrgn) );
	SetLastError( ERROR_INVALID_HANDLE );
	return NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -