Blender V2.61 - r43446

memtest.c

Go to the documentation of this file.
00001 
00036 /* Number of chunks to test with */
00037 #define NUM_BLOCKS 10
00038 
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 #include "MEM_guardedalloc.h"
00043 
00044 static void mem_error_cb(const char *errorStr)
00045 {
00046     fprintf(stderr, "%s", errorStr);
00047     fflush(stderr);
00048 }
00049 
00050 int main (int argc, char *argv[])
00051 {
00052     int verbose       = 0;
00053     int error_status  = 0;
00054     int retval        = 0;
00055     int *ip;
00056 
00057     void *p[NUM_BLOCKS];
00058     int i = 0;
00059 
00060     /* ----------------------------------------------------------------- */
00061     switch (argc) {
00062     case 2:     
00063         verbose = atoi(argv[1]);
00064         if (verbose < 0) verbose = 0;
00065         break;      
00066     case 1:
00067     default:
00068         verbose = 0;
00069     }
00070     if (verbose) {
00071         fprintf(stderr,"\n*** Simple memory test\n|\n");
00072     }
00073 
00074     /* ----------------------------------------------------------------- */
00075     /* Round one, do a normal allocation, and free the blocks again.     */
00076     /* ----------------------------------------------------------------- */
00077     /* flush mem lib output to stderr */
00078     MEM_set_error_callback(mem_error_cb);
00079     
00080     for (i = 0; i < NUM_BLOCKS; i++) {
00081         int blocksize = 10000;
00082         char tagstring[1000];
00083         if (verbose >1) printf("|--* Allocating block %d\n", i);
00084         sprintf(tagstring,"Memblock no. %d : ", i);
00085         p[i]= MEM_callocN(blocksize, strdup(tagstring));
00086     }
00087 
00088     /* report on that */
00089     if (verbose > 1) MEM_printmemlist();
00090 
00091     /* memory is there: test it */
00092     error_status = MEM_check_memory_integrity();
00093 
00094     if (verbose) {
00095         if (error_status) {
00096             fprintf(stderr, "|--* Memory test FAILED\n|\n");
00097         } else {
00098             fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
00099         }
00100     } 
00101 
00102     for (i = 0; i < NUM_BLOCKS; i++) {
00103         MEM_freeN(p[i]);
00104     }
00105 
00106     /* ----------------------------------------------------------------- */
00107     /* Round two, do a normal allocation, and corrupt some blocks.       */
00108     /* ----------------------------------------------------------------- */
00109     /* switch off, because it will complain about some things.           */
00110     MEM_set_error_callback(NULL);
00111 
00112     for (i = 0; i < NUM_BLOCKS; i++) {
00113         int blocksize = 10000;
00114         char tagstring[1000];
00115         if (verbose >1) printf("|--* Allocating block %d\n", i);
00116         sprintf(tagstring,"Memblock no. %d : ", i);
00117         p[i]= MEM_callocN(blocksize, strdup(tagstring));
00118     }
00119 
00120     /* now corrupt a few blocks...*/
00121     ip = (int*) p[5] - 50 ;
00122     for (i = 0; i< 1000; i++,ip++) *ip = i+1;
00123     ip = (int*) p[6];
00124     *(ip+10005) = 0;
00125     
00126     retval = MEM_check_memory_integrity();
00127 
00128     /* the test should have failed */
00129     error_status |= !retval;        
00130     if (verbose) {
00131         if (retval) {
00132             fprintf(stderr, "|--* Memory test failed (as it should be)\n");
00133         } else {
00134             fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
00135         }
00136     } 
00137     
00138     for (i = 0; i < NUM_BLOCKS; i++) {
00139         MEM_freeN(p[i]);
00140     }
00141 
00142 
00143     if (verbose && error_status) {
00144         fprintf(stderr,"|--* Memory was corrupted\n");
00145     }
00146     /* ----------------------------------------------------------------- */ 
00147     if (verbose) {
00148         if (error_status) {
00149             fprintf(stderr,"|\n|--* Errors were detected\n");
00150         } else {
00151             fprintf(stderr,"|\n|--* Test exited succesfully\n");
00152         }
00153         
00154         fprintf(stderr,"|\n*** Finished test\n\n");
00155     }
00156     return error_status;
00157 }
00158 
00159