]>
Commit | Line | Data |
---|---|---|
40d6b753 | 1 | /* Header file for libgcov-*.c. |
99dee823 | 2 | Copyright (C) 1996-2021 Free Software Foundation, Inc. |
40d6b753 RX |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | Under Section 7 of GPL version 3, you are granted additional | |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | You should have received a copy of the GNU General Public License and | |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | #ifndef GCC_LIBGCOV_H | |
26 | #define GCC_LIBGCOV_H | |
27 | ||
28 | /* work around the poisoned malloc/calloc in system.h. */ | |
29 | #ifndef xmalloc | |
30 | #define xmalloc malloc | |
31 | #endif | |
32 | #ifndef xcalloc | |
33 | #define xcalloc calloc | |
34 | #endif | |
35 | ||
c77556a5 RX |
36 | #ifndef IN_GCOV_TOOL |
37 | /* About the target. */ | |
38 | /* This path will be used by libgcov runtime. */ | |
39 | ||
40d6b753 | 40 | #include "tconfig.h" |
a51a76e5 | 41 | #include "auto-target.h" |
40d6b753 RX |
42 | #include "tsystem.h" |
43 | #include "coretypes.h" | |
44 | #include "tm.h" | |
45 | #include "libgcc_tm.h" | |
46928a8f | 46 | #include "gcov.h" |
40d6b753 | 47 | |
00d79dc4 ML |
48 | #if HAVE_SYS_MMAN_H |
49 | #include <sys/mman.h> | |
50 | #endif | |
51 | ||
a153644f | 52 | #if __CHAR_BIT__ == 8 |
40d6b753 RX |
53 | typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI))); |
54 | typedef unsigned gcov_position_t __attribute__ ((mode (SI))); | |
55 | #if LONG_LONG_TYPE_SIZE > 32 | |
56 | typedef signed gcov_type __attribute__ ((mode (DI))); | |
57 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (DI))); | |
58 | #else | |
59 | typedef signed gcov_type __attribute__ ((mode (SI))); | |
60 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI))); | |
61 | #endif | |
62 | #else | |
a153644f | 63 | #if __CHAR_BIT__ == 16 |
40d6b753 RX |
64 | typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI))); |
65 | typedef unsigned gcov_position_t __attribute__ ((mode (HI))); | |
66 | #if LONG_LONG_TYPE_SIZE > 32 | |
67 | typedef signed gcov_type __attribute__ ((mode (SI))); | |
68 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI))); | |
69 | #else | |
70 | typedef signed gcov_type __attribute__ ((mode (HI))); | |
71 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI))); | |
72 | #endif | |
73 | #else | |
74 | typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI))); | |
75 | typedef unsigned gcov_position_t __attribute__ ((mode (QI))); | |
76 | #if LONG_LONG_TYPE_SIZE > 32 | |
77 | typedef signed gcov_type __attribute__ ((mode (HI))); | |
78 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI))); | |
79 | #else | |
80 | typedef signed gcov_type __attribute__ ((mode (QI))); | |
81 | typedef unsigned gcov_type_unsigned __attribute__ ((mode (QI))); | |
82 | #endif | |
83 | #endif | |
84 | #endif | |
85 | ||
86 | #if defined (TARGET_POSIX_IO) | |
87 | #define GCOV_LOCKED 1 | |
88 | #else | |
89 | #define GCOV_LOCKED 0 | |
90 | #endif | |
91 | ||
eb3480fc ML |
92 | #ifndef GCOV_SUPPORTS_ATOMIC |
93 | /* Detect whether target can support atomic update of profilers. */ | |
94 | #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 | |
95 | #define GCOV_SUPPORTS_ATOMIC 1 | |
96 | #else | |
97 | #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 | |
98 | #define GCOV_SUPPORTS_ATOMIC 1 | |
99 | #else | |
100 | #define GCOV_SUPPORTS_ATOMIC 0 | |
101 | #endif | |
102 | #endif | |
103 | #endif | |
104 | ||
19926161 NS |
105 | /* In libgcov we need these functions to be extern, so prefix them with |
106 | __gcov. In libgcov they must also be hidden so that the instance in | |
107 | the executable is not also used in a DSO. */ | |
108 | #define gcov_var __gcov_var | |
109 | #define gcov_open __gcov_open | |
110 | #define gcov_close __gcov_close | |
111 | #define gcov_write_tag_length __gcov_write_tag_length | |
112 | #define gcov_position __gcov_position | |
113 | #define gcov_seek __gcov_seek | |
114 | #define gcov_rewrite __gcov_rewrite | |
115 | #define gcov_is_error __gcov_is_error | |
116 | #define gcov_write_unsigned __gcov_write_unsigned | |
117 | #define gcov_write_counter __gcov_write_counter | |
118 | #define gcov_write_summary __gcov_write_summary | |
119 | #define gcov_read_unsigned __gcov_read_unsigned | |
120 | #define gcov_read_counter __gcov_read_counter | |
121 | #define gcov_read_summary __gcov_read_summary | |
19926161 | 122 | |
c77556a5 RX |
123 | #else /* IN_GCOV_TOOL */ |
124 | /* About the host. */ | |
125 | /* This path will be compiled for the host and linked into | |
126 | gcov-tool binary. */ | |
127 | ||
128 | #include "config.h" | |
129 | #include "system.h" | |
130 | #include "coretypes.h" | |
131 | #include "tm.h" | |
132 | ||
133 | typedef unsigned gcov_unsigned_t; | |
134 | typedef unsigned gcov_position_t; | |
135 | /* gcov_type is typedef'd elsewhere for the compiler */ | |
136 | #if defined (HOST_HAS_F_SETLKW) | |
137 | #define GCOV_LOCKED 1 | |
138 | #else | |
139 | #define GCOV_LOCKED 0 | |
140 | #endif | |
141 | ||
142 | /* Some Macros specific to gcov-tool. */ | |
143 | ||
144 | #define L_gcov 1 | |
145 | #define L_gcov_merge_add 1 | |
596341c7 | 146 | #define L_gcov_merge_topn 1 |
c77556a5 RX |
147 | #define L_gcov_merge_ior 1 |
148 | #define L_gcov_merge_time_profile 1 | |
149 | ||
c77556a5 RX |
150 | extern gcov_type gcov_read_counter_mem (); |
151 | extern unsigned gcov_get_merge_weight (); | |
d10ee722 | 152 | extern struct gcov_info *gcov_list; |
c77556a5 RX |
153 | |
154 | #endif /* !IN_GCOV_TOOL */ | |
155 | ||
40d6b753 RX |
156 | #if defined(inhibit_libc) |
157 | #define IN_LIBGCOV (-1) | |
158 | #else | |
159 | #define IN_LIBGCOV 1 | |
160 | #if defined(L_gcov) | |
161 | #define GCOV_LINKAGE /* nothing */ | |
162 | #endif | |
163 | #endif | |
164 | ||
40d6b753 RX |
165 | /* Poison these, so they don't accidentally slip in. */ |
166 | #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length | |
17d1594b | 167 | #pragma GCC poison gcov_time |
40d6b753 RX |
168 | |
169 | #ifdef HAVE_GAS_HIDDEN | |
170 | #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden"))) | |
171 | #else | |
172 | #define ATTRIBUTE_HIDDEN | |
173 | #endif | |
174 | ||
175 | #include "gcov-io.h" | |
176 | ||
177 | /* Structures embedded in coveraged program. The structures generated | |
178 | by write_profile must match these. */ | |
179 | ||
180 | /* Information about counters for a single function. */ | |
181 | struct gcov_ctr_info | |
182 | { | |
183 | gcov_unsigned_t num; /* number of counters. */ | |
184 | gcov_type *values; /* their values. */ | |
185 | }; | |
186 | ||
187 | /* Information about a single function. This uses the trailing array | |
188 | idiom. The number of counters is determined from the merge pointer | |
189 | array in gcov_info. The key is used to detect which of a set of | |
190 | comdat functions was selected -- it points to the gcov_info object | |
191 | of the object file containing the selected comdat function. */ | |
192 | ||
193 | struct gcov_fn_info | |
194 | { | |
195 | const struct gcov_info *key; /* comdat key */ | |
196 | gcov_unsigned_t ident; /* unique ident of function */ | |
197 | gcov_unsigned_t lineno_checksum; /* function lineo_checksum */ | |
198 | gcov_unsigned_t cfg_checksum; /* function cfg checksum */ | |
72602c6c | 199 | struct gcov_ctr_info ctrs[1]; /* instrumented counters */ |
40d6b753 RX |
200 | }; |
201 | ||
202 | /* Type of function used to merge counters. */ | |
203 | typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t); | |
204 | ||
205 | /* Information about a single object file. */ | |
206 | struct gcov_info | |
207 | { | |
208 | gcov_unsigned_t version; /* expected version number */ | |
209 | struct gcov_info *next; /* link to next, used by libgcov */ | |
210 | ||
211 | gcov_unsigned_t stamp; /* uniquifying time stamp */ | |
212 | const char *filename; /* output file name */ | |
213 | ||
214 | gcov_merge_fn merge[GCOV_COUNTERS]; /* merge functions (null for | |
215 | unused) */ | |
216 | ||
217 | unsigned n_functions; /* number of functions */ | |
c77556a5 RX |
218 | |
219 | #ifndef IN_GCOV_TOOL | |
40d6b753 | 220 | const struct gcov_fn_info *const *functions; /* pointer to pointers |
c77556a5 RX |
221 | to function information */ |
222 | #else | |
5fc312a9 | 223 | struct gcov_fn_info **functions; |
88891c5f | 224 | struct gcov_summary summary; |
c77556a5 | 225 | #endif /* !IN_GCOV_TOOL */ |
40d6b753 RX |
226 | }; |
227 | ||
4303c581 NS |
228 | /* Root of a program/shared-object state */ |
229 | struct gcov_root | |
230 | { | |
231 | struct gcov_info *list; | |
232 | unsigned dumped : 1; /* counts have been dumped. */ | |
233 | unsigned run_counted : 1; /* run has been accounted for. */ | |
cadb2b96 NS |
234 | struct gcov_root *next; |
235 | struct gcov_root *prev; | |
4303c581 NS |
236 | }; |
237 | ||
238 | extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN; | |
239 | ||
cadb2b96 NS |
240 | struct gcov_master |
241 | { | |
242 | gcov_unsigned_t version; | |
243 | struct gcov_root *root; | |
244 | }; | |
3edbcdbe ML |
245 | |
246 | struct indirect_call_tuple | |
247 | { | |
248 | /* Callee function. */ | |
249 | void *callee; | |
250 | ||
251 | /* Pointer to counters. */ | |
252 | gcov_type *counters; | |
253 | }; | |
cadb2b96 NS |
254 | |
255 | /* Exactly one of these will be active in the process. */ | |
256 | extern struct gcov_master __gcov_master; | |
00d79dc4 ML |
257 | extern struct gcov_kvp *__gcov_kvp_dynamic_pool; |
258 | extern unsigned __gcov_kvp_dynamic_pool_index; | |
259 | extern unsigned __gcov_kvp_dynamic_pool_size; | |
cadb2b96 | 260 | |
4303c581 NS |
261 | /* Dump a set of gcov objects. */ |
262 | extern void __gcov_dump_one (struct gcov_root *) ATTRIBUTE_HIDDEN; | |
263 | ||
40d6b753 RX |
264 | /* Register a new object file module. */ |
265 | extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN; | |
266 | ||
8c9434c2 ML |
267 | /* GCOV exit function registered via a static destructor. */ |
268 | extern void __gcov_exit (void) ATTRIBUTE_HIDDEN; | |
269 | ||
b20ee094 NS |
270 | /* Function to reset all counters to 0. Both externally visible (and |
271 | overridable) and internal version. */ | |
b20ee094 | 272 | extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN; |
40d6b753 | 273 | |
cadb2b96 | 274 | /* User function to enable early write of profile information so far. */ |
cadb2b96 | 275 | extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN; |
40d6b753 | 276 | |
d39f7dc8 ML |
277 | /* Lock critical section for __gcov_dump and __gcov_reset functions. */ |
278 | extern void __gcov_lock (void) ATTRIBUTE_HIDDEN; | |
279 | ||
280 | /* Unlock critical section for __gcov_dump and __gcov_reset functions. */ | |
281 | extern void __gcov_unlock (void) ATTRIBUTE_HIDDEN; | |
282 | ||
40d6b753 RX |
283 | /* The merge function that just sums the counters. */ |
284 | extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN; | |
285 | ||
286 | /* The merge function to select the minimum valid counter value. */ | |
287 | extern void __gcov_merge_time_profile (gcov_type *, unsigned) ATTRIBUTE_HIDDEN; | |
288 | ||
596341c7 ML |
289 | /* The merge function to choose the most common N values. */ |
290 | extern void __gcov_merge_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN; | |
40d6b753 | 291 | |
40d6b753 RX |
292 | /* The merge function that just ors the counters together. */ |
293 | extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN; | |
294 | ||
295 | /* The profiler functions. */ | |
296 | extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned); | |
a266236e ML |
297 | extern void __gcov_interval_profiler_atomic (gcov_type *, gcov_type, int, |
298 | unsigned); | |
40d6b753 | 299 | extern void __gcov_pow2_profiler (gcov_type *, gcov_type); |
a266236e | 300 | extern void __gcov_pow2_profiler_atomic (gcov_type *, gcov_type); |
596341c7 ML |
301 | extern void __gcov_topn_values_profiler (gcov_type *, gcov_type); |
302 | extern void __gcov_topn_values_profiler_atomic (gcov_type *, gcov_type); | |
92d41717 | 303 | extern void __gcov_indirect_call_profiler_v4 (gcov_type, void *); |
3ae37f92 | 304 | extern void __gcov_indirect_call_profiler_v4_atomic (gcov_type, void *); |
40d6b753 | 305 | extern void __gcov_time_profiler (gcov_type *); |
a266236e | 306 | extern void __gcov_time_profiler_atomic (gcov_type *); |
40d6b753 | 307 | extern void __gcov_average_profiler (gcov_type *, gcov_type); |
a266236e | 308 | extern void __gcov_average_profiler_atomic (gcov_type *, gcov_type); |
40d6b753 | 309 | extern void __gcov_ior_profiler (gcov_type *, gcov_type); |
a266236e | 310 | extern void __gcov_ior_profiler_atomic (gcov_type *, gcov_type); |
40d6b753 RX |
311 | |
312 | #ifndef inhibit_libc | |
313 | /* The wrappers around some library functions.. */ | |
314 | extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN; | |
315 | extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN; | |
316 | extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN; | |
317 | extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN; | |
318 | extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN; | |
319 | extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN; | |
320 | extern int __gcov_execve (const char *, char *const [], char *const []) | |
321 | ATTRIBUTE_HIDDEN; | |
322 | ||
323 | /* Functions that only available in libgcov. */ | |
324 | GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN; | |
325 | GCOV_LINKAGE void gcov_write_counter (gcov_type) ATTRIBUTE_HIDDEN; | |
326 | GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t) | |
327 | ATTRIBUTE_HIDDEN; | |
328 | GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/, | |
329 | const struct gcov_summary *) | |
330 | ATTRIBUTE_HIDDEN; | |
331 | GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN; | |
19926161 | 332 | GCOV_LINKAGE void gcov_rewrite (void) ATTRIBUTE_HIDDEN; |
40d6b753 | 333 | |
c77556a5 RX |
334 | /* "Counts" stored in gcda files can be a real counter value, or |
335 | an target address. When differentiate these two types because | |
336 | when manipulating counts, we should only change real counter values, | |
337 | rather target addresses. */ | |
338 | ||
339 | static inline gcov_type | |
340 | gcov_get_counter (void) | |
341 | { | |
342 | #ifndef IN_GCOV_TOOL | |
343 | /* This version is for reading count values in libgcov runtime: | |
344 | we read from gcda files. */ | |
345 | ||
346 | return gcov_read_counter (); | |
347 | #else | |
348 | /* This version is for gcov-tool. We read the value from memory and | |
349 | multiply it by the merge weight. */ | |
350 | ||
351 | return gcov_read_counter_mem () * gcov_get_merge_weight (); | |
352 | #endif | |
353 | } | |
354 | ||
92d41717 ML |
355 | /* Similar function as gcov_get_counter(), but do not scale |
356 | when read value is equal to IGNORE_SCALING. */ | |
357 | ||
358 | static inline gcov_type | |
54e2d83c | 359 | gcov_get_counter_ignore_scaling (gcov_type ignore_scaling ATTRIBUTE_UNUSED) |
92d41717 ML |
360 | { |
361 | #ifndef IN_GCOV_TOOL | |
362 | /* This version is for reading count values in libgcov runtime: | |
363 | we read from gcda files. */ | |
364 | ||
365 | return gcov_read_counter (); | |
366 | #else | |
367 | /* This version is for gcov-tool. We read the value from memory and | |
368 | multiply it by the merge weight. */ | |
369 | ||
370 | gcov_type v = gcov_read_counter_mem (); | |
371 | if (v != ignore_scaling) | |
372 | v *= gcov_get_merge_weight (); | |
373 | ||
374 | return v; | |
375 | #endif | |
376 | } | |
377 | ||
c77556a5 RX |
378 | /* Similar function as gcov_get_counter(), but handles target address |
379 | counters. */ | |
380 | ||
381 | static inline gcov_type | |
382 | gcov_get_counter_target (void) | |
383 | { | |
384 | #ifndef IN_GCOV_TOOL | |
385 | /* This version is for reading count target values in libgcov runtime: | |
386 | we read from gcda files. */ | |
387 | ||
388 | return gcov_read_counter (); | |
389 | #else | |
390 | /* This version is for gcov-tool. We read the value from memory and we do NOT | |
391 | multiply it by the merge weight. */ | |
392 | ||
393 | return gcov_read_counter_mem (); | |
394 | #endif | |
395 | } | |
396 | ||
871e5ada ML |
397 | /* Add VALUE to *COUNTER and make it with atomic operation |
398 | if USE_ATOMIC is true. */ | |
399 | ||
400 | static inline void | |
eb3480fc ML |
401 | gcov_counter_add (gcov_type *counter, gcov_type value, |
402 | int use_atomic ATTRIBUTE_UNUSED) | |
871e5ada | 403 | { |
eb3480fc | 404 | #if GCOV_SUPPORTS_ATOMIC |
871e5ada ML |
405 | if (use_atomic) |
406 | __atomic_fetch_add (counter, value, __ATOMIC_RELAXED); | |
407 | else | |
eb3480fc | 408 | #endif |
871e5ada ML |
409 | *counter += value; |
410 | } | |
411 | ||
6a8fc0c3 ML |
412 | #if HAVE_SYS_MMAN_H |
413 | ||
414 | /* Allocate LENGTH with mmap function. */ | |
415 | ||
416 | static inline void * | |
417 | malloc_mmap (size_t length) | |
418 | { | |
419 | return mmap (NULL, length, PROT_READ | PROT_WRITE, | |
420 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
421 | } | |
422 | ||
423 | #endif | |
424 | ||
14e19b82 ML |
425 | /* Allocate gcov_kvp from statically pre-allocated pool, |
426 | or use heap otherwise. */ | |
bc2b1a23 ML |
427 | |
428 | static inline struct gcov_kvp * | |
429 | allocate_gcov_kvp (void) | |
430 | { | |
00d79dc4 | 431 | #define MMAP_CHUNK_SIZE (128 * 1024) |
bc2b1a23 | 432 | struct gcov_kvp *new_node = NULL; |
00d79dc4 ML |
433 | unsigned kvp_sizeof = sizeof(struct gcov_kvp); |
434 | ||
435 | /* Try mmaped pool if available. */ | |
436 | #if !defined(IN_GCOV_TOOL) && !defined(L_gcov_merge_topn) && HAVE_SYS_MMAN_H | |
437 | if (__gcov_kvp_dynamic_pool == NULL | |
438 | || __gcov_kvp_dynamic_pool_index >= __gcov_kvp_dynamic_pool_size) | |
439 | { | |
6a8fc0c3 | 440 | void *ptr = malloc_mmap (MMAP_CHUNK_SIZE); |
00d79dc4 ML |
441 | if (ptr != MAP_FAILED) |
442 | { | |
443 | __gcov_kvp_dynamic_pool = ptr; | |
444 | __gcov_kvp_dynamic_pool_size = MMAP_CHUNK_SIZE / kvp_sizeof; | |
445 | __gcov_kvp_dynamic_pool_index = 0; | |
446 | } | |
447 | } | |
bc2b1a23 | 448 | |
00d79dc4 | 449 | if (__gcov_kvp_dynamic_pool != NULL) |
bc2b1a23 ML |
450 | { |
451 | unsigned index; | |
452 | #if GCOV_SUPPORTS_ATOMIC | |
453 | index | |
00d79dc4 ML |
454 | = __atomic_fetch_add (&__gcov_kvp_dynamic_pool_index, 1, |
455 | __ATOMIC_RELAXED); | |
bc2b1a23 | 456 | #else |
00d79dc4 | 457 | index = __gcov_kvp_dynamic_pool_index++; |
bc2b1a23 | 458 | #endif |
00d79dc4 ML |
459 | if (index < __gcov_kvp_dynamic_pool_size) |
460 | new_node = __gcov_kvp_dynamic_pool + index; | |
bc2b1a23 | 461 | } |
bc2b1a23 | 462 | #endif |
14e19b82 | 463 | |
00d79dc4 | 464 | /* Fallback to malloc. */ |
14e19b82 | 465 | if (new_node == NULL) |
00d79dc4 | 466 | new_node = (struct gcov_kvp *)xcalloc (1, kvp_sizeof); |
bc2b1a23 ML |
467 | |
468 | return new_node; | |
469 | } | |
470 | ||
871e5ada ML |
471 | /* Add key value pair VALUE:COUNT to a top N COUNTERS. When INCREMENT_TOTAL |
472 | is true, add COUNT to total of the TOP counter. If USE_ATOMIC is true, | |
5089df53 ML |
473 | do it in atomic way. Return true when the counter is full, otherwise |
474 | return false. */ | |
871e5ada | 475 | |
5089df53 | 476 | static inline unsigned |
871e5ada ML |
477 | gcov_topn_add_value (gcov_type *counters, gcov_type value, gcov_type count, |
478 | int use_atomic, int increment_total) | |
479 | { | |
480 | if (increment_total) | |
d40b21ee ML |
481 | { |
482 | /* In the multi-threaded mode, we can have an already merged profile | |
483 | with a negative total value. In that case, we should bail out. */ | |
484 | if (counters[0] < 0) | |
485 | return 0; | |
486 | gcov_counter_add (&counters[0], 1, use_atomic); | |
487 | } | |
871e5ada ML |
488 | |
489 | struct gcov_kvp *prev_node = NULL; | |
490 | struct gcov_kvp *minimal_node = NULL; | |
862b9b22 | 491 | struct gcov_kvp *current_node = (struct gcov_kvp *)(intptr_t)counters[2]; |
871e5ada ML |
492 | |
493 | while (current_node) | |
494 | { | |
495 | if (current_node->value == value) | |
496 | { | |
497 | gcov_counter_add (¤t_node->count, count, use_atomic); | |
5089df53 | 498 | return 0; |
871e5ada ML |
499 | } |
500 | ||
501 | if (minimal_node == NULL | |
502 | || current_node->count < minimal_node->count) | |
503 | minimal_node = current_node; | |
504 | ||
505 | prev_node = current_node; | |
506 | current_node = current_node->next; | |
507 | } | |
508 | ||
509 | if (counters[1] == GCOV_TOPN_MAXIMUM_TRACKED_VALUES) | |
510 | { | |
511 | if (--minimal_node->count < count) | |
512 | { | |
513 | minimal_node->value = value; | |
514 | minimal_node->count = count; | |
515 | } | |
5089df53 ML |
516 | |
517 | return 1; | |
871e5ada ML |
518 | } |
519 | else | |
520 | { | |
bc2b1a23 ML |
521 | struct gcov_kvp *new_node = allocate_gcov_kvp (); |
522 | if (new_node == NULL) | |
5089df53 | 523 | return 0; |
bc2b1a23 | 524 | |
871e5ada ML |
525 | new_node->value = value; |
526 | new_node->count = count; | |
527 | ||
528 | int success = 0; | |
529 | if (!counters[2]) | |
862b9b22 ML |
530 | { |
531 | #if GCOV_SUPPORTS_ATOMIC | |
532 | if (use_atomic) | |
533 | { | |
534 | struct gcov_kvp **ptr = (struct gcov_kvp **)(intptr_t)&counters[2]; | |
535 | success = !__sync_val_compare_and_swap (ptr, 0, new_node); | |
536 | } | |
537 | else | |
538 | #endif | |
539 | { | |
540 | counters[2] = (intptr_t)new_node; | |
541 | success = 1; | |
542 | } | |
543 | } | |
871e5ada | 544 | else if (prev_node && !prev_node->next) |
862b9b22 ML |
545 | { |
546 | #if GCOV_SUPPORTS_ATOMIC | |
547 | if (use_atomic) | |
548 | success = !__sync_val_compare_and_swap (&prev_node->next, 0, | |
549 | new_node); | |
550 | else | |
551 | #endif | |
552 | { | |
553 | prev_node->next = new_node; | |
554 | success = 1; | |
555 | } | |
556 | } | |
871e5ada ML |
557 | |
558 | /* Increment number of nodes. */ | |
559 | if (success) | |
560 | gcov_counter_add (&counters[1], 1, use_atomic); | |
561 | } | |
5089df53 ML |
562 | |
563 | return 0; | |
871e5ada ML |
564 | } |
565 | ||
40d6b753 RX |
566 | #endif /* !inhibit_libc */ |
567 | ||
568 | #endif /* GCC_LIBGCOV_H */ |