[vector] Add internal API for exact-size allocation

Use it from a couple of places.
This commit is contained in:
Behdad Esfahbod 2022-12-31 12:27:13 -07:00
parent a0b46f3f6b
commit 2eacc37e08
6 changed files with 12 additions and 12 deletions

View File

@ -184,7 +184,7 @@ struct SimpleGlyph
if (unlikely (!bytes.check_range (&endPtsOfContours[num_contours]))) return false;
unsigned int num_points = endPtsOfContours[num_contours - 1] + 1;
points_.alloc (num_points + 4); // Allocate for phantom points, to avoid a possible copy
points_.alloc (num_points + 4, true); // Allocate for phantom points, to avoid a possible copy
if (!points_.resize (num_points)) return false;
if (phantom_only) return true;
@ -272,9 +272,9 @@ struct SimpleGlyph
unsigned num_points = all_points.length - 4;
hb_vector_t<uint8_t> flags, x_coords, y_coords;
if (unlikely (!flags.alloc (num_points))) return false;
if (unlikely (!x_coords.alloc (2*num_points))) return false;
if (unlikely (!y_coords.alloc (2*num_points))) return false;
if (unlikely (!flags.alloc (num_points, true))) return false;
if (unlikely (!x_coords.alloc (2*num_points, true))) return false;
if (unlikely (!y_coords.alloc (2*num_points, true))) return false;
uint8_t lastflag = 255, repeat = 0;
int prev_x = 0, prev_y = 0;

View File

@ -429,7 +429,7 @@ glyf::_create_font_for_instancing (const hb_subset_plan_t *plan) const
if (unlikely (font == hb_font_get_empty ())) return nullptr;
hb_vector_t<hb_variation_t> vars;
if (unlikely (!vars.alloc (plan->user_axes_location->get_population ())))
if (unlikely (!vars.alloc (plan->user_axes_location->get_population (), true)))
return nullptr;
for (auto _ : *plan->user_axes_location)

View File

@ -4020,7 +4020,7 @@ struct hb_ot_layout_lookup_accelerator_t
void init (const TLookup &lookup)
{
subtables.init ();
subtables.alloc (lookup.get_subtable_count ());
subtables.alloc (lookup.get_subtable_count (), true);
hb_accelerate_subtables_context_t c_accelerate_subtables (subtables);
lookup.dispatch (&c_accelerate_subtables);

View File

@ -115,7 +115,7 @@ bool _promote_extensions_if_needed (graph::gsubgpos_graph_context_t& ext_context
if (!ext_context.lookups) return true;
hb_vector_t<lookup_size_t> lookup_sizes;
lookup_sizes.alloc (ext_context.lookups.get_population ());
lookup_sizes.alloc (ext_context.lookups.get_population (), true);
for (unsigned lookup_index : ext_context.lookups.keys ())
{

View File

@ -81,11 +81,11 @@ struct hb_serialize_context_t
head = o.head;
tail = o.tail;
next = nullptr;
real_links.alloc (o.num_real_links);
real_links.alloc (o.num_real_links, true);
for (unsigned i = 0 ; i < o.num_real_links; i++)
real_links.push (o.real_links[i]);
virtual_links.alloc (o.num_virtual_links);
virtual_links.alloc (o.num_virtual_links, true);
for (unsigned i = 0; i < o.num_virtual_links; i++)
virtual_links.push (o.virtual_links[i]);
}

View File

@ -337,7 +337,7 @@ struct hb_vector_t
}
/* Allocate for size but don't adjust length. */
bool alloc (unsigned int size)
bool alloc (unsigned int size, bool exact=false)
{
if (unlikely (in_error ()))
return false;
@ -347,8 +347,8 @@ struct hb_vector_t
/* Reallocate */
unsigned int new_allocated = allocated;
while (size >= new_allocated)
unsigned int new_allocated = exact ? size : allocated;
while (size > new_allocated)
new_allocated += (new_allocated >> 1) + 8;
Type *new_array = nullptr;