Speed up buffer variable allocation sanity check

This makes defining HB_NDEBUG much less relevant, to the
point of irrelevance.  Sorry about all the fuss in previous
release!
This commit is contained in:
Behdad Esfahbod 2016-02-25 14:40:09 +09:00
parent 91dd115652
commit 0c7fb7419c
2 changed files with 43 additions and 94 deletions

View File

@ -124,18 +124,46 @@ struct hb_buffer_t {
void *message_data;
hb_destroy_func_t message_destroy;
#ifndef HB_NDEBUG
/* Internal debugging. */
/* These reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
uint8_t allocated_var_bytes[8];
const char *allocated_var_owner[8];
HB_INTERNAL void allocate_var (unsigned int byte_i, unsigned int count, const char *owner);
HB_INTERNAL void deallocate_var (unsigned int byte_i, unsigned int count, const char *owner);
HB_INTERNAL void assert_var (unsigned int byte_i, unsigned int count, const char *owner);
HB_INTERNAL void deallocate_var_all (void);
#else
inline void deallocate_var_all (void) {}
/* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
#ifndef HB_NDEBUG
uint8_t allocated_var_bits;
#endif
inline void allocate_var (unsigned int start, unsigned int count)
{
#ifndef HB_NDEBUG
unsigned int end = start + count;
assert (end <= 8);
unsigned int bits = (1<<end) - (1<<start);
assert (0 == (allocated_var_bits & bits));
allocated_var_bits |= bits;
#endif
}
inline void deallocate_var (unsigned int start, unsigned int count)
{
#ifndef HB_NDEBUG
unsigned int end = start + count;
assert (end <= 8);
unsigned int bits = (1<<end) - (1<<start);
assert (bits == (allocated_var_bits & bits));
allocated_var_bits &= ~bits;
#endif
}
inline void assert_var (unsigned int start, unsigned int count)
{
#ifndef HB_NDEBUG
unsigned int end = start + count;
assert (end <= 8);
unsigned int bits = (1<<end) - (1<<start);
assert (bits == (allocated_var_bits & bits));
#endif
}
inline void deallocate_var_all (void)
{
#ifndef HB_NDEBUG
allocated_var_bits = 0;
#endif
}
/* Methods */
@ -257,21 +285,12 @@ struct hb_buffer_t {
};
#define HB_BUFFER_XALLOCATE_VAR(b, func, var, owner) \
#define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
sizeof (b->info[0].var), owner)
#ifndef HB_NDEBUG
#define HB_BUFFER_ALLOCATE_VAR(b, var) \
HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var (), #var)
#define HB_BUFFER_DEALLOCATE_VAR(b, var) \
HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var (), #var)
#define HB_BUFFER_ASSERT_VAR(b, var) \
HB_BUFFER_XALLOCATE_VAR (b, assert_var, var (), #var)
#else
#define HB_BUFFER_ALLOCATE_VAR(b, var)
#define HB_BUFFER_DEALLOCATE_VAR(b, var)
#define HB_BUFFER_ASSERT_VAR(b, var)
#endif
sizeof (b->info[0].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_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
#endif /* HB_BUFFER_PRIVATE_HH */

View File

@ -661,76 +661,6 @@ hb_buffer_t::guess_segment_properties (void)
}
#ifndef HB_NDEBUG
static inline void
dump_var_allocation (const hb_buffer_t *buffer)
{
char buf[80];
for (unsigned int i = 0; i < 8; i++)
buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
buf[8] = '\0';
DEBUG_MSG (BUFFER, buffer,
"Current var allocation: %s",
buf);
}
void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
{
assert (byte_i < 8 && byte_i + count <= 8);
if (DEBUG_ENABLED (BUFFER))
dump_var_allocation (this);
DEBUG_MSG (BUFFER, this,
"Allocating var bytes %d..%d for %s",
byte_i, byte_i + count - 1, owner);
for (unsigned int i = byte_i; i < byte_i + count; i++) {
assert (!allocated_var_bytes[i]);
allocated_var_bytes[i]++;
allocated_var_owner[i] = owner;
}
}
void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
{
if (DEBUG_ENABLED (BUFFER))
dump_var_allocation (this);
DEBUG_MSG (BUFFER, this,
"Deallocating var bytes %d..%d for %s",
byte_i, byte_i + count - 1, owner);
assert (byte_i < 8 && byte_i + count <= 8);
for (unsigned int i = byte_i; i < byte_i + count; i++) {
assert (allocated_var_bytes[i]);
assert (0 == strcmp (allocated_var_owner[i], owner));
allocated_var_bytes[i]--;
}
}
void hb_buffer_t::assert_var (unsigned int byte_i, unsigned int count, const char *owner)
{
if (DEBUG_ENABLED (BUFFER))
dump_var_allocation (this);
DEBUG_MSG (BUFFER, this,
"Asserting var bytes %d..%d for %s",
byte_i, byte_i + count - 1, owner);
assert (byte_i < 8 && byte_i + count <= 8);
for (unsigned int i = byte_i; i < byte_i + count; i++) {
assert (allocated_var_bytes[i]);
assert (0 == strcmp (allocated_var_owner[i], owner));
}
}
void hb_buffer_t::deallocate_var_all (void)
{
memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
}
#endif /* HB_NDEBUG */
/* Public API */
/**