[glib/wip/3v1n0/regex-pcre2-flags-fixes: 9/11] gregex: Avoid re-allocating if we have no size change
- From: Marco Trevisan <marcotrevi src gnome org>
- To: commits-list gnome org
- Cc: 
- Subject: [glib/wip/3v1n0/regex-pcre2-flags-fixes: 9/11] gregex: Avoid re-allocating if we have no size change
- Date: Tue,  6 Sep 2022 20:17:53 +0000 (UTC)
commit 65fb37e8c6de35823babc863f4e638df6b41f680
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Sep 6 18:56:39 2022 +0200
    gregex: Avoid re-allocating if we have no size change
    
    This is handled by the syscall underneath, but we can just avoid a call
    cheaply.
 glib/gregex.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/glib/gregex.c b/glib/gregex.c
index cbf96812e0..a4b9b2201c 100644
--- a/glib/gregex.c
+++ b/glib/gregex.c
@@ -842,6 +842,7 @@ recalc_match_offsets (GMatchInfo *match_info,
                       GError     **error)
 {
   PCRE2_SIZE *ovector;
+  uint32_t pre_n_offset;
   uint32_t i;
 
   if (pcre2_get_ovector_count (match_info->match_data) > G_MAXUINT32 / 2)
@@ -852,11 +853,17 @@ recalc_match_offsets (GMatchInfo *match_info,
       return FALSE;
     }
 
+  pre_n_offset = match_info->n_offsets;
   match_info->n_offsets = pcre2_get_ovector_count (match_info->match_data) * 2;
   ovector = pcre2_get_ovector_pointer (match_info->match_data);
-  match_info->offsets = g_realloc_n (match_info->offsets,
-                                     match_info->n_offsets,
-                                     sizeof (gint));
+
+  if (match_info->n_offsets != pre_n_offset)
+    {
+      match_info->offsets = g_realloc_n (match_info->offsets,
+                                         match_info->n_offsets,
+                                         sizeof (gint));
+    }
+
   for (i = 0; i < match_info->n_offsets; i++)
     {
       match_info->offsets[i] = (int) ovector[i];
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]