home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / windbase / memslc.3 / mem2.c < prev    next >
C/C++ Source or Header  |  1996-07-11  |  5KB  |  119 lines

  1. /*****************************************************************************\
  2. **                                                                           **
  3. **  WW     WW IIIIIIII NNN   NN DDDDDDD  BBBBBBB     AA     SSSSSS EEEEEEEE  **
  4. **  WW  W  WW    II    NNNN  NN DD    DD BB    BB  AA  AA  SS      EE        **
  5. **  WW  W  WW    II    NN NN NN DD    DD BBBBBBB  AAAAAAAA  SSSSSS EEEEEE    **
  6. **   WW W WW     II    NN  NNNN DD    DD BB    BB AA    AA      SS EE        **
  7. **    WWWWW   IIIIIIII NN   NNN DDDDDDD  BBBBBBB  AA    AA SSSSSS  EEEEEEEE  **
  8. **                                                                           **
  9. **   SSSSSS  OOOOOO  FFFFFFFF TTTTTTTT WW     WW    AA    RRRRRRR  EEEEEEEE  **
  10. **  SS      OO    OO FF          TT    WW  W  WW  AA  AA  RR    RR EE        **
  11. **   SSSSS  OO    OO FFFFF       TT    WW  W  WW AAAAAAAA RRRRRRR  EEEEEE    **
  12. **       SS OO    OO FF          TT     WW W WW  AA    AA RR   RR  EE        **
  13. **  SSSSSS   OOOOOO  FF          TT      WWWWW   AA    AA RR    RR EEEEEEEE  **
  14. **                                                                           **
  15. *********** NOTICE ************************************************************
  16. **        This file contains valuable trade secrets and proprietary          **
  17. **        assets of Windbase Software Inc.  Embodying substantial            **
  18. **        creative efforts and confidential information.  Unauthorized       **
  19. **        use, copying, decompiling, translating, disclosure or              **
  20. **        transfer, of any kind, is strictly prohibited.                     **
  21. **                                                                           **
  22. **        COPYRIGHT (C) 1992, 1993, 1994.  Windbase Software Inc.            **
  23. **        ALL RIGHTS RESERVED.                                               **
  24. \*****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <malloc.h>
  29.  
  30. #define WBMEMTRACE
  31. #include "../memsl.h"
  32. #include "mymal.h"
  33.  
  34. #if defined(WBTRC_LEVEL1) || defined(WBTRC_LEVEL2)
  35.   #ifdef WBSTDC
  36.     int main(int argc, char **argv)
  37.   #else
  38.     int main(argc, argv)
  39.       int argc;
  40.       char **argv;
  41.   #endif
  42. #else
  43.   #ifdef WBSTDC
  44.     int main(void)
  45.   #else
  46.     int main()
  47.   #endif
  48. #endif
  49.   {
  50.     WBMEM *mem;
  51.     WBERROR *errhdl;
  52.     FILE *file;
  53.     char str[21], *strptr[200];
  54.     int i = 0, i2;
  55.  
  56.     WBTrcMainEntry();
  57.  
  58.     /*
  59.     ** Set up to use user defined memory (see mymal.c)
  60.     ** By opening a memory handle and overriding malloc()
  61.     ** and free() with WBMallocMemH(), malloc() and free()
  62.     ** become user defined.
  63.     **
  64.     ** If we want tracing, we could simply use WB_TRUE in
  65.     ** the call to WBMemoryTracing().  Or, since tracing is
  66.     ** the default after the WBMemoryOpen() call, we can
  67.     ** simply omit the call to WBMemoryTracing().
  68.     **
  69.     ** As you can see, we can use both user defined memory
  70.     ** and tracing at the same time.
  71.     */
  72.     if ((errhdl = WBErrorOpen(0,NULL)) != NULL)
  73.       {
  74.         if ((mem = WBMemoryOpen(errhdl,NULL,MyMalloc,NULL,MyFree)) != NULL)
  75.           {
  76. /*            WBMemoryTracing(mem,WB_FALSE); */
  77.             WBMallocMemH(mem);
  78.  
  79.             if ((file = fopen("data.dat","r")) != NULL)
  80.               {
  81.                 while (i < 200 && fgets(str,20,file))
  82.                   {
  83.                     if (str[strlen(str)-1] == '\n')
  84.                       str[strlen(str)-1] = 0;
  85.  
  86.                     /*
  87.                     ** Example of user defined memory overriding
  88.                     ** malloc().  See the file mymal.c
  89.                     */
  90.                     if ((strptr[i] = malloc(strlen(str)+1)) != NULL)
  91.                       strcpy(strptr[i],str);
  92.                     i++;
  93.                   }
  94.                 fclose(file);
  95.  
  96.                 /*
  97.                 ** Example of user defined memory overriding
  98.                 ** free().  See the file mymal.c
  99.                 **
  100.                 ** If memory tracing is on, starting at 1
  101.                 ** instead of 0 should generate one "still
  102.                 ** allocated message".
  103.                 */
  104.                 for (i2 = 1; i2 < i; i2++)
  105.                   free(strptr[i2]);
  106.               }
  107.             /*
  108.             ** Tracing with user defined memory does not
  109.             ** automaticly call the close function.  Therefore,
  110.             ** WBMemoryClose() needs to be called after all
  111.             ** mallocs and frees.
  112.             */
  113.             WBMemoryClose(mem,1);
  114.           }
  115.         WBErrorClose(errhdl);
  116.       }
  117.     WBTrcReturn(0,0,("0"));
  118.   }
  119.