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