From 16f175cb2e081e605fe7f9cd01bbe8c24380278a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 12 Nov 2013 17:22:49 -0500 Subject: [PATCH] Fix scratch-buffer alignment warnings --- src/hb-buffer-private.hh | 2 +- src/hb-buffer.cc | 9 +++++---- src/hb-coretext.cc | 17 ++++++++++------- src/hb-graphite2.cc | 28 +++++++++++++++++----------- src/hb-private.hh | 3 +++ src/hb-uniscribe.cc | 29 +++++++++++++++++------------ 6 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/hb-buffer-private.hh b/src/hb-buffer-private.hh index 703239035..4647b9b4c 100644 --- a/src/hb-buffer-private.hh +++ b/src/hb-buffer-private.hh @@ -186,7 +186,7 @@ struct hb_buffer_t { HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out); HB_INTERNAL bool shift_forward (unsigned int count); - HB_INTERNAL void *get_scratch_buffer (unsigned int *size); + HB_INTERNAL int *get_scratch_buffer (unsigned int *int_size); inline void clear_context (unsigned int side) { context_len[side] = 0; } }; diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 867438a90..c5a74c30f 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -152,8 +152,8 @@ hb_buffer_t::shift_forward (unsigned int count) return true; } -void * -hb_buffer_t::get_scratch_buffer (unsigned int *size) +int * +hb_buffer_t::get_scratch_buffer (unsigned int *int_size) { have_output = false; have_positions = false; @@ -161,8 +161,9 @@ hb_buffer_t::get_scratch_buffer (unsigned int *size) out_len = 0; out_info = info; - *size = allocated * sizeof (pos[0]); - return pos; + ASSERT_STATIC (sizeof (pos[0]) % sizeof (int) == 0); + *int_size = allocated * (sizeof (pos[0]) / sizeof (int)); + return (int *) pos; } diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc index f78097356..82785c440 100644 --- a/src/hb-coretext.cc +++ b/src/hb-coretext.cc @@ -647,17 +647,20 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, buffer->ensure (buffer->len + num_glyphs); - /* Testing indicates that CTRunGetGlyphsPtr (almost?) always succeeds, - * and so copying data to our own buffer with CTRunGetGlyphs will be - * extremely rare. */ - unsigned int scratch_size; - char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); + int *scratch = buffer->get_scratch_buffer (&scratch_size); #define ALLOCATE_ARRAY(Type, name, len) \ Type *name = (Type *) scratch; \ - scratch += (len) * sizeof ((name)[0]); \ - scratch_size -= (len) * sizeof ((name)[0]); + { \ + unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ + assert (_consumed <= scratch_size); \ + scratch += _consumed; \ + scratch_size -= _consumed; \ + } + + /* Testing indicates that CTRunGetGlyphsPtr, etc (almost?) always + * succeed, and so copying data to our own buffer will be rare. */ const CGGlyph* glyphs = CTRunGetGlyphsPtr (run); if (!glyphs) { diff --git a/src/hb-graphite2.cc b/src/hb-graphite2.cc index 6fe16ca93..d1cd1dc52 100644 --- a/src/hb-graphite2.cc +++ b/src/hb-graphite2.cc @@ -243,14 +243,9 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan, float curradvx = 0., curradvy = 0.; unsigned int scratch_size; - char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); + int *scratch = buffer->get_scratch_buffer (&scratch_size); -#define ALLOCATE_ARRAY(Type, name, len) \ - Type *name = (Type *) scratch; \ - scratch += (len) * sizeof ((name)[0]); \ - scratch_size -= (len) * sizeof ((name)[0]); - - ALLOCATE_ARRAY (uint32_t, chars, buffer->len); + uint32_t *chars = (uint32_t *) scratch; for (unsigned int i = 0; i < buffer->len; ++i) chars[i] = buffer->info[i].codepoint; @@ -276,9 +271,9 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan, return false; } - scratch = (char *) buffer->get_scratch_buffer (&scratch_size); - while ((sizeof (hb_graphite2_cluster_t) * buffer->len + - sizeof (hb_codepoint_t) * glyph_count) > scratch_size) + scratch = buffer->get_scratch_buffer (&scratch_size); + while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) + + DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size) { buffer->ensure (buffer->allocated * 2); if (unlikely (buffer->in_error)) { @@ -286,12 +281,23 @@ _hb_graphite2_shape (hb_shape_plan_t *shape_plan, gr_seg_destroy (seg); return false; } - scratch = (char *) buffer->get_scratch_buffer (&scratch_size); + scratch = buffer->get_scratch_buffer (&scratch_size); + } + +#define ALLOCATE_ARRAY(Type, name, len) \ + Type *name = (Type *) scratch; \ + { \ + unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ + assert (_consumed <= scratch_size); \ + scratch += _consumed; \ + scratch_size -= _consumed; \ } ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len); ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count); +#undef ALLOCATE_ARRAY + memset (clusters, 0, sizeof (clusters[0]) * buffer->len); hb_codepoint_t *pg = gids; diff --git a/src/hb-private.hh b/src/hb-private.hh index 4152e2755..4b72260ed 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -79,6 +79,9 @@ static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } template static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } +static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) +{ return (a + (b - 1)) / b; } + #undef ARRAY_LENGTH template diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc index 65a63f3be..4a93db840 100644 --- a/src/hb-uniscribe.cc +++ b/src/hb-uniscribe.cc @@ -729,15 +729,10 @@ _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, retry: unsigned int scratch_size; - char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); + int *scratch = buffer->get_scratch_buffer (&scratch_size); /* Allocate char buffers; they all fit */ -#define ALLOCATE_ARRAY(Type, name, len) \ - Type *name = (Type *) scratch; \ - scratch += (len) * sizeof ((name)[0]); \ - scratch_size -= (len) * sizeof ((name)[0]); - #define utf16_index() var1.u32 WCHAR *pchars = (WCHAR *) scratch; @@ -756,6 +751,15 @@ retry: } } +#define ALLOCATE_ARRAY(Type, name, len) \ + Type *name = (Type *) scratch; \ + { \ + unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ + assert (_consumed <= scratch_size); \ + scratch += _consumed; \ + scratch_size -= _consumed; \ + } + ALLOCATE_ARRAY (WCHAR, wchars, chars_len); ALLOCATE_ARRAY (WORD, log_clusters, chars_len); ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); @@ -774,12 +778,13 @@ retry: } } - /* On Windows, we don't care about alignment...*/ - unsigned int glyphs_size = scratch_size / (sizeof (WORD) + - sizeof (SCRIPT_GLYPHPROP) + - sizeof (int) + - sizeof (GOFFSET) + - sizeof (uint32_t)); + /* All the following types are sized in multiples of sizeof(int). */ + unsigned int glyphs_size = scratch_size / ((sizeof (WORD) + + sizeof (SCRIPT_GLYPHPROP) + + sizeof (int) + + sizeof (GOFFSET) + + sizeof (uint32_t)) + / sizeof (int)); ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);