+
+#ifdef GATHER_STATISTICS
+
+/* Store information about each particular vector. */
+struct vec_descriptor
+{
+ const char *function;
+ const char *file;
+ int line;
+ size_t allocated;
+ size_t times;
+ size_t peak;
+};
+
+
+/* Hashtable mapping vec addresses to descriptors. */
+static htab_t vec_desc_hash;
+
+/* Hashtable helpers. */
+static hashval_t
+hash_descriptor (const void *p)
+{
+ const struct vec_descriptor *const d =
+ (const struct vec_descriptor *) p;
+ return htab_hash_pointer (d->file) + d->line;
+}
+static int
+eq_descriptor (const void *p1, const void *p2)
+{
+ const struct vec_descriptor *const d = (const struct vec_descriptor *) p1;
+ const struct vec_descriptor *const l = (const struct vec_descriptor *) p2;
+ return d->file == l->file && d->function == l->function && d->line == l->line;
+}
+
+/* Hashtable converting address of allocated field to loc descriptor. */
+static htab_t ptr_hash;
+struct ptr_hash_entry
+{
+ void *ptr;
+ struct vec_descriptor *loc;
+ size_t allocated;
+};
+
+/* Hash table helpers functions. */
+static hashval_t
+hash_ptr (const void *p)
+{
+ const struct ptr_hash_entry *const d = (const struct ptr_hash_entry *) p;
+
+ return htab_hash_pointer (d->ptr);
+}
+
+static int
+eq_ptr (const void *p1, const void *p2)
+{
+ const struct ptr_hash_entry *const p = (const struct ptr_hash_entry *) p1;
+
+ return (p->ptr == p2);
+}
+
+/* Return descriptor for given call site, create new one if needed. */
+static struct vec_descriptor *
+vec_descriptor (const char *name, int line, const char *function)
+{
+ struct vec_descriptor loc;
+ struct vec_descriptor **slot;
+
+ loc.file = name;
+ loc.line = line;
+ loc.function = function;
+ if (!vec_desc_hash)
+ vec_desc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
+
+ slot = (struct vec_descriptor **) htab_find_slot (vec_desc_hash, &loc,
+ INSERT);
+ if (*slot)
+ return *slot;
+ *slot = XCNEW (struct vec_descriptor);
+ (*slot)->file = name;
+ (*slot)->line = line;
+ (*slot)->function = function;
+ (*slot)->allocated = 0;
+ (*slot)->peak = 0;
+ return *slot;
+}
+
+/* Account the overhead. */
+static void
+register_overhead (struct vec_prefix *ptr, size_t size,
+ const char *name, int line, const char *function)
+{
+ struct vec_descriptor *loc = vec_descriptor (name, line, function);
+ struct ptr_hash_entry *p = XNEW (struct ptr_hash_entry);
+ PTR *slot;
+
+ p->ptr = ptr;
+ p->loc = loc;
+ p->allocated = size;
+ if (!ptr_hash)
+ ptr_hash = htab_create (10, hash_ptr, eq_ptr, NULL);
+ slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr), INSERT);
+ gcc_assert (!*slot);
+ *slot = p;
+
+ loc->allocated += size;
+ if (loc->peak < loc->allocated)
+ loc->peak += loc->allocated;
+ loc->times++;
+}
+
+/* Notice that the pointer has been freed. */
+static void
+free_overhead (struct vec_prefix *ptr)
+{
+ PTR *slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr),
+ NO_INSERT);
+ struct ptr_hash_entry *p = (struct ptr_hash_entry *) *slot;
+ p->loc->allocated -= p->allocated;
+ htab_clear_slot (ptr_hash, slot);
+ free (p);
+}
+
+void
+vec_heap_free (void *ptr)
+{
+ free_overhead ((struct vec_prefix *)ptr);
+ free (ptr);
+}
+#endif
+
+/* Calculate the new ALLOC value, making sure that RESERVE slots are
+ free. If EXACT grow exactly, otherwise grow exponentially. */
+
+static inline unsigned
+calculate_allocation (const struct vec_prefix *pfx, int reserve, bool exact)
+{
+ unsigned alloc = 0;
+ unsigned num = 0;
+
+ gcc_assert (reserve >= 0);
+
+ if (pfx)
+ {
+ alloc = pfx->alloc;
+ num = pfx->num;
+ }
+ else if (!reserve)
+ /* If there's no prefix, and we've not requested anything, then we
+ will create a NULL vector. */
+ return 0;
+
+ /* We must have run out of room. */
+ gcc_assert (alloc - num < (unsigned) reserve);
+
+ if (exact)
+ /* Exact size. */
+ alloc = num + reserve;
+ else
+ {
+ /* Exponential growth. */
+ if (!alloc)
+ alloc = 4;
+ else if (alloc < 16)
+ /* Double when small. */
+ alloc = alloc * 2;
+ else
+ /* Grow slower when large. */
+ alloc = (alloc * 3 / 2);
+
+ /* If this is still too small, set it to the right size. */
+ if (alloc < num + reserve)
+ alloc = num + reserve;
+ }
+ return alloc;
+}
+
+/* Ensure there are at least RESERVE free slots in VEC. If EXACT grow
+ exactly, else grow exponentially. As a special case, if VEC is
+ NULL and RESERVE is 0, no vector will be created. The vector's
+ trailing array is at VEC_OFFSET offset and consists of ELT_SIZE
+ sized elements. */
+
+static void *
+vec_gc_o_reserve_1 (void *vec, int reserve, size_t vec_offset, size_t elt_size,
+ bool exact MEM_STAT_DECL)
+{
+ struct vec_prefix *pfx = (struct vec_prefix *) vec;
+ unsigned alloc = calculate_allocation (pfx, reserve, exact);
+
+ if (!alloc)
+ {
+ if (pfx)
+ ggc_free (pfx);
+ return NULL;
+ }
+
+ vec = ggc_realloc_stat (vec, vec_offset + alloc * elt_size PASS_MEM_STAT);
+ ((struct vec_prefix *)vec)->alloc = alloc;
+ if (!pfx)
+ ((struct vec_prefix *)vec)->num = 0;
+
+ return vec;
+}
+
+/* Ensure there are at least RESERVE free slots in VEC, growing
+ exponentially. If RESERVE < 0 grow exactly, else grow
+ exponentially. As a special case, if VEC is NULL, and RESERVE is
+ 0, no vector will be created. */
+
+void *
+vec_gc_p_reserve (void *vec, int reserve MEM_STAT_DECL)
+{
+ return vec_gc_o_reserve_1 (vec, reserve,
+ offsetof (struct vec_prefix, vec),
+ sizeof (void *), false
+ PASS_MEM_STAT);
+}
+
+/* Ensure there are at least RESERVE free slots in VEC, growing
+ exactly. If RESERVE < 0 grow exactly, else grow exponentially. As
+ a special case, if VEC is NULL, and RESERVE is 0, no vector will be
+ created. */
+
+void *
+vec_gc_p_reserve_exact (void *vec, int reserve MEM_STAT_DECL)
+{
+ return vec_gc_o_reserve_1 (vec, reserve,
+ offsetof (struct vec_prefix, vec),
+ sizeof (void *), true
+ PASS_MEM_STAT);
+}
+
+/* As for vec_gc_p_reserve, but for object vectors. The vector's
+ trailing array is at VEC_OFFSET offset and consists of ELT_SIZE
+ sized elements. */