Bug 561740 – Use stack allocation for thai_engine_break
authorChris Wilson <chris@chris-wilson.co.uk>
Sat, 22 Nov 2008 00:10:07 +0000 (00:10 +0000)
committerChris Wilson <cpwilson@src.gnome.org>
Sat, 22 Nov 2008 00:10:07 +0000 (00:10 +0000)
2008-11-22  Chris Wilson  <chris@chris-wilson.co.uk>

Bug 561740 – Use stack allocation for thai_engine_break

* modules/thai/thai-lang.c (utf8_to_tis), (thai_engine_break):
Small tweak to allocate small, temporary arrays on the stack.

svn path=/trunk/; revision=2740

ChangeLog
modules/thai/thai-lang.c

index b936000..e57c959 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-11-22  Chris Wilson  <chris@chris-wilson.co.uk>
+
+       Bug 561740 – Use stack allocation for thai_engine_break
+
+       * modules/thai/thai-lang.c (utf8_to_tis), (thai_engine_break):
+       Small tweak to allocate small, temporary arrays on the stack.
+
 2008-11-21  Behdad Esfahbod  <behdad@gnome.org>
 
        Bug 561779 – incorrect calculations in pango_matrix_concat
index 83d1642..b6494b3 100644 (file)
@@ -45,25 +45,22 @@ static PangoEngineInfo script_engines[] = {
   }
 };
 
+/*
+ * tis_text is assumed to be large enough to hold the converted string,
+ * i.e. it must be at least g_utf8_strlen(text, len)+1 bytes.
+ */
 static thchar_t *
-utf8_to_tis (const char *text, int len)
+utf8_to_tis (const char *text, int len, thchar_t *tis_text, int *tis_cnt)
 {
-  thchar_t   *tis_text;
   thchar_t   *tis_p;
   const char *text_p;
 
-  if (len < 0)
-    len = strlen (text);
-
-  tis_text = g_new (thchar_t, g_utf8_strlen (text, len) + 1);
-  if (!tis_text)
-    return NULL;
-
   tis_p = tis_text;
   for (text_p = text; text_p < text + len; text_p = g_utf8_next_char (text_p))
     *tis_p++ = th_uni2tis (g_utf8_get_char (text_p));
-  *tis_p = '\0';
+  *tis_p++ = '\0';
 
+  *tis_cnt = tis_p - tis_text;
   return tis_text;
 }
 
@@ -75,28 +72,40 @@ thai_engine_break (PangoEngineLang *engine,
                   PangoLogAttr    *attrs,
                   int              attrs_len)
 {
+  thchar_t tis_stack[512];
+  int brk_stack[512];
   thchar_t *tis_text;
+  int *brk_pnts;
+  int cnt;
 
-  tis_text = utf8_to_tis (text, len);
-  if (tis_text)
+  if (len < 0)
+    len = strlen (text);
+  cnt = g_utf8_strlen (text, len) + 1;
+
+  tis_text = tis_stack;
+  if (cnt > G_N_ELEMENTS (tis_stack))
+    tis_text = g_new (thchar_t, cnt);
+
+  utf8_to_tis (text, len, tis_text, &cnt);
+
+  brk_pnts = brk_stack;
+  if (cnt > G_N_ELEMENTS (brk_stack))
+    brk_pnts = g_new (int, cnt);
+
+  /* find line break positions */
+  len = th_brk (tis_text, brk_pnts, len);
+  for (cnt = 0; cnt < len; cnt++)
     {
-      int brk_len = strlen ((const char*)tis_text) + 1;
-      int *brk_pnts = g_new (int, brk_len);
-      int brk_n;
-      int i;
-
-      /* find line break positions */
-      brk_n = th_brk (tis_text, brk_pnts, brk_len);
-      for (i = 0; i < brk_n; i++)
-       {
-         attrs[brk_pnts[i]].is_line_break = TRUE;
-         attrs[brk_pnts[i]].is_word_start = TRUE;
-         attrs[brk_pnts[i]].is_word_end = TRUE;
-       }
+      attrs[brk_pnts[cnt]].is_line_break = TRUE;
+      attrs[brk_pnts[cnt]].is_word_start = TRUE;
+      attrs[brk_pnts[cnt]].is_word_end = TRUE;
+    }
 
+  if (brk_pnts != brk_stack)
       g_free (brk_pnts);
+
+  if (tis_text != tis_stack)
       g_free (tis_text);
-    }
 }
 
 static void