[buffer] Add try_allocate for buffer variables

This commit is contained in:
Behdad Esfahbod 2022-06-04 06:56:35 -06:00
parent b5f621b08d
commit 104dc85a22
1 changed files with 14 additions and 13 deletions

View File

@ -132,9 +132,7 @@ struct hb_buffer_t
* Managed by enter / leave * Managed by enter / leave
*/ */
#ifndef HB_NDEBUG
uint8_t allocated_var_bits; uint8_t allocated_var_bits;
#endif
uint8_t serial; uint8_t serial;
hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */ hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
unsigned int max_len; /* Maximum allowed len. */ unsigned int max_len; /* Maximum allowed len. */
@ -163,38 +161,40 @@ struct hb_buffer_t
void allocate_var (unsigned int start, unsigned int count) void allocate_var (unsigned int start, unsigned int count)
{ {
#ifndef HB_NDEBUG
unsigned int end = start + count; unsigned int end = start + count;
assert (end <= 8); assert (end <= 8);
unsigned int bits = (1u<<end) - (1u<<start); unsigned int bits = (1u<<end) - (1u<<start);
assert (0 == (allocated_var_bits & bits)); assert (0 == (allocated_var_bits & bits));
allocated_var_bits |= bits; allocated_var_bits |= bits;
#endif }
bool try_allocate_var (unsigned int start, unsigned int count)
{
unsigned int end = start + count;
assert (end <= 8);
unsigned int bits = (1u<<end) - (1u<<start);
if (allocated_var_bits & bits)
return false;
allocated_var_bits |= bits;
return true;
} }
void deallocate_var (unsigned int start, unsigned int count) void deallocate_var (unsigned int start, unsigned int count)
{ {
#ifndef HB_NDEBUG
unsigned int end = start + count; unsigned int end = start + count;
assert (end <= 8); assert (end <= 8);
unsigned int bits = (1u<<end) - (1u<<start); unsigned int bits = (1u<<end) - (1u<<start);
assert (bits == (allocated_var_bits & bits)); assert (bits == (allocated_var_bits & bits));
allocated_var_bits &= ~bits; allocated_var_bits &= ~bits;
#endif
} }
void assert_var (unsigned int start, unsigned int count) void assert_var (unsigned int start, unsigned int count)
{ {
#ifndef HB_NDEBUG
unsigned int end = start + count; unsigned int end = start + count;
assert (end <= 8); assert (end <= 8);
HB_UNUSED unsigned int bits = (1u<<end) - (1u<<start); HB_UNUSED unsigned int bits = (1u<<end) - (1u<<start);
assert (bits == (allocated_var_bits & bits)); assert (bits == (allocated_var_bits & bits));
#endif
} }
void deallocate_var_all () void deallocate_var_all ()
{ {
#ifndef HB_NDEBUG
allocated_var_bits = 0; allocated_var_bits = 0;
#endif
} }
hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; } hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
@ -621,9 +621,10 @@ DECLARE_NULL_INSTANCE (hb_buffer_t);
#define HB_BUFFER_XALLOCATE_VAR(b, func, var) \ #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \ b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
sizeof (b->info[0].var)) sizeof (b->info[0].var))
#define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ()) #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ())
#define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ()) #define HB_BUFFER_TRY_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, try_allocate_var, var ())
#define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ()) #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ())
#define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
#endif /* HB_BUFFER_HH */ #endif /* HB_BUFFER_HH */