home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / memdebug / test.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-08  |  13.5 KB  |  504 lines

  1. /********************************************************************************/
  2. /* REMARK: set tab width to 4 spaces for best format                            */
  3. /********************************************************************************/
  4. /********************************************************************************/
  5. /*                                                                                 */
  6. /* Copyright (C) 1993    All Rights Reserved                                        */
  7. /* Centre de Recherche Public Henri Tudor (CRP-HT)                                */
  8. /* 6, rue Coudenhove-Kalergi                                                    */
  9. /* L1359 Luxembourg-Kirchberg                                                    */
  10. /*                                                                                 */
  11. /* Author            : Schmit Rene                                                */
  12. /* Internet            : Rene.Schmit@crpht.lu                                        */
  13. /* Creation Date    : Tuesday, June 29, 1993                                    */
  14. /* File name        : Test.C for memdebug.c                                        */
  15. /* Project            : Tools                                                        */
  16. /*                                                                                 */
  17. /* No responsibility is assumed for any damages that may result                 */
  18. /* from any defect in this software.                                            */
  19. /*                                                                                 */
  20. /********************************************************************************/
  21. /********************************************************************************/
  22.  
  23. #include <limits.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #ifndef MEMDEBUG
  29. #define MEMDEBUG
  30. #endif
  31.  
  32. #include "memdebug.h"
  33.  
  34. /********************************************************************************/
  35. /******************************* Constants **************************************/
  36. /********************************************************************************/
  37.  
  38. #define D_TableSize 13
  39.  
  40. /********************************************************************************/
  41. /******************************* Data types *************************************/
  42. /********************************************************************************/
  43.  
  44. enum t_CallKind {c_Malloc, c_Calloc};
  45.  
  46. typedef enum t_CallKind t_CallKind ;
  47.  
  48. /********************************************************************************/
  49. /**************************** File global data **********************************/
  50. /********************************************************************************/
  51.  
  52. /********************************************************************************/
  53. /*********************** Local function prototypes ******************************/
  54. /********************************************************************************/
  55.  
  56. int main (void);
  57.  
  58. void check_OrdinaryStuff        ( void );
  59.  
  60. void loose_Memory                  ( void );
  61.  
  62. void free_Twice                      ( void );
  63. void realloc_UnallocatedBlock    ( void );
  64.  
  65. void fill_Table            (    int            p_LeftBound,
  66.                         int            p_RightBound,
  67.                         t_CallKind    p_Allocator);
  68.                         
  69. void test_reallocOK                      (void);
  70. void test_realloc_FirstLeftBad          (void);
  71. void test_realloc_FirstRightBad          (void);
  72. void test_realloc_SecondLeftBad          (void);
  73. void test_realloc_SecondRightBad      (void);
  74.  
  75. void test_NullMalloc                (void);
  76. void test_NullCalloc                (void);
  77. void test_realloc_as_Malloc            (void);
  78. void test_realloc_as_Free            (void);
  79.  
  80. void allocate_HugeBlock                (void);
  81. void generate_MallocError            (void);
  82. void generate_ReallocError            (void);
  83.     
  84. /********************************************************************************/
  85. /**************************** Local functions ***********************************/
  86. /********************************************************************************/
  87.  
  88. void check_OrdinaryStuff(void)
  89.  
  90. {
  91. char * l_OrdinaryPointer;
  92.     
  93.     l_OrdinaryPointer = malloc (D_TableSize);
  94.     free (l_OrdinaryPointer);
  95.     
  96.     l_OrdinaryPointer = calloc(1,D_TableSize);
  97.     free(l_OrdinaryPointer);
  98.     
  99.     l_OrdinaryPointer = malloc(D_TableSize);
  100.     l_OrdinaryPointer = realloc(l_OrdinaryPointer, D_TableSize * 2);
  101.     free(l_OrdinaryPointer);
  102.  
  103.     l_OrdinaryPointer = malloc(D_TableSize);
  104.     l_OrdinaryPointer = realloc(l_OrdinaryPointer,D_TableSize / 2);
  105.     free(l_OrdinaryPointer);
  106.  
  107. /* no need to test free any further!    */    
  108. }
  109.  
  110. /********************************************************************************/
  111.  
  112. void loose_Memory(void)
  113.  
  114. {
  115. char * l_Text;
  116.     l_Text = malloc (255* sizeof (char));
  117.     strcpy(l_Text,"Lost memory!");
  118. }
  119.  
  120. /********************************************************************************/
  121.  
  122. void free_Twice          ( void )
  123.  
  124. {
  125. char * l_Char;
  126.     l_Char = malloc(sizeof(char));
  127.     free(l_Char);
  128.     free(l_Char);
  129. }
  130.  
  131. /********************************************************************************/
  132.  
  133. void realloc_UnallocatedBlock    ( void )
  134.  
  135. {
  136. double * l_Double;
  137.     l_Double = malloc(sizeof(double));
  138.     free(l_Double);
  139.     
  140.     l_Double = realloc(l_Double,2 * sizeof(double));
  141. }
  142.  
  143. /********************************************************************************/
  144.  
  145. void fill_Table      (    int            p_LeftBound,
  146.                     int            p_RightBound,
  147.                     t_CallKind    p_Allocator)
  148.  
  149. {
  150. char  *    l_Table;
  151. int     l_i;
  152.     switch (p_Allocator)
  153.     {
  154.         case c_Malloc : l_Table = malloc(D_TableSize *    sizeof(char));
  155.                         break;
  156.                         
  157.         case c_Calloc : l_Table = calloc(D_TableSize,    sizeof(char));
  158.                         break;
  159.                         
  160.         default          :    l_Table = NULL;
  161.                         fprintf(stderr,"Very fatal ERROR\n");
  162.                         abort();
  163.                         break;
  164.     }
  165.     
  166.     for (l_i = p_LeftBound; l_i < p_RightBound; l_i++)
  167.         *(l_Table + l_i) = (char) ('a' + l_i);
  168.     free (l_Table);
  169. }
  170.  
  171. /********************************************************************************/
  172.  
  173. void test_reallocOK      (    void)
  174.  
  175. {
  176. int      *    l_OKTable;
  177. int     l_i;
  178.     l_OKTable = malloc(D_TableSize *    sizeof(int));
  179.  
  180.     for (l_i = 0; l_i < D_TableSize;         l_i++)
  181.         l_OKTable[l_i] = l_i;
  182.  
  183.     l_OKTable = realloc(l_OKTable,    D_TableSize *    sizeof(int) *    2);
  184.     
  185.     for (l_i = 0; l_i < D_TableSize * 2;     l_i++)
  186.         l_OKTable[l_i] = l_i;
  187.  
  188.     free (l_OKTable);    
  189. }
  190.  
  191. /********************************************************************************/
  192.  
  193. void test_realloc_FirstLeftBad      (    void)
  194.  
  195. {
  196. int      *    l_FirstLeftTable;
  197. int     l_i;
  198.     l_FirstLeftTable = malloc(D_TableSize *    sizeof(int));
  199.  
  200.     for (l_i = -1; l_i < D_TableSize;         l_i++)
  201.         l_FirstLeftTable[l_i] = l_i;
  202.  
  203.     l_FirstLeftTable = realloc(l_FirstLeftTable,    D_TableSize *    sizeof(int) *    2);
  204.     
  205.     for (l_i = 0; l_i < D_TableSize * 2;     l_i++)
  206.         l_FirstLeftTable[l_i] = l_i;
  207.  
  208.     free (l_FirstLeftTable);    
  209. }
  210.  
  211. /********************************************************************************/
  212.  
  213. void test_realloc_FirstRightBad      (    void)
  214.  
  215. {
  216. int      *    l_FirstRightTable;
  217. int     l_i;
  218.     l_FirstRightTable = malloc(D_TableSize *    sizeof(int));
  219.  
  220.     for (l_i = 0; l_i < D_TableSize + 1;     l_i++)
  221.         l_FirstRightTable[l_i] = l_i;
  222.  
  223.     l_FirstRightTable = realloc(l_FirstRightTable,    D_TableSize *    sizeof(int) *    2);
  224.     
  225.     for (l_i = 0; l_i < D_TableSize * 2;     l_i++)
  226.         l_FirstRightTable[l_i] = l_i;
  227.  
  228.     free (l_FirstRightTable);    
  229. }
  230.  
  231. /********************************************************************************/
  232.  
  233. void test_realloc_SecondLeftBad      (    void)
  234.  
  235. {
  236. int      *    l_SecondLeftTable;
  237. int     l_i;
  238.     l_SecondLeftTable = malloc(D_TableSize *    sizeof(int));
  239.  
  240.     for (l_i = 0; l_i < D_TableSize;         l_i++)
  241.         l_SecondLeftTable[l_i] = l_i;
  242.  
  243.     l_SecondLeftTable = realloc(l_SecondLeftTable,    D_TableSize *    sizeof(int) *    2);
  244.     
  245.     for (l_i = -1; l_i < D_TableSize * 2;     l_i++)
  246.         l_SecondLeftTable[l_i] = l_i;
  247.  
  248.     free (    l_SecondLeftTable        );    
  249. }
  250.  
  251. /********************************************************************************/
  252.  
  253. void test_realloc_SecondRightBad      (    void)
  254.  
  255. {
  256. int      *    l_SecondRightTable;
  257. int     l_i;
  258.     l_SecondRightTable = malloc(D_TableSize *    sizeof(int));
  259.  
  260.     for (l_i = 0; l_i < D_TableSize;         l_i++)
  261.         l_SecondRightTable[l_i] = l_i;
  262.  
  263.     l_SecondRightTable = realloc(l_SecondRightTable,    D_TableSize *    sizeof(int) *    2);
  264.     
  265.     for (l_i = 0; l_i < D_TableSize * 2 + 1; l_i++)
  266.         l_SecondRightTable[l_i] = l_i;
  267.         
  268.     free (l_SecondRightTable    );    
  269. }
  270.  
  271. /********************************************************************************/
  272.  
  273. void test_NullMalloc                (void)
  274.  
  275. {
  276. int * l_EmptyTable;
  277.     l_EmptyTable = malloc(0);
  278.     free(l_EmptyTable);
  279. }
  280.  
  281.  
  282. /********************************************************************************/
  283.  
  284. void test_NullCalloc                (void)
  285.  
  286. {
  287. int * l_EmptyTable;
  288.     l_EmptyTable = calloc(0,0);
  289.     free(l_EmptyTable);
  290. }
  291.  
  292.  
  293. /********************************************************************************/
  294.  
  295. void test_realloc_as_Malloc            (void)
  296.  
  297. {
  298. int * l_Table;
  299.     l_Table = realloc(NULL,sizeof(int));
  300.     free(l_Table);
  301. }
  302.  
  303. /********************************************************************************/
  304.  
  305. void test_realloc_as_Free            (void)
  306.  
  307. {
  308. int * l_Table;
  309.     l_Table = malloc(sizeof(int));
  310.     l_Table = realloc(l_Table,0);
  311.     if (l_Table != 0)
  312.         free(l_Table);
  313. }
  314.  
  315. /********************************************************************************/
  316.  
  317. void allocate_HugeBlock(void)
  318.  
  319. {
  320. int * l_HugeBlock;
  321.  
  322. #ifndef _MSDOS
  323. #ifndef __MSDOS__
  324. l_HugeBlock = malloc(LONG_MAX); /* should be large enough...    */
  325.     if (l_HugeBlock != NULL)
  326.         free (l_HugeBlock);
  327. #endif
  328. #endif
  329. }
  330.  
  331. /********************************************************************************/
  332.  
  333. void generate_MallocError            (void)
  334.  
  335. {
  336. short * l_Short;
  337.     l_Short = malloc(sizeof(short));
  338.     if (l_Short != NULL)
  339.         free (l_Short);
  340. }
  341.     
  342. /********************************************************************************/
  343.  
  344. void generate_ReallocError            (void)
  345.  
  346. {
  347. short * l_Short;
  348.     l_Short = malloc(sizeof        (    short    )    );
  349.     generate_MemdebugError();
  350.     l_Short = realloc(l_Short,2*sizeof(l_Short)    );
  351.     if (l_Short != NULL)
  352.         free (l_Short);
  353. }
  354.     
  355. /********************************************************************************/
  356. /**************************** Global functions **********************************/
  357. /********************************************************************************/
  358.  
  359. int main (void)
  360.  
  361. {
  362. /*
  363. **    Check conventionnal calls
  364. */
  365.  
  366.     check_OrdinaryStuff();
  367.     
  368. /*
  369. **    Memory Leaks
  370. */
  371.     loose_Memory();
  372.  
  373. /*
  374. **    Illegal or dubious function calls
  375. */
  376.     free_Twice();
  377.     free((void *)1);
  378.     realloc_UnallocatedBlock();
  379.  
  380. /*
  381. **    Bound debuging
  382. */
  383.     fill_Table(0,    D_TableSize,    c_Malloc);    /* ok                */
  384.     fill_Table(0,    D_TableSize,    c_Calloc);
  385.  
  386.     fill_Table(-1,    D_TableSize,    c_Malloc);    /* 1 of at left        */
  387.     fill_Table(-1,    D_TableSize,    c_Calloc);
  388.  
  389.     fill_Table(0,    D_TableSize+1,    c_Malloc);    /* 1 of at right    */
  390.     fill_Table(0,    D_TableSize+1,    c_Calloc);
  391.  
  392.     test_reallocOK                      ();
  393.     test_realloc_FirstLeftBad          ();
  394.     test_realloc_FirstRightBad          ();
  395.     test_realloc_SecondLeftBad          ();
  396.     test_realloc_SecondRightBad      ();
  397.  
  398. /*
  399. **    Strange ways to call realloc: as malloc or free!
  400. */
  401.     test_realloc_as_Malloc();
  402.     test_realloc_as_Free();
  403.  
  404. /*
  405. **    Strange ways to call malloc & calloc: malloc(0), calloc(0,0)
  406. */
  407.  
  408.     test_NullMalloc();
  409.     test_NullCalloc();
  410. /*
  411. */
  412.  
  413. /*
  414. **    Out of memory error simulation
  415. */
  416.     allocate_HugeBlock();
  417.     generate_ReallocError();
  418.     generate_MallocError();
  419.         
  420.     return EXIT_SUCCESS;
  421. }
  422.  
  423. /* Test output
  424.  
  425.  
  426. File test.c         ; Line 128   # Freeing unallocated block : l_Char
  427. File test.c         ; Line 377   # Freeing unallocated block : (void *)1
  428. File test.c         ; Line 140   # Reallocating unallocated block : l_Double
  429. Memory block corrupted at start: l_Table
  430. Memory block corrupted at start: l_Table
  431. Memory block corrupted at end  : l_Table
  432. Memory block corrupted at end  : l_Table
  433. Memory block corrupted at start: l_FirstLeftTable
  434. Memory block corrupted at end  : l_FirstRightTable
  435. Memory block corrupted at start: l_SecondLeftTable
  436. Memory block corrupted at end  : l_SecondRightTable
  437. File test.c         ; Line 299   # Realloc used with NULL initial pointer. 
  438. Potential portability problem, calling malloc() instead
  439. File test.c         ; Line 310   # Realloc used with new block size 0. 
  440. Potential portability problem, calling free() instead
  441. File test.c         ; Line 277   # Malloc used with block size 0. 
  442. Potential portability problem, calling malloc(1) instead
  443. File test.c         ; Line 288   # Calloc used to allocate 0 bytes. 
  444. Potential portability problem, calling as calloc(1,1) instead
  445. File test.c         ; Line 324   # Out of memory while allocating LONG_MAX
  446. File test.c         ; Line 350   # Out of memory while reallocating 2*sizeof(l_Short) for l_Short
  447. File test.c         ; Line 337   # Out of memory while allocating sizeof(short)
  448.  
  449. ************************************
  450. **  Memory usage statistics:   1  **
  451. ************************************
  452.  
  453. Calls to Malloc                   20
  454. Calls to Calloc                    5
  455. Calls to Realloc                   9
  456. Calls to Free                     23
  457.  
  458. Total blocks allocated            23
  459. Currently allocated blocks         2
  460. Same time allocated blocks         2
  461.  
  462. Total memory allocated           932
  463. Currently allocated memory       257
  464. Same time allocated memory       359
  465.  
  466. Unsuccessful malloc calls          2
  467. Unsuccessful realloc calls         1
  468.  
  469. Realloc of unallocated blocks      1
  470. Free of unallocated blocks         2
  471. Spurious free calls                0
  472. Non-portable malloc calls          1
  473. Non-portable calloc calls          1
  474. Non-portable realloc calls         2
  475. Corrupted blocks                   8
  476.  
  477. ***************************************
  478. ** Alphabetic pointer variable list  **
  479. ***************************************
  480. Identifier                           Blocks   Size   Average Size
  481. l_Char                             :      1      1     1.00
  482. l_Double                           :      1      8     8.00
  483. l_EmptyTable                       :      2      2     1.00
  484. l_FirstLeftTable                   :      1    104   104.00
  485. l_FirstRightTable                  :      1    104   104.00
  486. l_OKTable                          :      1    104   104.00
  487. l_OrdinaryPointer                  :      4     58    14.50
  488. l_SecondLeftTable                  :      1    104   104.00
  489. l_SecondRightTable                 :      1    104   104.00
  490. l_Table                            :      8     86    10.75
  491.  
  492. ****************************************
  493. ** List of currently allocated blocks **
  494. ****************************************
  495.                                      Size  Size Expression     Content String
  496. File test.c         ; Line 348   #      2  sizeof ( short )    
  497. File test.c         ; Line 116   #    255  255* sizeof (char)  Lost memory!
  498.  
  499.  
  500. 2 pointers have not been freed.
  501.  
  502. */
  503.  
  504.