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

📄 memspace_demo.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的内存管理源码
💻 C
字号:
/*
 *  +-------------------------------------------------------------------+
 *  | Copyright (c) 1999,2000 TriMedia Technologies Inc.                |
 *  |                                                                   |
 *  | This software  is furnished under a license  and may only be used |
 *  | and copied in accordance with the terms  and conditions of such a |
 *  | license  and with  the inclusion of this  copyright notice.  This |
 *  | software or any other copies of this software may not be provided |
 *  | or otherwise  made available  to any other person.  The ownership |
 *  | and title of this software is not transferred.                    |
 *  |                                                                   |
 *  | The information  in this software  is subject  to change  without |
 *  | any  prior notice  and should not be construed as a commitment by |
 *  | Philips Semiconductors.                                           |
 *  |                                                                   |
 *  | This  code  and  information  is  provided  "as is"  without  any |
 *  | warranty of any kind,  either expressed or implied, including but |
 *  | not limited  to the implied warranties  of merchantability and/or |
 *  | fitness for any particular purpose.                               |
 *  +-------------------------------------------------------------------+
 *
 *  Module name              : memspace_demo.c    1.4
 *
 *  Title                    : Demo use of Memspace manager
 *
 *  Last update              : 12:13:00 - 00/06/16
 *
 *  Description              : 
 *            
 *             This program demonstrates the use of the TriMedia
 *             memory space manager. This demo is split into the
 *             following three parts:
 *
 *               1) a function 'print_memspace' for printing information
 *                  on a specified memory space.
 *
 *               2) a function 'demonstrate_allocations', which demonstrates
 *                  allocation and deallocation of blocks with different properties.
 *
 *               3) a function 'demonstrate_memspaces', for demonstrating creating
 *                  using and deleting memory spaces.  
 *                  
 *               4) a function 'demonstrate_custom_memspace', for demonstrating creating
 *                  using and deleting a memory space using a user-specified malloc/free pair.
 *
 */

#include "tmlib/Memspace.h" 
#include "stdlib.h" 
#include "stdio.h" 



/* ------------------------------------------------------------------------------------- */

/*
 *               1) The following function prints information
 *                  on a specified memory space; the information is
 *                  the information retrieved via function 'memspGetInfo'  
 *                  of the memspace API. 'print_memspace' is used in function
 *                  `demonstrate_allocations` (see point 2), for printing
 *                  info of a single memspace, and in function 'demonstrate_memspaces'
 *                  (see point 3 below) for printing info of all memspaces.
 *                  Printing all memspaces is performed by passing 'print_memspace'
 *                  to as parameter to the memory space traversal function 
 *                  'memspTraverseSpaces' of the memspace API; this is the reason
 *                  for the second argument.
 */
static void print_memspace(memspSpace space, Pointer data)
{    
     Int i;
     memspSpaceInfo info;
     memspGetInfo(space, &info);

     printf("\t\t-----------> memspace: '%s'\n", info.name );
     printf("\t\t\t   total_size     : %d bytes\n", info.total_size );
     printf("\t\t\t   segment_size   : %d bytes\n", info.segment_size );
     printf("\t\t\t   increment_size : %d bytes\n", info.increment_size );

     printf("\t\t\t\n");
     printf("\t\t\t   variable size block pool:\n");

     printf("\t\t\t     - total free space      : %d bytes\n", 
                    info.variable_block_info.total_free_space );

     printf("\t\t\t     - largest free block    : %d bytes\n", 
                    info.variable_block_info.max_free_blocksize );

     printf("\t\t\t     - amount of free blocks : %d\n", 
                    info.variable_block_info.nrof_free_blocks );
 
     printf("\t\t\t\n");
     printf("\t\t\t   fixed size block pools:\n");

     for (i=0; i<memspFastSizeBound; i++) {
         if (info.small_block_info[i].amount_segments > 0) {
             printf("\t\t\t  block size= %d:\n", i);
             printf("\t\t\t     - amount of block segments : %d\n", 
                    info.small_block_info[i].amount_segments );
             printf("\t\t\t     - amount of free blocks    : %d\n", 
                    info.small_block_info[i].nrof_free_blocks );
         }
     }
}






/* ------------------------------------------------------------------------------------- */

