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

📄 zmemreq.c

📁 可用于TM1300/PNX1300系列DSP(主要用于视频处理)压缩库即应用例子。
💻 C
字号:
/*
 *  +-------------------------------------------------------------------+
 *  | Copyright (c) 1995,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 |
 *  | TriMedia Technologies.                                            |
 *  |                                                                   |
 *  | 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.                               |
 *  +-------------------------------------------------------------------+
 */

/*------------------------------ Includes ------------------------------------*/

#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include "unistd.h"
#include "errno.h"

#include <tmlib/tmtypes.h>
#include <tmlib/dprintf.h>

#include "zlib.h"

#ifndef O_BINARY
#define O_BINARY
#endif


/*---------------------------- Test parameters -------------------------------*/

typedef struct stat StatBuf;

int total_mallocd = 0;
int   max_mallocd = 0;
int   cur_mallocd = 0;

typedef void* (*mallocfun)(size_t);
typedef void  (*freefun)(void *);
typedef void* (*reallocfun)(void *, size_t );

static mallocfun old_malloc;
static freefun old_free;

/* ---------------------------- New Memory Manager ---------------------------*/

static void*  my_malloc(UInt size )
{

   long* result= (long*)old_malloc(size+4);

   if(result == NULL)
      printf("old malloc return null for %d Bytes\n", size+4);
   
   total_mallocd+= size;

   cur_mallocd+= size;

   if(cur_mallocd > max_mallocd)
      max_mallocd = cur_mallocd;
   
   *result= size;
   
   return result+1;
   
}


static void my_free( Pointer mem )
{
   int* block= (int*)(((long*)mem)-1);

   total_mallocd-= *block;
   cur_mallocd-= *block;

   old_free(block);
}

static void reset_max_malloc()
{
   max_mallocd = 0;
   cur_mallocd = 0;
}

/*------------------------ Recursive directory printing ----------------------*/

void print_file_info(String f)
{
    StatBuf buf;
    Int32 fid_norm;
    Int32 lv_read;
    Int32 lv_ret;
    Bytef *comp_mem, *uncomp_mem, *decomp_mem;
    UInt32 lv_uncomp_size, lv_decomp_size, lv_comp_size;

    if (stat(f,&buf)==0 &&  S_ISDIR(buf.st_mode)== 0)
    {
       printf(" * size= %4d dir=%d chr=%d reg=%d ino=%04d | %s\n",
             buf.st_size, S_ISDIR(buf.st_mode), 
             S_ISCHR(buf.st_mode), S_ISREG(buf.st_mode), buf.st_ino, f);

       /* save size of uncompressed file */
       lv_uncomp_size = buf.st_size;

       /* allocate memory for uncompressed and decompressed data */
       uncomp_mem = (Byte *)malloc(lv_uncomp_size);
       decomp_mem = (Byte *)malloc(lv_uncomp_size);

       /* save size of max compressed file */
       lv_comp_size = lv_uncomp_size + ((lv_uncomp_size*0.015)+1) +12 ;
       comp_mem = (Byte *)malloc(lv_comp_size);

       if(comp_mem == NULL || uncomp_mem == NULL || decomp_mem == NULL)
       {
          printf("failed to allocate memory\n");
          return;
       }

       /* open uncompressed files */
       if((fid_norm = open(f, O_BINARY|O_RDONLY,0)) == -1){
          printf("failed to open %s\n",f+2);
          return;
       }

       /* read uncompressed data */
       if((lv_read = read(fid_norm,uncomp_mem,lv_uncomp_size)) != lv_uncomp_size){
           printf("failed to read data %d %d %s\n",lv_read,lv_uncomp_size,f);
           close (fid_norm);
           return;
       }

       /* reset the max malloc to 0 */
       reset_max_malloc();

       /* compress data */
       if(lv_ret = compress(comp_mem, &lv_comp_size, uncomp_mem, lv_uncomp_size) != Z_OK){
          printf("%d failed compress data %d %d %s\n",lv_ret,lv_comp_size,lv_uncomp_size,f);
          free(uncomp_mem);
          free(decomp_mem);
          free(comp_mem);
          close (fid_norm);
          return;
       }
       
       /* uncompressed size equals decompressed size */
       lv_decomp_size = lv_uncomp_size;
       
       /* max memory allocated during compression */
       printf(" max_mallocd= %d during compression of %d bytes\n", max_mallocd,lv_decomp_size );
       
       /* reset the max malloc to 0 */
       reset_max_malloc();
          

       /* uncompress compressed memory */
       if(lv_ret = uncompress(decomp_mem, &lv_decomp_size, comp_mem, lv_comp_size) != Z_OK){
          printf("%d failed uncompress data %d %d %d %s\n",lv_ret,lv_decomp_size,lv_comp_size,f);
          free(uncomp_mem);
          free(decomp_mem);
          free(comp_mem);
          close (fid_norm);
          return;
       }


       /* max memory allocated during compression */
       printf(" max_mallocd= %d during decompression of %d bytes\n", max_mallocd, lv_uncomp_size );

       free(uncomp_mem);
       free(decomp_mem);
       free(comp_mem);
       
       close(fid_norm);
    }
}

void treewalk(String f)
{
   DIR *dirp;
   struct dirent *direntp;

   dirp = opendir(f);

   if (dirp) {
       while ( (direntp = readdir( dirp )) != NULL ) {
	   Char buffer[1000];
	   sprintf(buffer,"%s/%s",f,direntp->d_name);
           print_file_info(buffer);
           if (strcmp(direntp->d_name,".") != 0 
                 && strcmp(direntp->d_name,"..") != 0) {
	      treewalk(buffer);
           }
       }
       (void)closedir( dirp );
   }
}

void dumpflash() { treewalk("/flash"); }


/*------------------------------ Main program --------------------------------*/

Int main()
{
   printf("-------------  APPLICATION STARTED  ------------\n");

   old_malloc= my_malloc;
   old_free  = my_free;
    
   _swap_mm( &old_malloc, &old_free,&realloc );

   dumpflash();

   _swap_mm( &old_malloc, &old_free, &realloc );
      
   printf("-------------SUCCESSFULL TERMINATION ------------\n");
         
   exit (0);
}

⌨️ 快捷键说明

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