Merge pull request #5570 from harfbuzz/alloc-pool-improvements

[alloc-pool] Implement alignment
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-09-18 09:31:05 -06:00
commit fa7b7fd888
3 files changed
+33 -5

No files matched your search

+19 -2
View File
@@ -42,10 +42,14 @@ struct hb_alloc_pool_t
{
unsigned ChunkSize = 65536 - 2 * sizeof (void *);
void *alloc (size_t size)
void *alloc (size_t size, unsigned alignment = 2 * sizeof (void *))
{
if (unlikely (chunks.in_error ())) return nullptr;
assert (alignment > 0);
assert (alignment <= 2 * sizeof (void *));
assert ((alignment & (alignment - 1)) == 0); /* power of two */
if (size > (ChunkSize) / 4)
{
/* Big chunk, allocate separately. */
@@ -63,8 +67,11 @@ struct hb_alloc_pool_t
return ret;
}
unsigned pad = current_chunk.length & (alignment - 1);
if (pad) pad = alignment - pad;
// Small chunk, allocate from the last chunk.
if (current_chunk.length < size)
if (current_chunk.length < pad + size)
{
chunks.push ();
if (unlikely (chunks.in_error ())) return nullptr;
@@ -72,6 +79,8 @@ struct hb_alloc_pool_t
if (unlikely (!chunk.resize (ChunkSize))) return nullptr;
current_chunk = chunk;
}
else
current_chunk += pad;
assert (current_chunk.length >= size);
void *ret = current_chunk.arrayZ;
@@ -79,6 +88,14 @@ struct hb_alloc_pool_t
return ret;
}
void discard (void *p_, size_t size)
{
// Reclaim memory if we can.
char *p = (char *) p_;
if (current_chunk.arrayZ == p + size && current_chunk.backwards_length >= size)
current_chunk -= size;
}
private:
hb_vector_t<hb_vector_t<char>> chunks;
hb_array_t<char> current_chunk;
+2 -2
View File
@@ -523,7 +523,7 @@ struct tuple_delta_t
o->tupleIndex = flag;
unsigned total_header_len = 4 + (peak_count + interim_count) * (F2DOT14::static_size);
compiled_tuple_header.shrink (total_header_len, false);
compiled_tuple_header.shrink_back_to_pool (pool, total_header_len);
return true;
}
@@ -600,7 +600,7 @@ struct tuple_delta_t
if (j != rounded_deltas.length) return false;
encoded_len += compile_deltas (compiled_deltas.as_array ().sub_array (encoded_len), rounded_deltas);
}
compiled_deltas.shrink (encoded_len, false);
compiled_deltas.shrink_back_to_pool (pool, encoded_len);
return true;
}
+12 -1
View File
@@ -548,7 +548,7 @@ struct hb_vector_t
if (allocator)
{
assert (!length && !allocated);
arrayZ = (Type *) allocator->alloc (size * sizeof (Type));
arrayZ = (Type *) allocator->alloc (size * sizeof (Type), alignof (Type));
if (unlikely (!arrayZ))
{
set_error ();
@@ -574,6 +574,17 @@ struct hb_vector_t
return true;
}
template <typename allocator_t>
void shrink_back_to_pool (allocator_t *allocator, int size)
{
unsigned orig_length = length;
shrink (size, false);
if (allocator && !is_owned ())
allocator->discard (arrayZ + length, (orig_length - length) * sizeof (Type));
}
HB_ALWAYS_INLINE_VECTOR_ALLOCS
bool resize_full (int size_, bool initialize, bool exact)
{