]> git.lyx.org Git - lyx.git/blob - development/Win32/vld/src/callstack.h
add leak tool for msvc 'Visual Leak Detection' 1.9f: original files
[lyx.git] / development / Win32 / vld / src / callstack.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //  $Id: callstack.h,v 1.10 2006/11/18 03:12:34 dmouldin Exp $
3 //
4 //  Visual Leak Detector - CallStack Class Definitions
5 //  Copyright (c) 2005-2006 Dan Moulding
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Lesser General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2.1 of the License, or (at your option) any later version.
11 //
12 //  This library is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //  Lesser General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Lesser General Public
18 //  License along with this library; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 //
21 //  See COPYING.txt for the full terms of the GNU Lesser General Public License.
22 //
23 ////////////////////////////////////////////////////////////////////////////////
24
25 #pragma once
26
27 #ifndef VLDBUILD
28 #error \
29 "This header should only be included by Visual Leak Detector when building it from source. \
30 Applications should never include this header."
31 #endif
32
33 #include <windows.h>
34
35 #define CALLSTACKCHUNKSIZE 32 // Number of frame slots in each CallStack chunk.
36
37 ////////////////////////////////////////////////////////////////////////////////
38 //
39 //  The CallStack Class
40 //
41 //    CallStack objects can be used for obtaining, storing, and displaying the
42 //    call stack at a given point during program execution.
43 //
44 //    The primary data structure used by the CallStack is similar in concept to
45 //    a STL vector, but is specifically tailored for use by VLD, making it more
46 //    efficient than a standard STL vector.
47 //
48 //    Inside the CallStack are a number of "chunks" which are arranged in a
49 //    linked list. Each chunk contains an array of frames (each frame is
50 //    represented by a program counter address). If we run out of space when
51 //    pushing new frames onto an existing chunk in the CallStack chunk list,
52 //    then a new chunk is allocated and appended to the end of the list. In this
53 //    way, the CallStack can grow dynamically as needed. New frames are always
54 //    pushed onto the chunk at the end of the list known as the "top" chunk.
55 //
56 class CallStack
57 {
58 public:
59     CallStack ();
60     CallStack (const CallStack &other);
61     ~CallStack ();
62
63     // Public APIs - see each function definition for details.
64     VOID clear ();
65     VOID dump (BOOL showinternalframes) const;
66     virtual VOID getstacktrace (UINT32 maxdepth, SIZE_T *framepointer) = 0;
67     CallStack& operator = (const CallStack &other);
68     BOOL operator == (const CallStack &other) const;
69     SIZE_T operator [] (UINT32 index) const;
70     VOID push_back (const SIZE_T programcounter);
71
72 protected:
73     // Protected data.
74     UINT32 m_status;                    // Status flags:
75 #define CALLSTACK_STATUS_INCOMPLETE 0x1 //   If set, the stack trace stored in this CallStack appears to be incomplete.
76
77 private:
78     // The chunk list is made of a linked list of Chunks.
79     typedef struct chunk_s {
80         struct chunk_s *next;                        // Pointer to the next chunk in the chunk list.
81         SIZE_T          frames [CALLSTACKCHUNKSIZE]; // Pushed frames (program counter addresses) are stored in this array.
82     } chunk_t;
83
84     // Private data.
85     UINT32              m_capacity; // Current capacity limit (in frames)
86     UINT32              m_size;     // Current size (in frames)
87     CallStack::chunk_t  m_store;    // Pointer to the underlying data store (i.e. head of the chunk list)
88     CallStack::chunk_t *m_topchunk; // Pointer to the chunk at the top of the stack
89     UINT32              m_topindex; // Index, within the top chunk, of the top of the stack
90 };
91
92
93 ////////////////////////////////////////////////////////////////////////////////
94 //
95 //  The FastCallStack Class
96 //
97 //    This class is a specialization of the CallStack class which provides a
98 //    very fast stack tracing function.
99 //
100 class FastCallStack : public CallStack
101 {
102 public:
103     VOID getstacktrace (UINT32 maxdepth, SIZE_T *framepointer);
104 };
105
106 ////////////////////////////////////////////////////////////////////////////////
107 //
108 //  The SafeCallStack Class
109 //
110 //    This class is a specialization of the CallStack class which provides a
111 //    more robust, but quite slow, stack tracing function.
112 //
113 class SafeCallStack : public CallStack
114 {
115 public:
116     VOID getstacktrace (UINT32 maxdepth, SIZE_T *framepointer);
117 };