Fix scratch-buffer alignment warnings

This commit is contained in:
Behdad Esfahbod 2013-11-12 17:22:49 -05:00
parent c7c4ccf8a1
commit 16f175cb2e
6 changed files with 53 additions and 35 deletions

View File

@ -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; }
};

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;

View File

@ -79,6 +79,9 @@ static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
template <typename Type>
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 <typename Type, unsigned int n>

View File

@ -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);