1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file is compiled twice: once for the generator programs
23 once for the compiler. */
31 #include "coretypes.h"
32 #include "hash-table.h"
38 #include "diagnostic-core.h"
41 /* Vector memory usage. */
42 class vec_usage
: public mem_usage
45 /* Default constructor. */
46 vec_usage (): m_items (0), m_items_peak (0), m_element_size (0) {}
49 vec_usage (size_t allocated
, size_t times
, size_t peak
,
50 size_t items
, size_t items_peak
, size_t element_size
)
51 : mem_usage (allocated
, times
, peak
),
52 m_items (items
), m_items_peak (items_peak
),
53 m_element_size (element_size
) {}
55 /* Sum the usage with SECOND usage. */
57 operator+ (const vec_usage
&second
)
59 return vec_usage (m_allocated
+ second
.m_allocated
,
60 m_times
+ second
.m_times
,
61 m_peak
+ second
.m_peak
,
62 m_items
+ second
.m_items
,
63 m_items_peak
+ second
.m_items_peak
, 0);
66 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
68 dump (mem_location
*loc
, mem_usage
&total
) const
71 sprintf (s
, "%s:%i (%s)", loc
->get_trimmed_filename (),
72 loc
->m_line
, loc
->m_function
);
77 "%-48s %10" PRIu64
PRsa (10) ":%4.1f%%" PRsa (9) "%10" PRIu64
78 ":%4.1f%%" PRsa (10) PRsa (10) "\n",
80 (uint64_t)m_element_size
,
81 SIZE_AMOUNT (m_allocated
),
82 m_allocated
* 100.0 / total
.m_allocated
,
83 SIZE_AMOUNT (m_peak
), (uint64_t)m_times
,
84 m_times
* 100.0 / total
.m_times
,
85 SIZE_AMOUNT (m_items
), SIZE_AMOUNT (m_items_peak
));
92 fprintf (stderr
, "%s" PRsa (64) PRsa (25) PRsa (16) "\n",
93 "Total", SIZE_AMOUNT (m_allocated
),
94 SIZE_AMOUNT (m_times
), SIZE_AMOUNT (m_items
));
97 /* Dump header with NAME. */
99 dump_header (const char *name
)
101 fprintf (stderr
, "%-48s %10s%11s%16s%10s%17s%11s\n", name
, "sizeof(T)",
102 "Leak", "Peak", "Times", "Leak items", "Peak items");
105 /* Current number of items allocated. */
107 /* Peak value of number of allocated items. */
109 /* Size of element of the vector. */
110 size_t m_element_size
;
113 /* Vector memory description. */
114 static mem_alloc_description
<vec_usage
> vec_mem_desc
;
116 /* Account the overhead. */
119 vec_prefix::register_overhead (void *ptr
, size_t elements
,
120 size_t element_size MEM_STAT_DECL
)
122 vec_mem_desc
.register_descriptor (ptr
, VEC_ORIGIN
, false
123 FINAL_PASS_MEM_STAT
);
125 = vec_mem_desc
.register_instance_overhead (elements
* element_size
, ptr
);
126 usage
->m_element_size
= element_size
;
127 usage
->m_items
+= elements
;
128 if (usage
->m_items_peak
< usage
->m_items
)
129 usage
->m_items_peak
= usage
->m_items
;
132 /* Notice that the memory allocated for the vector has been freed. */
135 vec_prefix::release_overhead (void *ptr
, size_t size
, size_t elements
,
136 bool in_dtor MEM_STAT_DECL
)
138 if (!vec_mem_desc
.contains_descriptor_for_instance (ptr
))
139 vec_mem_desc
.register_descriptor (ptr
, VEC_ORIGIN
,
140 false FINAL_PASS_MEM_STAT
);
141 vec_usage
*usage
= vec_mem_desc
.release_instance_overhead (ptr
, size
,
143 usage
->m_items
-= elements
;
146 /* Calculate the number of slots to reserve a vector, making sure that
147 it is of at least DESIRED size by growing ALLOC exponentially. */
150 vec_prefix::calculate_allocation_1 (unsigned alloc
, unsigned desired
)
152 /* We must have run out of room. */
153 gcc_assert (alloc
< desired
);
155 /* Exponential growth. */
159 /* Double when small. */
162 /* Grow slower when large. */
163 alloc
= (alloc
* 3 / 2);
165 /* If this is still too small, set it to the right size. */
171 /* Dump per-site memory statistics. */
174 dump_vec_loc_statistics (void)
176 vec_mem_desc
.dump (VEC_ORIGIN
);
180 /* Report qsort comparator CMP consistency check failure with P1, P2, P3 as
182 ATTRIBUTE_NORETURN ATTRIBUTE_COLD
184 qsort_chk_error (const void *p1
, const void *p2
, const void *p3
,
185 sort_r_cmp_fn
*cmp
, void *data
)
189 int r1
= cmp (p1
, p2
, data
), r2
= cmp (p2
, p1
, data
);
190 error ("qsort comparator not anti-symmetric: %d, %d", r1
, r2
);
194 int r
= cmp (p1
, p3
, data
);
195 error ("qsort comparator non-negative on sorted output: %d", r
);
199 int r1
= cmp (p1
, p2
, data
);
200 int r2
= cmp (p2
, p3
, data
);
201 int r3
= cmp (p1
, p3
, data
);
202 error ("qsort comparator not transitive: %d, %d, %d", r1
, r2
, r3
);
204 internal_error ("qsort checking failed");
207 /* Verify anti-symmetry and transitivity for comparator CMP on sorted array
208 of N SIZE-sized elements pointed to by BASE. */
210 qsort_chk (void *base
, size_t n
, size_t size
, sort_r_cmp_fn
*cmp
, void *data
)
215 /* Limit overall time complexity to O(n log n). */
216 #define LIM(n) ((n) <= 16 ? (n) : 12 + floor_log2 (n))
218 #define ELT(i) ((const char *) base + (i) * size)
219 #define CMP(i, j) cmp (ELT (i), ELT (j), data)
220 #define ERR2(i, j) qsort_chk_error (ELT (i), ELT (j), NULL, cmp, data)
221 #define ERR3(i, j, k) qsort_chk_error (ELT (i), ELT (j), ELT (k), cmp, data)
223 /* This outer loop iterates over maximum spans [i1, i2) such that
224 elements within each span compare equal to each other. */
225 for (i1
= 0; i1
< n
; i1
= i2
)
227 /* Position i2 one past last element that compares equal to i1'th. */
228 for (i2
= i1
+ 1; i2
< n
; i2
++)
231 else if (CMP (i2
, i1
))
233 size_t lim1
= LIM (i2
- i1
), lim2
= LIM (n
- i2
);
234 /* Verify that other pairs within current span compare equal. */
235 for (i
= i1
+ 1; i
+ 1 < i2
; i
++)
236 for (j
= i
+ 1; j
< i1
+ lim1
; j
++)
241 /* Verify that elements within this span compare less than
242 elements beyond the span. */
243 for (i
= i1
; i
< i2
; i
++)
244 for (j
= i2
; j
< i2
+ lim2
; j
++)
247 else if (CMP (j
, i
) <= 0)
256 #endif /* #if CHECKING_P */
258 #ifndef GENERATOR_FILE
265 /* Call V.safe_push for all ints from START up to, but not including LIMIT.
266 Helper function for selftests. */
269 safe_push_range (vec
<int>&v
, int start
, int limit
)
271 for (int i
= start
; i
< limit
; i
++)
275 /* Verify forms of initialization. */
282 ASSERT_EQ (0, v1
.length ());
285 ASSERT_EQ (0, v2
.length ());
289 vec
<int> v1
= vec
<int>();
290 ASSERT_EQ (0, v1
.length ());
293 ASSERT_EQ (0, v2
.length ());
298 ASSERT_EQ (0, v1
.length ());
302 ASSERT_EQ (1, v1
.length ());
305 ASSERT_EQ (2, v1
.length ());
306 ASSERT_EQ (2, v2
.length ());
311 /* Verify that vec::quick_push works correctly. */
317 ASSERT_EQ (0, v
.length ());
319 ASSERT_EQ (0, v
.length ());
320 ASSERT_TRUE (v
.space (3));
324 ASSERT_EQ (3, v
.length ());
330 /* Verify that vec::safe_push works correctly. */
336 ASSERT_EQ (0, v
.length ());
340 ASSERT_EQ (3, v
.length ());
346 /* Verify that vec::truncate works correctly. */
352 ASSERT_EQ (0, v
.length ());
353 safe_push_range (v
, 0, 10);
354 ASSERT_EQ (10, v
.length ());
357 ASSERT_EQ (5, v
.length ());
360 /* Verify that vec::safe_grow_cleared works correctly. */
363 test_safe_grow_cleared ()
366 ASSERT_EQ (0, v
.length ());
367 v
.safe_grow_cleared (50, true);
368 ASSERT_EQ (50, v
.length ());
370 ASSERT_EQ (0, v
[49]);
373 /* Verify that vec::pop works correctly. */
379 safe_push_range (v
, 5, 20);
380 ASSERT_EQ (15, v
.length ());
383 ASSERT_EQ (19, last
);
384 ASSERT_EQ (14, v
.length ());
387 /* Verify that vec::safe_insert works correctly. */
393 safe_push_range (v
, 0, 10);
394 v
.safe_insert (5, 42);
396 ASSERT_EQ (42, v
[5]);
398 ASSERT_EQ (11, v
.length ());
401 /* Verify that vec::ordered_remove works correctly. */
404 test_ordered_remove ()
407 safe_push_range (v
, 0, 10);
408 v
.ordered_remove (5);
411 ASSERT_EQ (9, v
.length ());
414 /* Verify that vec::ordered_remove_if works correctly. */
417 test_ordered_remove_if (void)
420 safe_push_range (v
, 0, 10);
423 VEC_ORDERED_REMOVE_IF (v
, ix
, ix2
, elem_ptr
,
424 *elem_ptr
== 5 || *elem_ptr
== 7);
428 ASSERT_EQ (8, v
.length ());
431 safe_push_range (v
, 0, 10);
432 VEC_ORDERED_REMOVE_IF_FROM_TO (v
, ix
, ix2
, elem_ptr
, 0, 6,
433 *elem_ptr
== 5 || *elem_ptr
== 7);
437 ASSERT_EQ (9, v
.length ());
440 safe_push_range (v
, 0, 10);
441 VEC_ORDERED_REMOVE_IF_FROM_TO (v
, ix
, ix2
, elem_ptr
, 0, 5,
442 *elem_ptr
== 5 || *elem_ptr
== 7);
443 VEC_ORDERED_REMOVE_IF_FROM_TO (v
, ix
, ix2
, elem_ptr
, 8, 10,
444 *elem_ptr
== 5 || *elem_ptr
== 7);
448 ASSERT_EQ (10, v
.length ());
451 safe_push_range (v
, 0, 10);
452 VEC_ORDERED_REMOVE_IF (v
, ix
, ix2
, elem_ptr
, *elem_ptr
== 5);
456 ASSERT_EQ (9, v
.length ());
459 /* Verify that vec::unordered_remove works correctly. */
462 test_unordered_remove ()
465 safe_push_range (v
, 0, 10);
466 v
.unordered_remove (5);
467 ASSERT_EQ (9, v
.length ());
470 /* Verify that vec::block_remove works correctly. */
476 safe_push_range (v
, 0, 10);
477 v
.block_remove (5, 3);
482 ASSERT_EQ (7, v
.length ());
485 /* Comparator for use by test_qsort. */
488 reverse_cmp (const void *p_i
, const void *p_j
)
490 return *(const int *)p_j
- *(const int *)p_i
;
493 /* Verify that vec::qsort works correctly. */
499 safe_push_range (v
, 0, 10);
500 v
.qsort (reverse_cmp
);
505 ASSERT_EQ (10, v
.length ());
508 /* Verify that vec::reverse works correctly. */
513 /* Reversing an empty vec ought to be a no-op. */
516 ASSERT_EQ (0, v
.length ());
518 ASSERT_EQ (0, v
.length ());
521 /* Verify reversing a vec with even length. */
524 safe_push_range (v
, 0, 4);
530 ASSERT_EQ (4, v
.length ());
533 /* Verify reversing a vec with odd length. */
536 safe_push_range (v
, 0, 3);
541 ASSERT_EQ (3, v
.length ());
545 /* A test class that increments a counter every time its dtor is called. */
550 count_dtor (int *counter
) : m_counter (counter
) {}
551 ~count_dtor () { (*m_counter
)++; }
557 /* Verify that auto_delete_vec deletes the elements within it. */
560 test_auto_delete_vec ()
564 auto_delete_vec
<count_dtor
> v
;
565 v
.safe_push (new count_dtor (&dtor_count
));
566 v
.safe_push (new count_dtor (&dtor_count
));
568 ASSERT_EQ (dtor_count
, 2);
571 /* Verify accesses to vector elements are done indirectly. */
582 for (int ix
= i
; v
.iterate (ix
, &val
); ix
++)
587 /* Run all of the selftests within this file. */
596 test_safe_grow_cleared ();
599 test_ordered_remove ();
600 test_ordered_remove_if ();
601 test_unordered_remove ();
602 test_block_remove ();
605 test_auto_delete_vec ();
609 } // namespace selftest
611 #endif /* #if CHECKING_P */
612 #endif /* #ifndef GENERATOR_FILE */