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

📄 test_osapi_memorypool.c

📁 这是DVD中伺服部分的核心代码
💻 C
字号:
/*****************************************************************************
******************************************************************************
**                                                                          **
**  Copyright (c) 2006 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 test_osapi_memory.c
 *
 * $Revision: 1.3 $ 
 *
 * Operating System API (OSAPI) test file. 
 *
 */

#include "vdvd_types.h"
#include "osapi.h"
#include "dbgprint.h"
#include "test_osapi.h"

/*
 * Function to test Memory Pool.  Create memory pool and perform allocations.
 */
void MemoryPoolTest_RandomAllocs( void )
{
    PVOID         pvBuffer, 
                  pvBlock1,
                  pvBlock2,
                  pvBlock3;
    OS_MEMPOOL_ID poolID;
    ULONG         ulBufferSz,
                  ulPageSz,
                  ulMemSize1,
                  ulMemSize2;

    /*
     * Initialization.
     */
    ulBufferSz = 501;
    ulPageSz   = 50;
    ulMemSize1 = 99;
    ulMemSize2 = 151;
    pvBuffer   = OS_MemAlloc(ulBufferSz);

    /*
     * Create memory pool.
     */
    DbgPrint( ("\n-- Create memory pool of size %d with page size of %d\n",
               ulBufferSz, ulPageSz) );
    poolID = OS_CreateMemPool(pvBuffer, ulBufferSz, ulPageSz);
    if (poolID == NULL)
    {
        DbgPrint( ("Memory pool not created!!\n") );
        return;
    }
    OS_MemoryInfo(poolID);
        
    /*
     * Allocate and free memory from memory pool.
     */
    DbgPrint( ("\n-- Allocate %d bytes of memory from pool.\n", ulMemSize1) );
    pvBlock1 = OS_MemPoolAlloc(poolID, ulMemSize1);
    if (pvBlock1 == NULL)
    {
        DbgPrint( ("Memory not allocated!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Allocate %d bytes of memory from pool.\n", ulMemSize2) );
    pvBlock2 = OS_MemPoolAlloc(poolID, ulMemSize2);
    if (pvBlock1 == NULL)
    {
        DbgPrint( ("Memory not allocated!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Allocate %d bytes of memory from pool.\n", ulMemSize2) );
    pvBlock3 = OS_MemPoolAlloc(poolID, ulMemSize2);
    if (pvBlock1 == NULL)
    {
        DbgPrint( ("Memory not allocated!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Free memory at address %x", pvBlock2) );
    if (OS_MemPoolFree(poolID, pvBlock2) != OS_OK)
    {
        DbgPrint( ("Memory not freed!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Free memory at address %x", pvBlock3) );
    if (OS_MemPoolFree(poolID, pvBlock3) != OS_OK)
    {
        DbgPrint( ("Memory not freed!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Allocate %d bytes of memory from pool.\n", ulMemSize1) );
    pvBlock2 = OS_MemPoolAlloc(poolID, ulMemSize1);
    if (pvBlock2 == NULL)
    {
        DbgPrint( ("Memory not allocated!!\n") );
    }
    OS_MemoryInfo(poolID);
    
    DbgPrint( ("\n-- Free memory at address %x", pvBlock1) );
    if (OS_MemPoolFree(poolID, pvBlock1) != OS_OK)
    {
        DbgPrint( ("Memory not freed!!\n") );
    }
    OS_MemoryInfo(poolID);
    
    DbgPrint( ("\n-- Allocate %d bytes of memory from pool.\n", ulMemSize2) );
    pvBlock1 = OS_MemPoolAlloc(poolID, ulMemSize2);
    if (pvBlock2 == NULL)
    {
        DbgPrint( ("Memory not allocated!!\n") );
    }
    OS_MemoryInfo(poolID);
    
    DbgPrint( ("\n-- Free memory at address %x", pvBlock1) );
    if (OS_MemPoolFree(poolID, pvBlock1) != OS_OK)
    {
        DbgPrint( ("Memory not freed!!\n") );
    }
    OS_MemoryInfo(poolID);

    DbgPrint( ("\n-- Free memory at address %x", pvBlock2) );
    if (OS_MemPoolFree(poolID, pvBlock2) != OS_OK)
    {
        DbgPrint( ("Memory not freed!!\n") );
    }
    OS_MemoryInfo(poolID);

    /*
     * Delete the memory pool that was created.
     */
    DbgPrint( ("\n-- Delete memory pool.\n") );
    if (OS_DeleteMemPool(poolID) != OS_OK)
    {
        DbgPrint( ("Memory pool not deleted!!\n") );
    }

    /*
     * Free memory buffer that was allocated.
     */
    (void)OS_MemFree(pvBuffer);
}

/*
 * Function to test Memory Pool.  Create several memory pools.
 */
void MemoryPoolTest_SeveralPools( void)
{
    #define         ulcNumPools 3

    PVOID           pvBuffer[ulcNumPools],
                    pvBlocks[ulcNumPools];
    OS_MEMPOOL_ID   poolID[ulcNumPools];
    ULONG           ulBufferSz,
                    ulPageSz,
                    ulBlockSz,
                    i;

    /*
     * Initialization.
     */
    ulBufferSz = 100;
    ulPageSz   = 15;
    ulBlockSz  = 65;

    /*
     * Create memory pools.
     */
    DbgPrint( ("\n-- Create 3 memory pools.\n") );
    for (i = 0; i < ulcNumPools; i++)
    {
        pvBuffer[i] = OS_MemAlloc(ulBufferSz);
        poolID[i]   = OS_CreateMemPool(pvBuffer[i], ulBufferSz, ulPageSz);
        if (poolID[i] == NULL)
        {
            DbgPrint( ("Memory pool # %d not created!!\n", i + 1) );
            return;
        }
        DbgPrint( ("\n   Information for memory pool # %d\n",i + 1) );
        OS_MemoryInfo(poolID[i]);
    }

    /*
     * Allocate memory from each memory pool.
     */
    DbgPrint( ("\n-- Allocate memory for each memory pool.\n") );
    for (i = 0; i < ulcNumPools; i++)
    {
        pvBlocks[i] = OS_MemPoolAlloc(poolID[i], ulBlockSz);
    if (pvBlocks[i] == NULL)
        {
            DbgPrint( ("Memory not allocated for memory pool # $d!!\n") );  
        }
        DbgPrint( ("\n   Information for memory pool # %d\n",i + 1) );
        OS_MemoryInfo(poolID[i]);
    }

    /*
     * Free memory from each memory pool.
     */
    DbgPrint( ("\n-- Free memory for each memory pool.\n") );
    for (i = 0; i < ulcNumPools; i++)
    {
        if (OS_MemPoolFree(poolID[i], pvBlocks[i]) != OS_OK)
        {
            DbgPrint( ("\nMemory not freed for memory pool # %d!!\n", i + 1) );
        }
        DbgPrint( ("\n   Information for memory pool # %d\n",i + 1) );
        OS_MemoryInfo(poolID[i]);
    }

    /*
     * Delete memory pools.
     */
    DbgPrint( ("\n-- Delete memory pools\n") );
    for (i = 0; i < ulcNumPools; i++)
    {
    if (OS_DeleteMemPool(poolID[i]) != OS_OK)
        {
            DbgPrint( ("Memory pool # %d not deleted!!\n", i + 1) );
            return;
        }
        (void)OS_MemFree(pvBuffer[i]);
    }
}
    
/*
 * Function to test Memory Pool.  Attempt to free invalid addresses from pool.
 */
void MemoryPoolTest_InvalidFree()
{
    OS_MEMPOOL_ID  poolID1,
                   poolID2;
    PVOID          pvBuffer1,
                   pvBuffer2,
                   pvBlock1,
                   pvBlock2;
    ULONG          ulBufferSz,
                   ulPageSz,
                   ulBlockSz;

    /*
     * Initialization.
     */
    ulBufferSz = 100;
    ulPageSz   = 15;
    ulBlockSz  = 65;
    pvBuffer1  = OS_MemAlloc(ulBufferSz);
    pvBuffer2  = OS_MemAlloc(ulBufferSz);

    /*
     * Create memory pools.
     */
    poolID1 = OS_CreateMemPool(pvBuffer1, ulBufferSz, ulPageSz);
    poolID2 = OS_CreateMemPool(pvBuffer2, ulBufferSz, ulPageSz);
    DbgPrint( ("\n-- Create 2 memory pools.\n") );
    if ( (poolID1 == NULL) || (poolID2 == NULL) )
    {
        DbgPrint( ("Memory pools not created!!\n") );
        return;
    }
    DbgPrint( ("\n    Information from memory pool # 1\n") );
    OS_MemoryInfo(poolID1);
    DbgPrint( ("\n    Information from memory pool # 2\n") );
    OS_MemoryInfo(poolID2);
    
    /*
     * Allocate memory from each memory pool.
     */
    DbgPrint( ("\n-- Allocate memory from memory pools.\n") );
    pvBlock1 = OS_MemPoolAlloc(poolID1, ulPageSz);
    pvBlock2 = OS_MemPoolAlloc(poolID2, ulPageSz);
    if ( (pvBlock1 == NULL) || (pvBlock2 == NULL) )
    {
        DbgPrint( ("\nMemory not allocated!!\n") );
    }
    DbgPrint( ("\n    Information from memory pool # 1\n") );
    OS_MemoryInfo(poolID1);
    DbgPrint( ("\n    Information from memory pool # 2\n") );
    OS_MemoryInfo(poolID2);
    
    /*
     * Attempt to free invalid memory from memory pools.
     */
    DbgPrint( ("\n-- Attempt to free invalid memory address from pool # 1\n") );
    if (OS_MemPoolFree(poolID1, pvBlock2) != OS_INVALID_MEMPOOL)
    {
        DbgPrint( ("    Unexpected return value from OS_MemPoolFree!!\n") );
    }
    DbgPrint( ("\n-- Attempt to free invalid memory address from pool # 2\n") );
    if (OS_MemPoolFree(poolID2, pvBlock1) != OS_INVALID_MEMPOOL)
    {
        DbgPrint( ("    Unexpected return value from OS_MemPoolFree!!\n") );
    }
    DbgPrint( ("\n-- Attempt to free non-aligned address from memory pool.\n") );
    if (OS_MemPoolFree(poolID1, (BYTE *)pvBlock1 + 67) != OS_FAILURE) 
    {
        DbgPrint( ("    Unexpected return value from OS_MemPoolFree!!\n") );
    }
    DbgPrint( ("\n    Information from memory pool # 1\n") );
    OS_MemoryInfo(poolID1);
    DbgPrint( ("\n    Information from memory pool # 2\n") );
    OS_MemoryInfo(poolID2);

    /*
     * Free memory from memory pools.
     */
    DbgPrint( ("\n-- Free memory from memory pools.\n") );
    if (OS_MemPoolFree(poolID1, pvBlock1) != OS_OK)
    {
        DbgPrint( ("    Memory not freed from memory pool # 1!!\n") );
    }
    if (OS_MemPoolFree(poolID2, pvBlock2) != OS_OK)
    {
        DbgPrint( ("    Memory not freed from memory pool # 2!!\n") );
    }
    DbgPrint( ("\n    Information from memory pool # 1\n") );
    OS_MemoryInfo(poolID1);
    DbgPrint( ("\n    Information from memory pool # 2\n") );
    OS_MemoryInfo(poolID2);
    
    /*
     * Attempt to free an already freed address.
     */
    DbgPrint( ("\n-- Attempt to free an already freed address.\n") );
    if (OS_MemPoolFree(poolID1, pvBlock1) != OS_FAILURE) 
    {
        DbgPrint( ("    Unexpected return value from OS_MemPoolFree!!\n") );
    }
    DbgPrint( ("\n    Information from memory pool # 1\n") );
    OS_MemoryInfo(poolID1);
   
    /*
     * Delete the memory pools.
     */
    DbgPrint( ("\n-- Delete memory pools.\n") );
    if (OS_DeleteMemPool(poolID1) != OS_OK)
    {
        DbgPrint( ("Memory pool # 1 not deleted!!\n") );
    }
    if (OS_DeleteMemPool(poolID2) != OS_OK)
    {
        DbgPrint( ("Memory pool # 2 not deleted!!\n") );
    }

    /*
     * Free memory buffer that was allocated.
     */
    (void)OS_MemFree(pvBuffer1);
    (void)OS_MemFree(pvBuffer2);
}



void OSAPIMemoryPoolTests( void )
{
    DbgPrint(("\n\nOSAPI MEMORY POOL TESTS\n"));

    DbgPrint( ("\n***** Random Allocations Test -- Going to randomly allocate\n") );
    DbgPrint( ("***** and deallocate memory from the memory pool.\n") );
    MemoryPoolTest_RandomAllocs();

    /*
     * Test managing several memory pools concurrently.
     */
    DbgPrint( ("\n***** Several Pools Test -- Going to create and manage\n") );
    DbgPrint( ("***** three different memory pools concurrently.\n") );
    MemoryPoolTest_SeveralPools();
     
    /*
     * Test freeing an invalid memory address.
     */
    DbgPrint( ("\n***** Invalid Free Test -- Going to attempt to free a\n") );
    DbgPrint( ("***** memory address that is not in the specified memory pool.\n") );
    MemoryPoolTest_InvalidFree();

    DbgPrint(("\n\nOSAPI MEMORY POOL TESTS FINISHED\n"));
}



⌨️ 快捷键说明

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