00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifdef _DEBUG
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043
00044
00045 #include <stddef.h>
00046 #include <memory.h>
00047 #include "myalloc.h"
00048 #include "testalloc.h"
00049
00050 #define CHECKBYTEVALUE 0xFF
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 static pTAlloc pMemorySegment;
00069 static long cbAllocBytes;
00070 static long cbID;
00071
00072
00073
00074
00075
00076
00077
00078 void testa_InitSegment(
00079 ){
00080
00081
00082 pMemorySegment = (pTAlloc)malloc(sizeof(TAlloc));
00083 if( pMemorySegment == NULL )exit(1);
00084 pMemorySegment->memory_allocating_function = malloc;
00085 pMemorySegment->memory_releasing_function = free;
00086 pMemorySegment->FirstUnit = NULL;
00087 cbAllocBytes = 0L;
00088 cbID = 0L;
00089 return;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 void testa_Assert0x80(
00116 ){
00117
00118
00119 long k;
00120 ptAllocUnit pAllU;
00121 static unsigned long counter=0;
00122
00123 counter++;
00124
00125 if( counter == 123 ){
00126 printf("");
00127 }
00128 if( cbAllocBytes == 0L )return;
00129 k = 0;
00130 pAllU = pMemorySegment->FirstUnit;
00131 while( pAllU ){
00132 if( pAllU->memory[pAllU->n] != CHECKBYTEVALUE ){
00133 printf("Segment altered: %d(%d)",pAllU->n,pAllU->id);
00134 printf("Segment content: %s\n",pAllU->memory);
00135 printf("Counter to catch is: %d\n",counter-1);
00136 k = 1;
00137 }
00138 pAllU = pAllU->next;
00139 }
00140 if( k ){
00141 printf("\n");
00142
00143 printf("Press any key to continue...\n");
00144 getchar();
00145 exit(1276);
00146 }
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156 void *testa_Alloc(size_t n
00157 ){
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 ptAllocUnit pAllU;
00177
00178 if( n == 0 )
00179 return NULL;
00180
00181 testa_Assert0x80();
00182
00183 cbAllocBytes += n;
00184
00185
00186
00187 pAllU = (ptAllocUnit)pMemorySegment->memory_allocating_function( n + offsetof(struct _tAllocUnit,memory) +1 );
00188
00189 if( pAllU == NULL )return NULL;
00190
00191 memset(pAllU,CHECKBYTEVALUE,n + offsetof(struct _tAllocUnit,memory) +1);
00192
00193 pAllU->n = n;
00194 pAllU->id = cbID++;
00195 pAllU->prev = NULL;
00196 pAllU->next = pMemorySegment->FirstUnit;
00197 if( pMemorySegment->FirstUnit )pMemorySegment->FirstUnit->prev = pAllU;
00198 pMemorySegment->FirstUnit = pAllU;
00199
00200
00201 if( pAllU->id == 26 ){
00202 printf("");
00203 }
00204
00205 return (void *)( pAllU->memory );
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 void testa_Free(void *pMem
00217 ){
00218
00219
00220 ptAllocUnit pAllU;
00221
00222 if( pMemorySegment == NULL )return;
00223 pAllU = (ptAllocUnit)( ((unsigned char *)pMem) - offsetof(struct _tAllocUnit,memory) );
00224
00225
00226 if( pAllU->memory[pAllU->n] != CHECKBYTEVALUE ){
00227 printf("Segment altered: %d(%d)",pAllU->n,pAllU->id);
00228 printf("Segment content: %s\n",pAllU->memory);
00229 printf("Press any key to continue...\n");
00230 getchar();
00231 exit(1276);
00232 }
00233
00234 if( pAllU->next )
00235 pAllU->next->prev = pAllU->prev;
00236 if( pAllU->prev )
00237 pAllU->prev->next = pAllU->next;
00238 else
00239 pMemorySegment->FirstUnit = pAllU->next;
00240 cbAllocBytes -= pAllU->n;
00241
00242 pMemorySegment->memory_releasing_function(pAllU);
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void testa_Check(void *pMem
00256 ){
00257
00258
00259 ptAllocUnit pAllU,pAllUi;
00260
00261 if( pMemorySegment == NULL ){
00262 printf("pMem %p can not be a valid segment as there are not segments\n",pMem);
00263 exit(1277);
00264 }
00265 pAllU = (ptAllocUnit)( ((unsigned char *)pMem) - offsetof(struct _tAllocUnit,memory) );
00266
00267 pAllUi = pMemorySegment->FirstUnit;
00268 while( pAllUi ){
00269 if( pAllUi == pAllU ){
00270 if( pAllU->memory[pAllU->n] != CHECKBYTEVALUE ){
00271 printf("Segment altered: %d(%d)",pAllU->n,pAllU->id);
00272 printf("Segment content: %s\n",pAllU->memory);
00273 exit(1278);
00274 }
00275 return;
00276 }
00277 pAllUi = pAllUi->next;
00278 }
00279
00280 printf("The segment %p is not on the list\n",pMem);
00281 exit(1279);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 void testa_AssertLeak(
00295 ){
00296
00297
00298 long k;
00299 ptAllocUnit pAllU;
00300
00301 if( cbAllocBytes == 0L )return;
00302
00303 printf("There is a memory leak of %ld bytes\n",cbAllocBytes);
00304 k = 0;
00305 pAllU = pMemorySegment->FirstUnit;
00306 while( pAllU ){
00307 pAllU = pAllU->next;
00308 k++;
00309 }
00310 printf("This memory is lost in %d chunks\n",k);
00311 pAllU = pMemorySegment->FirstUnit;
00312 while( pAllU ){
00313 printf("%d(%d)",pAllU->n,pAllU->id);
00314 pAllU = pAllU->next;
00315 if( pAllU )printf(",");
00316 }
00317 printf("\n");
00318
00319 printf("Press any key to continue...\n");
00320 getchar();
00321 exit(1276);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330 unsigned long testa_ReportLeak(
00331 ){
00332
00333
00334 long k;
00335 ptAllocUnit pAllU;
00336
00337 if( cbAllocBytes == 0L ){
00338 printf("No memory leak\n");
00339 return 0;
00340 }
00341
00342 printf("There is a memory leak of %ld bytes\n",cbAllocBytes);
00343 k = 0;
00344 pAllU = pMemorySegment->FirstUnit;
00345 while( pAllU ){
00346 pAllU = pAllU->next;
00347 k++;
00348 }
00349 printf("This memory is lost in %d chunks\n",k);
00350 pAllU = pMemorySegment->FirstUnit;
00351
00352
00353
00354
00355
00356 printf("\n");
00357 return cbAllocBytes;
00358 }
00359 #endif