home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwphescr.zip / XWPH0208.ZIP / include / helpers / memdebug.h < prev    next >
C/C++ Source or Header  |  2002-05-13  |  8KB  |  225 lines

  1.  
  2. /*
  3.  *@@sourcefile memdebug.h:
  4.  *      header file for memdebug.c.
  5.  *      See remarks there.
  6.  *
  7.  *      The following macros are used:
  8.  *
  9.  *      --  __XWPMEMDEBUG__: if defined, memory debugging is generally
  10.  *                           enabled. This must be set in setup.h.
  11.  *
  12.  *      --  __DEBUG_MALLOC_ENABLED__: malloc etc. have been replaced
  13.  *                           with memdMalloc etc. This is automatically
  14.  *                           defined by this header if __XWPMEMDEBUG__
  15.  *                           is defined, unless DONT_REPLACE_MALLOC
  16.  *                           is also defined.
  17.  *
  18.  *      Note: Version numbering in this file relates to XWorkplace version
  19.  *            numbering.
  20.  *
  21.  *@@include #include <os2.h>
  22.  *@@include #include "helpers\memdebug.h"
  23.  */
  24.  
  25. /*      Copyright (C) 2000 Ulrich Möller.
  26.  *      This file is part of the "XWorkplace helpers" source package.
  27.  *      This is free software; you can redistribute it and/or modify
  28.  *      it under the terms of the GNU General Public License as published
  29.  *      by the Free Software Foundation, in version 2 as it comes in the
  30.  *      "COPYING" file of the XWorkplace main distribution.
  31.  *      This program is distributed in the hope that it will be useful,
  32.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  *      GNU General Public License for more details.
  35.  */
  36.  
  37. #if __cplusplus
  38. extern "C" {
  39. #endif
  40.  
  41. #ifndef MEMDEBUG_HEADER_INCLUDED
  42.     #define MEMDEBUG_HEADER_INCLUDED
  43.  
  44.     #ifndef __stdlib_h           // <stdlib.h>
  45.         // #error stdlib.h must be included before memdebug.h.
  46.         typedef unsigned int size_t;
  47.     #endif
  48.  
  49.     #ifndef NULL
  50.        #if (defined(__EXTENDED__)  || defined( __cplusplus ))
  51.           #define NULL 0
  52.        #else
  53.           #define NULL ((void *)0)
  54.        #endif
  55.     #endif
  56.  
  57.     typedef void (FNCBMEMDLOG)(const char*);        // message
  58.     typedef FNCBMEMDLOG *PFNCBMEMDLOG;
  59.  
  60.     // global variable for memory error logger func
  61.     extern PFNCBMEMDLOG    G_pMemdLogFunc;
  62.  
  63.     /* ******************************************************************
  64.      *
  65.      *   Private declarations
  66.      *
  67.      ********************************************************************/
  68.  
  69.     #ifdef MEMDEBUG_PRIVATE
  70.  
  71.         BOOL memdLock(VOID);
  72.  
  73.         VOID memdUnlock(VOID);
  74.  
  75.         /*
  76.          *@@ HEAPITEM:
  77.          *      informational structure created for each
  78.          *      malloc() call by memdMalloc. These are stored
  79.          *      in a global linked list (G_pHeapItemsRoot).
  80.          *
  81.          *      We cannot use the linklist.c functions for
  82.          *      managing the linked list because these use
  83.          *      malloc in turn, which would lead to infinite
  84.          *      loops.
  85.          *
  86.          *@@added V0.9.3 (2000-04-11) [umoeller]
  87.          */
  88.  
  89.         typedef struct _HEAPITEM
  90.         {
  91.             // struct _HEAPITEM    *pNext;     // next item in linked list or NULL if last
  92.  
  93.             TREE                Tree;           // tree node for tree.* functions;
  94.                                                 // ulKey has the pointer that is returned
  95.                                                 // by memdMalloc and points to after the
  96.                                                 // magic string (in the buffer that was
  97.                                                 // really allocated). Using this as the
  98.                                                 // tree sort key allows us to do fast
  99.                                                 // searches in memdFree.
  100.  
  101.             // void                *pAfterMagic; // memory pointer returned by memdMalloc;
  102.                                               // this points to after the magic string
  103.  
  104.             unsigned long       ulSize;     // requested size (without magic head and tail)
  105.  
  106.             const char          *pcszSourceFile;    // as passed to memdMalloc
  107.             unsigned long       ulLine;             // as passed to memdMalloc
  108.             const char          *pcszFunction;      // as passed to memdMalloc
  109.  
  110.             DATETIME            dtAllocated;        // system date/time at time of memdMalloc call
  111.  
  112.             ULONG               ulTID;      // thread ID that memdMalloc was running on
  113.  
  114.             BOOL                fFreed;     // TRUE only after item has been freed by memdFree
  115.         } HEAPITEM, *PHEAPITEM;
  116.  
  117.         extern TREE         *G_pHeapItemsRoot;
  118.         extern LONG         G_cHeapItems;
  119.         extern ULONG        G_ulItemsReleased;
  120.         extern ULONG        G_ulBytesReleased;
  121.  
  122.     #endif // MEMDEBUG_PRIVATE
  123.  
  124.     /* ******************************************************************
  125.      *
  126.      *   Publics
  127.      *
  128.      ********************************************************************/
  129.  
  130.     void* memdMalloc(size_t stSize,
  131.                      const char *pcszSourceFile,
  132.                      unsigned long ulLine,
  133.                      const char *pcszFunction);
  134.  
  135.     void* memdCalloc(size_t num,
  136.                      size_t stSize,
  137.                      const char *pcszSourceFile,
  138.                      unsigned long ulLine,
  139.                      const char *pcszFunction);
  140.  
  141.     void memdFree(void *p,
  142.                   const char *pcszSourceFile,
  143.                   unsigned long ulLine,
  144.                   const char *pcszFunction);
  145.  
  146.     void* memdRealloc(void *p,
  147.                       size_t stSize,
  148.                       const char *pcszSourceFile,
  149.                       unsigned long ulLine,
  150.                       const char *pcszFunction);
  151.  
  152.     unsigned long memdReleaseFreed(void);
  153.  
  154.     #ifdef __XWPMEMDEBUG__
  155.  
  156.         #ifndef DONT_REPLACE_MALLOC
  157.  
  158.             #ifdef malloc
  159.                 #undef malloc
  160.             #endif
  161.             #define malloc(ul) memdMalloc(ul, __FILE__, __LINE__, __FUNCTION__)
  162.  
  163.             #ifdef calloc
  164.                 #undef calloc
  165.             #endif
  166.             #define calloc(n, size) memdCalloc(n, size, __FILE__, __LINE__, __FUNCTION__)
  167.  
  168.             #ifdef realloc
  169.                 #undef realloc
  170.             #endif
  171.             #define realloc(p, ul) memdRealloc(p, ul, __FILE__, __LINE__, __FUNCTION__)
  172.  
  173.             #ifdef free
  174.                 #undef free
  175.             #endif
  176.             #define free(p) memdFree(p, __FILE__, __LINE__, __FUNCTION__)
  177.  
  178.             #ifdef __string_h
  179.                 // string.h included and debugging is on:
  180.                 // redefine strdup to use memory debugging
  181.                 #define strdup(psz)                                 \
  182.                     strcpy( (char*)memdMalloc(strlen(psz) + 1, __FILE__, __LINE__, __FUNCTION__), psz)
  183.                     // the original crashes also if psz is NULL
  184.             #endif
  185.  
  186.             // tell other headers that these have been replaced
  187.             #define __DEBUG_MALLOC_ENABLED__
  188.  
  189.         #endif
  190.     #endif
  191.  
  192.     #ifdef PM_INCLUDED
  193.         /********************************************************************
  194.          *
  195.          *   XFolder debugging helpers
  196.          *
  197.          ********************************************************************/
  198.  
  199.         #ifdef _PMPRINTF_
  200.             void memdDumpMemoryBlock(PBYTE pb,
  201.                                      ULONG ulSize,
  202.                                      ULONG ulIndent);
  203.         #else
  204.             // _PMPRINTF not #define'd: do nothing
  205.             #define memdDumpMemoryBlock(pb, ulSize, ulIndent)
  206.         #endif
  207.  
  208.         /* ******************************************************************
  209.          *
  210.          *   Heap debugging window
  211.          *
  212.          ********************************************************************/
  213.  
  214.         MRESULT EXPENTRY memd_fnwpMemDebug(HWND hwndClient, ULONG msg, MPARAM mp1, MPARAM mp2);
  215.  
  216.         VOID memdCreateMemDebugWindow(VOID);
  217.     #endif
  218.  
  219. #endif
  220.  
  221. #if __cplusplus
  222. }
  223. #endif
  224.  
  225.