Mercurial > hg > CbC > CbC_gcc
diff gcc/vec.c @ 145:1830386684a0
gcc-9.2.0
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 11:34:05 +0900 |
parents | 84e7813d76e9 |
children |
line wrap: on
line diff
--- a/gcc/vec.c Thu Oct 25 07:37:49 2018 +0900 +++ b/gcc/vec.c Thu Feb 13 11:34:05 2020 +0900 @@ -1,5 +1,5 @@ /* Vector API for GNU compiler. - Copyright (C) 2004-2018 Free Software Foundation, Inc. + Copyright (C) 2004-2020 Free Software Foundation, Inc. Contributed by Nathan Sidwell <nathan@codesourcery.com> Re-implemented in C++ by Diego Novillo <dnovillo@google.com> @@ -49,16 +49,18 @@ vnull vNULL; /* Vector memory usage. */ -struct vec_usage: public mem_usage +class vec_usage: public mem_usage { +public: /* Default constructor. */ - vec_usage (): m_items (0), m_items_peak (0) {} + vec_usage (): m_items (0), m_items_peak (0), m_element_size (0) {} /* Constructor. */ vec_usage (size_t allocated, size_t times, size_t peak, - size_t items, size_t items_peak) + size_t items, size_t items_peak, size_t element_size) : mem_usage (allocated, times, peak), - m_items (items), m_items_peak (items_peak) {} + m_items (items), m_items_peak (items_peak), + m_element_size (element_size) {} /* Sum the usage with SECOND usage. */ vec_usage @@ -68,7 +70,7 @@ m_times + second.m_times, m_peak + second.m_peak, m_items + second.m_items, - m_items_peak + second.m_items_peak); + m_items_peak + second.m_items_peak, 0); } /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */ @@ -81,35 +83,41 @@ s[48] = '\0'; - fprintf (stderr, "%-48s %10li:%4.1f%%%10li%10li:%4.1f%%%11li%11li\n", s, - (long)m_allocated, m_allocated * 100.0 / total.m_allocated, - (long)m_peak, (long)m_times, m_times * 100.0 / total.m_times, - (long)m_items, (long)m_items_peak); + fprintf (stderr, + "%-48s %10" PRIu64 PRsa (10) ":%4.1f%%" PRsa (9) "%10" PRIu64 + ":%4.1f%%" PRsa (10) PRsa (10) "\n", + s, + (uint64_t)m_element_size, + SIZE_AMOUNT (m_allocated), + m_allocated * 100.0 / total.m_allocated, + SIZE_AMOUNT (m_peak), (uint64_t)m_times, + m_times * 100.0 / total.m_times, + SIZE_AMOUNT (m_items), SIZE_AMOUNT (m_items_peak)); } /* Dump footer. */ inline void dump_footer () { - print_dash_line (); - fprintf (stderr, "%s%55li%25li%17li\n", "Total", (long)m_allocated, - (long)m_times, (long)m_items); - print_dash_line (); + fprintf (stderr, "%s" PRsa (64) PRsa (25) PRsa (16) "\n", + "Total", SIZE_AMOUNT (m_allocated), + SIZE_AMOUNT (m_times), SIZE_AMOUNT (m_items)); } /* Dump header with NAME. */ static inline void dump_header (const char *name) { - fprintf (stderr, "%-48s %11s%15s%10s%17s%11s\n", name, "Leak", "Peak", - "Times", "Leak items", "Peak items"); - print_dash_line (); + fprintf (stderr, "%-48s %10s%11s%16s%10s%17s%11s\n", name, "sizeof(T)", + "Leak", "Peak", "Times", "Leak items", "Peak items"); } /* Current number of items allocated. */ size_t m_items; /* Peak value of number of allocated items. */ size_t m_items_peak; + /* Size of element of the vector. */ + size_t m_element_size; }; /* Vector memory description. */ @@ -118,12 +126,14 @@ /* Account the overhead. */ void -vec_prefix::register_overhead (void *ptr, size_t size, size_t elements - MEM_STAT_DECL) +vec_prefix::register_overhead (void *ptr, size_t elements, + size_t element_size MEM_STAT_DECL) { vec_mem_desc.register_descriptor (ptr, VEC_ORIGIN, false FINAL_PASS_MEM_STAT); - vec_usage *usage = vec_mem_desc.register_instance_overhead (size, ptr); + vec_usage *usage + = vec_mem_desc.register_instance_overhead (elements * element_size, ptr); + usage->m_element_size = element_size; usage->m_items += elements; if (usage->m_items_peak < usage->m_items) usage->m_items_peak = usage->m_items; @@ -132,16 +142,17 @@ /* Notice that the memory allocated for the vector has been freed. */ void -vec_prefix::release_overhead (void *ptr, size_t size, bool in_dtor - MEM_STAT_DECL) +vec_prefix::release_overhead (void *ptr, size_t size, size_t elements, + bool in_dtor MEM_STAT_DECL) { if (!vec_mem_desc.contains_descriptor_for_instance (ptr)) vec_mem_desc.register_descriptor (ptr, VEC_ORIGIN, false FINAL_PASS_MEM_STAT); - vec_mem_desc.release_instance_overhead (ptr, size, in_dtor); + vec_usage *usage = vec_mem_desc.release_instance_overhead (ptr, size, + in_dtor); + usage->m_items -= elements; } - /* Calculate the number of slots to reserve a vector, making sure that it is of at least DESIRED size by growing ALLOC exponentially. */ @@ -181,21 +192,23 @@ ATTRIBUTE_NORETURN ATTRIBUTE_COLD static void qsort_chk_error (const void *p1, const void *p2, const void *p3, - int (*cmp) (const void *, const void *)) + sort_r_cmp_fn *cmp, void *data) { if (!p3) { - int r1 = cmp (p1, p2), r2 = cmp (p2, p1); - error ("qsort comparator not anti-commutative: %d, %d", r1, r2); + int r1 = cmp (p1, p2, data), r2 = cmp (p2, p1, data); + error ("qsort comparator not anti-symmetric: %d, %d", r1, r2); } else if (p1 == p2) { - int r = cmp (p1, p3); + int r = cmp (p1, p3, data); error ("qsort comparator non-negative on sorted output: %d", r); } else { - int r1 = cmp (p1, p2), r2 = cmp (p2, p3), r3 = cmp (p1, p3); + int r1 = cmp (p1, p2, data); + int r2 = cmp (p2, p3, data); + int r3 = cmp (p1, p3, data); error ("qsort comparator not transitive: %d, %d, %d", r1, r2, r3); } internal_error ("qsort checking failed"); @@ -204,8 +217,7 @@ /* Verify anti-symmetry and transitivity for comparator CMP on sorted array of N SIZE-sized elements pointed to by BASE. */ void -qsort_chk (void *base, size_t n, size_t size, - int (*cmp)(const void *, const void *)) +qsort_chk (void *base, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data) { #if 0 #define LIM(n) (n) @@ -214,9 +226,9 @@ #define LIM(n) ((n) <= 16 ? (n) : 12 + floor_log2 (n)) #endif #define ELT(i) ((const char *) base + (i) * size) -#define CMP(i, j) cmp (ELT (i), ELT (j)) -#define ERR2(i, j) qsort_chk_error (ELT (i), ELT (j), NULL, cmp) -#define ERR3(i, j, k) qsort_chk_error (ELT (i), ELT (j), ELT (k), cmp) +#define CMP(i, j) cmp (ELT (i), ELT (j), data) +#define ERR2(i, j) qsort_chk_error (ELT (i), ELT (j), NULL, cmp, data) +#define ERR3(i, j, k) qsort_chk_error (ELT (i), ELT (j), ELT (k), cmp, data) size_t i1, i2, i, j; /* This outer loop iterates over maximum spans [i1, i2) such that elements within each span compare equal to each other. */ @@ -504,6 +516,32 @@ } } +/* A test class that increments a counter every time its dtor is called. */ + +class count_dtor +{ + public: + count_dtor (int *counter) : m_counter (counter) {} + ~count_dtor () { (*m_counter)++; } + + private: + int *m_counter; +}; + +/* Verify that auto_delete_vec deletes the elements within it. */ + +static void +test_auto_delete_vec () +{ + int dtor_count = 0; + { + auto_delete_vec <count_dtor> v; + v.safe_push (new count_dtor (&dtor_count)); + v.safe_push (new count_dtor (&dtor_count)); + } + ASSERT_EQ (dtor_count, 2); +} + /* Run all of the selftests within this file. */ void @@ -521,6 +559,7 @@ test_block_remove (); test_qsort (); test_reverse (); + test_auto_delete_vec (); } } // namespace selftest