+/* 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 = vec;
+ unsigned alloc = alloc = calculate_allocation (pfx, reserve, exact);
+
+ if (!alloc)
+ 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. */