/*
 *               2) a function 'demonstrate_allocations', which demonstrates
 *                  allocation and deallocation of blocks with different properties:
 *                  Using functions 'memspMalloc' and 'memspDebugMalloc' ,'normal' 
 *                  blocks are created, cache aligned blocks, blocks which have guard 
 *                  areas around them, and combinations of these. All of these blocks
 *                  are deleted using 'memspFree', and (not demonstrated here) all can
 *                  be resized using 'memspRealloc' which then returns blocks with
 *                  the same properties. It also is demonstrated how writes past the
 *                  boundaries of guarded blocks are flagged.
 *                  All allocation in 'demonstrate_allocations' is performed from
 *                  the system memspace, which can be abbreviated by Null.
 */
static void demonstrate_allocations()
{
    Address normal;
    Address normal1;
    Address aligned;
    Address aligned1;
    Address filepos_only;
    Address filepos_guard;
    Address filepos_guard_algn;
    Address buggy;

   /*
    * Print start info on the system memspace 
    * abbreviated by 'Null' here:
    */
    printf("\n\n#################### START BLOCK ALLOCATION:\n");
    print_memspace(Null,Null);


   /*
    * Allocate a number of blocks, all of size 34, and from
    * the system memory space (abbreviated by Null); try all
    * combintations of plain blocks, guarded blocks, and cache
    * aligned blocks:
    */
    normal             = memspMalloc     (Null,34, 0);
    normal1            = memspMalloc     (Null,34, 0);
    aligned            = memspMalloc     (Null,34, memspCACHE_ALIGNED);
    aligned1           = memspMalloc     (Null,34, memspCACHE_ALIGNED);
    filepos_only       = memspDebugMalloc(Null,34, 0,                   0, 0,__FILE__,__LINE__ );
    filepos_guard      = memspDebugMalloc(Null,34, 0,                  10,10,__FILE__,__LINE__ );
    filepos_guard_algn = memspDebugMalloc(Null,34, memspCACHE_ALIGNED, 10,10,__FILE__,__LINE__ );
    buggy              = memspDebugMalloc(Null,34, 0,                  30,20,__FILE__,__LINE__ );


   /*
    * Print the addresses of them all:
    */
    printf("\t\t\t- normal             = 0x%08x\n", normal);
    printf("\t\t\t- normal1            = 0x%08x\n", normal1);
    printf("\t\t\t- aligned            = 0x%08x\n", aligned);
    printf("\t\t\t- aligned1           = 0x%08x\n", aligned1);
    printf("\t\t\t- filepos_only       = 0x%08x\n", filepos_only);
    printf("\t\t\t- filepos_guard      = 0x%08x\n", filepos_guard);
    printf("\t\t\t- filepos_guard_algn = 0x%08x\n", filepos_guard_algn);
    printf("\t\t\t- buggy              = 0x%08x\n", buggy);


   /*
    * Write past the boundaries of block 'buggy',
    * triggering a guarded block error:
    */
    buggy[   -3 ]= 0;
    buggy[ 34+3 ]= 0;

    printf("\n\n#################### BLOCKS ALLOCATED, CORRUPTED BLOCK:\n");
    print_memspace(Null,Null);


   /*
    * Print list of guarded blocks in the 
    * system space onto the standard output:
    */
    printf("\n\n#################### GUARDED BLOCKS:\n");
    memspPrintGuarded(Null); 


   /*
    * Although guarded block checking is also implicitly
    * performed in memspPrintGuarded, it can also be 
    * triggered directly, by the overal consistency checker
    * as shown below:
    */
    printf("\n\n#################### MEMSPACE CHECKING:\n");
    memspCheck(); 


   /*
    * Note that, regardless the way in which 
    * they were created, all blocks can be
    * deallocated by the memspFree function.
    * Explicit deallocation is not needed
    * if the memory space on which the blocks
    * were created is deleted as a whole, but
    * that is demonstrated by the next function.
    */
    printf("\n\n#################### FREEING:\n");
    memspFree(normal);
    memspFree(normal1);
    memspFree(aligned);
    memspFree(aligned1);
    memspFree(filepos_only);
    memspFree(filepos_guard);
    memspFree(filepos_guard_algn);
    memspFree(buggy);


   /*
    * Print final info on the system memspace:
    */
    printf("\n\n#################### ALL BLOCKS FREED:\n");
    print_memspace(Null,Null); 
}

 




