home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / JNEW.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  148 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*--------------------------------------------------------------*/
  4. /* Debugging extension by Jeff Dunlop                           */
  5. /* Copyright 1992-1993, DB/Soft Publishing Co.                  */
  6. /* License is hereby granted for use of JMalloc as a debugging  */
  7. /* aid in any program.  JMalloc may not be sold or distributed  */
  8. /* as or part of any for-profit debugging program or library    */
  9. /* nor may it be included in any for-profit library that offers */
  10. /* debugging features.  Any redistribution of JMalloc source    */
  11. /* must include this copyright notice.                          */
  12. /*--------------------------------------------------------------*/
  13.  
  14. /*--------------------------------------------------------------*/
  15. /* Usage:                                                       */
  16. /*  If you want line number information available on your       */
  17. /*  allocated memory blocks, your new and delete calls have to  */
  18. /*  be replaced with NEW() and DELETE() macro calls.  This is   */
  19. /*  not absolutely required but finding where an orphaned block */
  20. /*  was allocated is much easier if the line information can be */
  21. /*  saved.  Otherwise, JNew is a drop-in replacement for        */
  22. /*  operators new and delete.                                   */
  23. /*                                                              */
  24. /*  If you compile jnew.cpp and jmalloc.c into .obj files, your */
  25. /*  linker will find jnew's operator new before that in the     */
  26. /*  standard library.  If you store jnew.obj and jmalloc.obj in */
  27. /*  a library file, that library must appear in your list       */
  28. /*  before the standard library or the linker will link in the  */
  29. /*  standard library's operator new.  Define DBUG to turn on    */
  30. /*  line numbers.  Undefine DBUG and remove the jnew binaries   */
  31. /*  from your linker list to disable.                           */
  32. /*                                                              */
  33. /*  There is a bit of memory overhead involved in having        */
  34. /*  diagnostic information stored for every allocated block.    */
  35. /*  Depending on your compiler, this can run from 30 bytes per  */
  36. /*  block with Borland C++ to 50+ bytes per block for C Set/2.  */
  37. /*  If your compiler supports duplicate string merging and your */
  38. /*  program won't break using it, file information stored in    */
  39. /*  the static data area will be compressed.                    */
  40. /*  If memory is tight and you want to use jnew on a            */
  41. /*  file-by-file basis you will need to redefine the NEW and    */
  42. /*  DELETE macros to use a different operator and rename        */
  43. /*  operators new and delete in jnew.cpp.  Performance doesn't  */
  44. /*  take much of a hit unless you have a LOT of small allocated */
  45. /*  blocks floating around most of the time.  Then, searching   */
  46. /*  the diagnostic list can become significant.                 */
  47. /*--------------------------------------------------------------*/
  48.  
  49. #define DBUG
  50.  
  51. #include <stdlib.h>
  52. #include <string.h>
  53.  
  54. #include <new.h>
  55. #include "extkword.h"
  56. #include "jmalloc.h"
  57. #include "jnew.h"
  58.  
  59. static char *db_file;
  60. static int db_line;
  61.  
  62. #ifdef _MSC_VER
  63.  typedef int (CDECL *NEW_HANDLER)(size_t);
  64.  #define set_new_handler _set_new_handler
  65. #else
  66.  typedef void (*NEW_HANDLER)(void);
  67. #endif
  68.  
  69. void db_set(char *file, int line)
  70. {
  71.     if ( db_file )
  72.         free(db_file);
  73.     db_file = strdup(file);
  74.     db_line = line;
  75. }
  76.  
  77. void *operator new(size_t n)
  78. {
  79.     void *q;
  80.  
  81.     NEW_HANDLER new_handler = set_new_handler(NULL);
  82.     set_new_handler(new_handler);
  83.  
  84.     while ( (q = j_malloc(n, db_file, db_line)) == NULL )
  85.     {
  86.         if ( new_handler )
  87.         {
  88. #ifdef _MSC_VER
  89.             new_handler(n);
  90. #else
  91.             new_handler();
  92. #endif
  93.         }
  94.         else
  95.             return NULL;
  96.     }
  97.     return q;
  98. }
  99.  
  100. void operator delete(void *p)
  101. {
  102.     j_free(p, db_file, db_line);
  103. }
  104.  
  105. #ifdef TEST
  106.  
  107. int main(void)
  108. {
  109.     char *n,
  110.          *p,
  111.          *q,
  112.          r[30],
  113.          *s[256] = {NULL};
  114.     int i = 1;
  115.  
  116.     n = NEW(char[64 * 56]);
  117.     JStrCpy(n + 10, "Test string");
  118.     DBUG_PRINT("alloc", ("r is invalid"));
  119.     JStrCpy(r, "Test");
  120.     q = NEW(char[16]);
  121.     q[16] = 3;
  122.  
  123.     DBUG_PRINT("alloc", ("p was never allocated"));
  124.     DELETE(p);
  125.  
  126.     i = 0;
  127.  
  128.     DBUG_PRINT("alloc", ("Deplete memory"));
  129.     do
  130.     {
  131.         s[i] = NEW(char[32000]);
  132.         i++;
  133.     } while ( s[i - 1] != NULL );
  134.  
  135.     i = 0;
  136.     while ( s[i] != NULL )
  137.     {
  138.         DELETE(s[i++]);
  139.     }
  140.  
  141.     DBUG_PRINT("alloc", ("New and q are orphaned blocks"));
  142.     DBUG_PRINT("alloc", ("q has an overrun"));
  143.     JMemcheck(1);
  144.     return 0;
  145. }
  146.  
  147. #endif /* TEST */
  148.