gallium: Dual source support in blend_factor_to_shader
[mesa.git] / src / gallium / auxiliary / util / u_debug_refcnt.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #if defined(DEBUG)
28
29 /**
30 * If the GALLIUM_REFCNT_LOG env var is defined as a filename, gallium
31 * reference counting will be logged to the file.
32 *
33 * See http://www-archive.mozilla.org/performance/refcnt-balancer.html
34 * for what to do with the output on Linux, use tools/addr2line.sh to
35 * postprocess it before anything else.
36 */
37
38 #include <stdio.h>
39
40 #include "util/u_debug.h"
41 #include "util/u_debug_refcnt.h"
42 #include "util/u_debug_stack.h"
43 #include "util/u_debug_symbol.h"
44 #include "util/u_string.h"
45 #include "util/u_hash_table.h"
46 #include "os/os_thread.h"
47 #include "pipe/p_config.h"
48
49 int debug_refcnt_state;
50
51 static FILE *stream;
52
53 /* TODO: maybe move this serial machinery to a stand-alone module and
54 * expose it?
55 */
56 static mtx_t serials_mutex = _MTX_INITIALIZER_NP;
57
58 static struct hash_table *serials_hash;
59 static unsigned serials_last;
60
61
62 /**
63 * Return a small integer serial number for the given pointer.
64 */
65 static boolean
66 debug_serial(void *p, unsigned *pserial)
67 {
68 unsigned serial;
69 boolean found = TRUE;
70 #ifdef PIPE_OS_WINDOWS
71 static boolean first = TRUE;
72
73 if (first) {
74 (void) mtx_init(&serials_mutex, mtx_plain);
75 first = FALSE;
76 }
77 #endif
78
79 mtx_lock(&serials_mutex);
80 if (!serials_hash)
81 serials_hash = util_hash_table_create_ptr_keys();
82
83 serial = (unsigned) (uintptr_t) util_hash_table_get(serials_hash, p);
84 if (!serial) {
85 /* time to stop logging... (you'll have a 100 GB logfile at least at
86 * this point) TODO: avoid this
87 */
88 serial = ++serials_last;
89 if (!serial) {
90 debug_error("More than 2^32 objects detected, aborting.\n");
91 os_abort();
92 }
93
94 _mesa_hash_table_insert(serials_hash, p, (void *) (uintptr_t) serial);
95 found = FALSE;
96 }
97 mtx_unlock(&serials_mutex);
98
99 *pserial = serial;
100
101 return found;
102 }
103
104
105 /**
106 * Free the serial number for the given pointer.
107 */
108 static void
109 debug_serial_delete(void *p)
110 {
111 mtx_lock(&serials_mutex);
112 _mesa_hash_table_remove_key(serials_hash, p);
113 mtx_unlock(&serials_mutex);
114 }
115
116
117 #if defined(PIPE_OS_WINDOWS)
118 #define STACK_LEN 60
119 #else
120 #define STACK_LEN 64
121 #endif
122
123 /**
124 * Log a reference count change to the log file (if enabled).
125 * This is called via the pipe_reference() and debug_reference() functions,
126 * basically whenever a reference count is initialized or changed.
127 *
128 * \param p the refcount being changed (the value is not changed here)
129 * \param get_desc a function which will be called to print an object's
130 * name/pointer into a string buffer during logging
131 * \param change the reference count change which must be +/-1 or 0 when
132 * creating the object and initializing the refcount.
133 */
134 void
135 debug_reference_slowpath(const struct pipe_reference *p,
136 debug_reference_descriptor get_desc, int change)
137 {
138 assert(change >= -1);
139 assert(change <= 1);
140
141 if (debug_refcnt_state < 0)
142 return;
143
144 if (!debug_refcnt_state) {
145 const char *filename = debug_get_option("GALLIUM_REFCNT_LOG", NULL);
146 if (filename && filename[0])
147 stream = fopen(filename, "wt");
148
149 if (stream)
150 debug_refcnt_state = 1;
151 else
152 debug_refcnt_state = -1;
153 }
154
155 if (debug_refcnt_state > 0) {
156 struct debug_stack_frame frames[STACK_LEN];
157 char buf[1024];
158 unsigned i;
159 unsigned refcnt = p->count;
160 unsigned serial;
161 boolean existing = debug_serial((void *) p, &serial);
162
163 debug_backtrace_capture(frames, 1, STACK_LEN);
164
165 get_desc(buf, p);
166
167 if (!existing) {
168 fprintf(stream, "<%s> %p %u Create\n", buf, (void *) p, serial);
169 debug_backtrace_print(stream, frames, STACK_LEN);
170
171 /* this is here to provide a gradual change even if we don't see
172 * the initialization
173 */
174 for (i = 1; i <= refcnt - change; ++i) {
175 fprintf(stream, "<%s> %p %u AddRef %u\n", buf, (void *) p,
176 serial, i);
177 debug_backtrace_print(stream, frames, STACK_LEN);
178 }
179 }
180
181 if (change) {
182 fprintf(stream, "<%s> %p %u %s %u\n", buf, (void *) p, serial,
183 change > 0 ? "AddRef" : "Release", refcnt);
184 debug_backtrace_print(stream, frames, STACK_LEN);
185 }
186
187 if (!refcnt) {
188 debug_serial_delete((void *) p);
189 fprintf(stream, "<%s> %p %u Destroy\n", buf, (void *) p, serial);
190 debug_backtrace_print(stream, frames, STACK_LEN);
191 }
192
193 fflush(stream);
194 }
195 }
196
197 #endif /* DEBUG */