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