BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file DebugObject.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Object used for detecting leaks.
32 */
33  
34 #ifndef BADVPN_DEBUGOBJECT_H
35 #define BADVPN_DEBUGOBJECT_H
36  
37 #include <stdint.h>
38  
39 #if !defined(BADVPN_THREAD_SAFE) || (BADVPN_THREAD_SAFE != 0 && BADVPN_THREAD_SAFE != 1)
40 #error BADVPN_THREAD_SAFE is not defined or incorrect
41 #endif
42  
43 #if BADVPN_THREAD_SAFE
44 #include <pthread.h>
45 #endif
46  
47 #include <misc/debug.h>
48 #include <misc/debugcounter.h>
49  
50 #define DEBUGOBJECT_VALID UINT32_C(0x31415926)
51  
52 /**
53 * Object used for detecting leaks.
54 */
55 typedef struct {
56 #ifndef NDEBUG
57 uint32_t c;
58 #else
59 int dummy_field; // struct must have at least one field
60 #endif
61 } DebugObject;
62  
63 /**
64 * Initializes the object.
65 *
66 * @param obj the object
67 */
68 static void DebugObject_Init (DebugObject *obj);
69  
70 /**
71 * Frees the object.
72 *
73 * @param obj the object
74 */
75 static void DebugObject_Free (DebugObject *obj);
76  
77 /**
78 * Does nothing.
79 *
80 * @param obj the object
81 */
82 static void DebugObject_Access (const DebugObject *obj);
83  
84 /**
85 * Does nothing.
86 * There must be no {@link DebugObject}'s initialized.
87 */
88 static void DebugObjectGlobal_Finish (void);
89  
90 #ifndef NDEBUG
91 extern DebugCounter debugobject_counter;
92 #if BADVPN_THREAD_SAFE
93 extern pthread_mutex_t debugobject_mutex;
94 #endif
95 #endif
96  
97 void DebugObject_Init (DebugObject *obj)
98 {
99 #ifndef NDEBUG
100  
101 obj->c = DEBUGOBJECT_VALID;
102  
103 #if BADVPN_THREAD_SAFE
104 ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
105 #endif
106  
107 DebugCounter_Increment(&debugobject_counter);
108  
109 #if BADVPN_THREAD_SAFE
110 ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
111 #endif
112  
113 #endif
114 }
115  
116 void DebugObject_Free (DebugObject *obj)
117 {
118 ASSERT(obj->c == DEBUGOBJECT_VALID)
119  
120 #ifndef NDEBUG
121  
122 obj->c = 0;
123  
124 #if BADVPN_THREAD_SAFE
125 ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
126 #endif
127  
128 DebugCounter_Decrement(&debugobject_counter);
129  
130 #if BADVPN_THREAD_SAFE
131 ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
132 #endif
133  
134 #endif
135 }
136  
137 void DebugObject_Access (const DebugObject *obj)
138 {
139 ASSERT(obj->c == DEBUGOBJECT_VALID)
140 }
141  
142 void DebugObjectGlobal_Finish (void)
143 {
144 #ifndef NDEBUG
145 DebugCounter_Free(&debugobject_counter);
146 #endif
147 }
148  
149 #endif