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

📄 linheap.cpp

📁 WinCE 3.0 BSP, 包含Inter SA1110, Intel_815E, Advantech_PCM9574 等
💻 CPP
字号:
//--------------------------------------------------------------------------
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//  Copyright (c) 1999  Microsoft Corporation
//
//  written by:     ESG Solution Center Munich
//
//  Module Name:    linheap.cpp
//   
//  abstract:       Windows CE display driver for C&T 69000
//                  linear video memory heap manager
//
//                  this heap manager is designed to cause no
//                  allocations from the system heap (using new/delete)
//                  with the tradeoff of supporting only 
//                  a fixed number of surfaces in a given video memory size.
//   
//--------------------------------------------------------------------------

#include "precomp.h"

//-----------------------------------------------------------------------------
//
//  LinHeap::LinHeap
//
//  nSize------size of memory in bytes
//  nAlign-----alignment of memory in bytes
//
//  Create linear heap manager. The maximum number of heap chunks that can be 
//  managed is given by LINMEM_VIDMEMSURFACES. Depending on the video memory 
//  a value of 32 up to 64 seems to be a good choice for 2MB video memory.
//
//-----------------------------------------------------------------------------

LinHeap::LinHeap( INT nSize, INT nAlign)
{
    m_nEntries = 1;
    m_nAlign = nAlign;
    m_nSize  = nSize;
    memset( &m_nVidMemSurfaces, 0, sizeof(m_nVidMemSurfaces));
    m_nVidMemSurfaces[0]=nSize;
}

//-----------------------------------------------------------------------------
//
//  LinHeap::~LinHeap()
//
//-----------------------------------------------------------------------------

LinHeap::~LinHeap()
{
}

//-----------------------------------------------------------------------------
//
//  LinHeap::Alloc   
//
//  nWidth---width of surface to allocate
//  nHeight--height of surface to allocate
//  format---format of surface according to gpe definitions
//  nStride--return value: stride of allocated surface
//
//  return---offset of surface in video memory
//           error if neg. value
//
//  allocate a surface from video memory heap. surface will be aligned.
//
//-----------------------------------------------------------------------------

INT     
LinHeap::Alloc   ( INT nWidth, INT nHeight, EGPEFormat format, INT &nStride)
{
    DEBUGMSG(CT69K_ZONE_SURF, (TEXT("LinHeap::Alloc(%d,%d,%d)\r\n"), 
                nWidth, nHeight, format));

    if (m_nEntries >= (LINMEM_VIDMEMSURFACES-1))
    {
        DEBUGMSG(CT69K_ZONE_SURF, 
                (TEXT("LinHeap::Alloc--error, too many surfaces\r\n")));
        return E_OUTOFMEMORY;
    }
    
    // first calculate stride of surface
    //
    nStride = ((nWidth*EGPEFormatToBpp[format]+
               ((m_nAlign<<3)-1))/(m_nAlign<<3))
              *m_nAlign;

    INT nSize = nStride*nHeight;
    INT nBestSize = m_nSize;
    INT nBestFit  = -1;
    INT nBestOffset= -1;
    INT nOffset=0;

    DEBUGMSG(CT69K_ZONE_SURF, 
            (TEXT("--stride %d, totalsize: %d\r\n"), nStride, nSize));

    //
    //  find smallest chunk fitting this surface
    //
    for (INT i=0; i<m_nEntries; i++)
    {
        if (m_nVidMemSurfaces[i] >= nSize &&
            m_nVidMemSurfaces[i] <= nBestSize)
        {
            nBestSize = m_nVidMemSurfaces[i];
            nBestFit  = i;
            nBestOffset=nOffset;
        }
        nOffset += labs(m_nVidMemSurfaces[i]);
    }

    if (nBestFit < 0)
    {
        DEBUGMSG(CT69K_ZONE_SURF, 
                (TEXT("LinHeap::Alloc--error, chunk is too big\r\n")));
        return E_OUTOFMEMORY;
    }

    //
    //  insert new surface at position i
    //
    if (m_nVidMemSurfaces[nBestFit] != nSize)
    {
        for ( i=m_nEntries; i>nBestFit; i--)
        {
            m_nVidMemSurfaces[i]=m_nVidMemSurfaces[i-1];
        }
        m_nVidMemSurfaces[nBestFit+1] -= nSize;
        m_nEntries++;
    }
    m_nVidMemSurfaces[nBestFit] = -nSize;

    DEBUGMSG(CT69K_ZONE_SURF, 
            (TEXT("--total used %06lx, free %06lx all %06lx\r\n"), 
             TotalUsed(), TotalFree(), TotalFree()+TotalUsed()));
    DEBUGMSG(CT69K_ZONE_SURF, 
            (TEXT("LinHeap::Alloc--return offset %06lx at pos: %d, %d total\r\n"), 
             nBestOffset, nBestFit, m_nEntries));

    return nBestOffset;
}