/* ------------------------------------------------------------------------------------- */

/*
 *               3) a function 'demonstrate_memspaces', for demonstrating creating
 *                  using and deleting memory spaces using functions 'memspCreate' 
 *                  and 'memspDelete'. Two memory spaces are created: 's1', for allocating
 *                  a number of large, variable size blocks, and 's2' for allocating 
 *                  a number of small (4 byte) blocks. The memory space printer is used
 *                  for showing the different internal effects on the memory spaces:
 *                  s2 escapes for its small blocks to the 4 byte block pool, which has 
 *                  (almost) no space overhead.  
 */            
static void demonstrate_memspaces()
{
    int i;
    memspSpace s1= memspCreate( "large_blocks", 100000 );
    memspSpace s2= memspCreate( "small_blocks", 100000 );

    printf("\n\n#################### S1 and S2 CREATED:\n");
    memspTraverseSpaces( print_memspace, Null );

    for (i=0; i<1000; i++) {
        memspMalloc(s1, 1000+i, 0);
        memspMalloc(s2,      4, 0);
    }

    printf("\n\n#################### S1 and S2 FILLED:\n");
    memspTraverseSpaces( print_memspace, Null );

    memspDelete(s1);

    printf("\n\n#################### S1 DELETED:\n");
    memspTraverseSpaces( print_memspace, Null );

    memspDelete(s2);

    printf("\n\n#################### S2 DELETED:\n");
    memspTraverseSpaces( print_memspace, Null );
}






/* ------------------------------------------------------------------------------------- */


/*
 *               4) a function 'demonstrate_custom_memspace', for demonstrating creating
 *                  using and deleting a memory space using a user-specified malloc/free pair
 *                  ('memspCustomCreate'). A memory space 's1' is created with
 *                  a malloc/free pair that still wraps the system malloc/free, but limits the
 *                  total amount of memory assigned to 's1'. The provided malloc/free functions also
 *                  print trace information. Note that the provided free routine is only
 *                  called at deletion of the memspace.
 *
 *                  Also note that this program allocates random sizes, which induces a 
 *                  worst case overhead of 15 * 4k= 60k of partially filled small block 
 *                  segments.
 */            

typedef struct {
    String   name;
    Int      size;
    Int      size_limit;
} MemspaceControl;

static Pointer my_malloc( MemspaceControl *control, Int size )
{
    if ( (size + control->size) > control->size_limit) {
        printf("%s: allocation of %d bytes failed (size limit reached; %d)\n", control->name, size, control->size_limit);
        return Null;
    } else {
        Pointer result= malloc(size);

        if (result != Null) {
            control->size += size;
            printf("%s: allocated %d bytes (new total %d bytes); block 0x%08x\n", control->name, size, control->size, result );
            return result;
        } else {
            printf("%s: allocation of %d bytes failed (system pool overflow)\n", control->name, size);
            return Null;
        }
    }
}

static void my_free( MemspaceControl *control, Pointer p )
{
    printf("%s: freed block 0x%08x\n", control->name, p );
    free(p);
}



static void demonstrate_custom_memspace()
{
    Int i;
    Int total= 0;

    MemspaceControl c1= { "small pool", 0, 100000  };
    memspSpace      s1= memspCustomCreate( "small pool", 16000, (memspMallocFun)my_malloc, (memspFreeFun)my_free, &c1 );

    printf("\n\n#################### S1 CREATED:\n");
    memspTraverseSpaces( print_memspace, Null );

    for (i=0; i<100; i++) {
        Int size= rand() & 0xff;

        if (memspMalloc(s1, size, 0)) {
            total += size;
            printf(" ...... %4i: %4d bytes allocation succeeded (total= %4d)\n", i, size, total);
        } else {
            printf(" @@@@@@ %4i: %4d bytes allocation failed (total= %4d)\n", i, size, total);
        }
    }

    printf("\n\n#################### S1 FILLED:\n");
    memspTraverseSpaces( print_memspace, Null );

    memspDelete(s1);

    printf("\n\n#################### S1 DELETED:\n");
    memspTraverseSpaces( print_memspace, Null );
}





/* ------------------------------------------------------------------------------------- */

main()
{
        demonstrate_allocations();
        demonstrate_memspaces();
        demonstrate_custom_memspace();

        exit(0);
}

⌨️ 快捷键说明

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