//-----------------------------------------------------------------------------
//
//  LinHeap::Free
//
//  nOffset---offset of surface to free
//
//  free surface previously allocated by Alloc function given the offset of the
//  surface in video memory.
//
//-----------------------------------------------------------------------------

VOID    
LinHeap::Free    ( INT nOffset)
{
    DEBUGMSG(CT69K_ZONE_SURF, (TEXT("LinHeap::Free(%06lx)\r\n"), nOffset));

    INT nLoopOffset=0;

    for (INT i=0; i<m_nEntries; i++)
    {
        if (nLoopOffset >= nOffset)
            break;
        nLoopOffset += labs(m_nVidMemSurfaces[i]);
    }

    DEBUGMSG(CT69K_ZONE_SURF, 
            (TEXT("--offset %06lx, ext. %06lx\r\n"), nLoopOffset, nOffset));

    //
    // remove node i;
    //
#if DEBUG
    if (m_nVidMemSurfaces[i]>=0) 
    {
        DEBUGMSG(CT69K_ZONE_ERROR, 
                (TEXT("LinHeap::Free--error, node %d not negative(%06lx)\r\n"), 
                i, m_nVidMemSurfaces[i]));
    }
#endif
    m_nVidMemSurfaces[i] = -m_nVidMemSurfaces[i];

    //
    //  now check if previous or next nodes are also free...
    //
    if (i<(m_nEntries-1))
    {
        if (m_nVidMemSurfaces[i+1] > 0)
        {
            m_nVidMemSurfaces[i] += m_nVidMemSurfaces[i+1];

            for (INT v=i+2; v<m_nEntries; v++)
            {
                m_nVidMemSurfaces[v-1] = m_nVidMemSurfaces[v];
            }

            m_nEntries--;

        }
    }

    //
    //  previous node
    //
    if (i>0)
    {
        if (m_nVidMemSurfaces[i-1] > 0)
        {
            m_nVidMemSurfaces[i-1] += m_nVidMemSurfaces[i];

            for (INT v=i+1; v<m_nEntries; v++)
            {
                m_nVidMemSurfaces[v-1] = m_nVidMemSurfaces[v];
            }

            m_nEntries--;

        }
    }

    DEBUGMSG(CT69K_ZONE_SURF, 
            (TEXT("--total used %06lx, free %06lx all %06lx\r\n"), 
             TotalUsed(), TotalFree(), TotalUsed()+TotalFree()));
}

//-----------------------------------------------------------------------------
//
//  LinHeap::TotalFree
//
//  return total free memory on heap
//
//-----------------------------------------------------------------------------

INT 
LinHeap::TotalFree()
{
    INT iTotal=0;

    for (INT i=0; i<m_nEntries; i++)
    {
        if (m_nVidMemSurfaces[i]>0)
            iTotal += m_nVidMemSurfaces[i];
    }

    return iTotal;
}


//-----------------------------------------------------------------------------
//
//  LinHeap::TotalUsed
//
//  return total used memory on heap
//
//-----------------------------------------------------------------------------

INT
LinHeap::TotalUsed()
{
    INT iTotal=0;

    for (INT i=0; i<m_nEntries; i++)
    {
        if (m_nVidMemSurfaces[i]<0)
            iTotal -= m_nVidMemSurfaces[i];
    }

    return iTotal;
}

⌨️ 快捷键说明

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