use-gslice.diff
author Myhailo Danylenko <isbear@isbear.org.ua>
Fri, 09 Dec 2016 03:14:57 +0200
changeset 93 d59df8a9413d
parent 92 66f7e2aa040c
permissions -rw-r--r--
[cmake] Fix ESCDELAY detection

# HG changeset patch
# Parent c45500769f0c34734851c9079e57add63d083c25
# Parent  a18571d686cd4eddcb55e356a8b82c0ac3990f49
Use glib slices to allocate constant-size blocks

diff -r a18571d686cd mcabber/mcabber/caps.c
--- a/mcabber/mcabber/caps.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/caps.c	Fri Dec 09 01:48:31 2016 +0200
@@ -50,7 +50,7 @@
   g_hash_table_destroy(c->identities);
   g_hash_table_destroy(c->features);
   g_hash_table_destroy(c->forms);
-  g_free(c);
+  g_slice_free(caps, c);
 }
 
 void identity_destroy(gpointer data)
@@ -59,14 +59,14 @@
   g_free(i->category);
   g_free(i->type);
   g_free(i->name);
-  g_free(i);
+  g_slice_free(identity, i);
 }
 
 void form_destroy(gpointer data)
 {
   dataform *f = data;
   g_hash_table_destroy(f->fields);
-  g_free(f);
+  g_slice_free(dataform, f);
 }
 
 void field_destroy(gpointer data)
@@ -95,7 +95,7 @@
 {
   if (!hash)
     return;
-  caps *c = g_new0(caps, 1);
+  caps *c = g_slice_new(caps);
   c->features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
   c->identities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, identity_destroy);
   c->forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, form_destroy);
@@ -156,7 +156,7 @@
 
   c = g_hash_table_lookup(caps_cache, hash);
   if (c) {
-    identity *i = g_new0(identity, 1);
+    identity *i = g_slice_new(identity);
 
     i->category = g_strdup(category);
     i->name = g_strdup(name);
@@ -180,7 +180,7 @@
     return;
   c = g_hash_table_lookup(caps_cache, hash);
   if (c) {
-    dataform *d = g_new0(dataform, 1);
+    dataform *d = g_slice_new(dataform);
     char *f = g_strdup(formtype);
 
     d->fields = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, field_destroy);
diff -r a18571d686cd mcabber/mcabber/commands.c
--- a/mcabber/mcabber/commands.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/commands.c	Fri Dec 09 01:48:31 2016 +0200
@@ -113,7 +113,7 @@
       gpointer userdata = command->userdata;
       Commands = g_slist_delete_link(Commands, sl_cmd);
       compl_del_category_word(COMPL_CMD, command->name);
-      g_free(command);
+      g_slice_free(cmd, command);
       return userdata;
     }
   return NULL;
@@ -125,7 +125,7 @@
 gpointer cmd_add(const char *name, const char *help, guint flags_row1,
                  guint flags_row2, void (*f)(char*), gpointer userdata)
 {
-  cmd *n_cmd = g_new0(cmd, 1);
+  cmd *n_cmd = g_slice_new0(cmd);
   strncpy(n_cmd->name, name, 32-1);
   n_cmd->help = help;
   n_cmd->completion_flags[0] = flags_row1;
@@ -1571,7 +1571,7 @@
     return NULL;
   }
 
-  msgbuf = g_new0(char, HBB_BLOCKSIZE);
+  msgbuf = g_slice_alloc0(HBB_BLOCKSIZE);
   len = fread(msgbuf, 1, HBB_BLOCKSIZE-1, fd);
   fclose(fd);
 
@@ -1595,7 +1595,7 @@
   if (*p || (size_t)(p-msgbuf) != len) { // We're not at the End Of Line...
     scr_LogPrint(LPRINT_LOGNORM, "Message file contains "
                  "invalid characters (%s)", filename);
-    g_free(msgbuf);
+    g_slice_free1(HBB_BLOCKSIZE, msgbuf);
     return NULL;
   }
 
@@ -1609,7 +1609,7 @@
   // It could be empty, once the trailing newlines are gone
   if (p == msgbuf && *p == '\n') {
     scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename);
-    g_free(msgbuf);
+    g_slice_free1(HBB_BLOCKSIZE, msgbuf);
     return NULL;
   }
 
@@ -1618,7 +1618,7 @@
   if (!msgbuf_utf8 && msgbuf)
     scr_LogPrint(LPRINT_LOGNORM, "Message file charset conversion error (%s)",
                  filename);
-  g_free(msgbuf);
+  g_slice_free1(HBB_BLOCKSIZE, msgbuf);
   return msgbuf_utf8;
 }
 
@@ -1921,7 +1921,7 @@
   esub   = buddy_getsubscription(bud);
   on_srv = buddy_getonserverflag(bud);
 
-  buffer = g_new(char, 4096);
+  buffer = g_slice_alloc(4096);
 
   if (bjid) {
     GSList *resources, *p_res;
@@ -2020,7 +2020,7 @@
                  type == ROSTER_TYPE_GROUP ? "group" :
                  (type == ROSTER_TYPE_SPECIAL ? "special" : "unknown"));
   }
-  g_free(buffer);
+  g_slice_free1(4096, buffer);
 
   // Tell the user if this item has an annotation.
   if (type == ROSTER_TYPE_USER ||
@@ -2069,7 +2069,7 @@
 
   bjid = buddy_getjid(bud);
 
-  buffer = g_new(char, 4096);
+  buffer = g_slice_alloc(4096);
   strncpy(buffer, "Room members:", 127);
   scr_WriteIncomingMessage(bjid, buffer, 0, HBB_PREFIX_INFO, 0);
 
@@ -2129,7 +2129,7 @@
   scr_WriteIncomingMessage(bjid, buffer, 0, HBB_PREFIX_INFO, 0);
 
   g_slist_free(resources);
-  g_free(buffer);
+  g_slice_free1(4096, buffer);
 }
 
 static void move_group_member(gpointer bud, void *groupnamedata)
@@ -3163,7 +3163,7 @@
   affil = buddy_getaffil(bud, nick);
   realjid = buddy_getrjid(bud, nick);
 
-  buffer = g_new(char, 4096);
+  buffer = g_slice_alloc(4096);
 
   snprintf(buffer, 4095, "Whois [%s]", nick);
   scr_WriteIncomingMessage(bjid, buffer, 0, msg_flag, 0);
@@ -3193,7 +3193,7 @@
 
   scr_WriteIncomingMessage(bjid, "End of WHOIS", 0, msg_flag, 0);
 
-  g_free(buffer);
+  g_slice_free1(4096, buffer);
   g_free(nick);
   if (paramlst)
     free_arg_lst(paramlst);
diff -r a18571d686cd mcabber/mcabber/events.c
--- a/mcabber/mcabber/events.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/events.c	Fri Dec 09 01:48:31 2016 +0200
@@ -81,7 +81,7 @@
     return NULL;
   }
 
-  event = g_new(evs_t, 1);
+  event = g_slice_new(evs_t);
 
   event->id          = stridn;
   event->description = g_strdup(desc);
@@ -134,7 +134,7 @@
   evs_list = g_slist_remove(evs_list, event);
   g_free(event->id);
   g_free(event->description);
-  g_free(event);
+  g_slice_free(evs_t, event);
 
   return 0; // Ok, deleted
 }
@@ -213,7 +213,7 @@
     evs_list = g_slist_remove(evs_list, event);
     g_free(event->id);
     g_free(event->description);
-    g_free(event);
+    g_slice_free(evs_t, event);
   }
   g_slist_free(evs_list);
   evs_list = NULL;
diff -r a18571d686cd mcabber/mcabber/hbuf.c
--- a/mcabber/mcabber/hbuf.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/hbuf.c	Fri Dec 09 01:48:31 2016 +0200
@@ -27,7 +27,6 @@
 #include "utf8.h"
 #include "screen.h"
 
-
 /* This is a private structure type */
 
 typedef struct {
@@ -57,7 +56,7 @@
 {
   GList *curr_elt = first_hbuf_elt;
 
-  // Let's add non-persistent blocs if necessary
+  // Let's add non-persistent blocks if necessary
   // - If there are '\n' in the string
   // - If length > width (and width != 0)
   while (curr_elt) {
@@ -93,17 +92,21 @@
       end = hbuf_b_curr->ptr_end;
       hbuf_b_curr->ptr_end = br;
       // Create another block
-      hbuf_b_curr = g_new0(hbuf_block, 1);
+      hbuf_b_curr = g_slice_new(hbuf_block);
       // The block must be persistent after a CR
       if (cr) {
         hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end + 1; // == cr+1
         hbuf_b_curr->flags  = HBB_FLAG_PERSISTENT;
       } else {
         hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end; // == br
-        hbuf_b_curr->flags    = 0;
+        hbuf_b_curr->flags  = 0;
       }
-      hbuf_b_curr->ptr_end  = end;
+      hbuf_b_curr->ptr_end       = end;
       hbuf_b_curr->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
+      hbuf_b_curr->prefix.timestamp  = 0;
+      hbuf_b_curr->prefix.flags      = 0;
+      hbuf_b_curr->prefix.mucnicklen = 0;
+      hbuf_b_curr->prefix.xep184     = NULL;
       // This is OK because insert_before(NULL) == append():
       *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr);
     }
@@ -136,7 +139,7 @@
   textlen = strlen(text);
   hbb_blocksize = MAX(textlen+1, HBB_BLOCKSIZE);
 
-  hbuf_block_elt = g_new0(hbuf_block, 1);
+  hbuf_block_elt = g_slice_new(hbuf_block);
   hbuf_block_elt->prefix.timestamp  = timestamp;
   hbuf_block_elt->prefix.flags      = prefix_flags;
   hbuf_block_elt->prefix.mucnicklen = mucnicklen;
@@ -144,7 +147,7 @@
   if (!*p_hbuf) {
     hbuf_block_elt->ptr  = g_new(char, hbb_blocksize);
     if (!hbuf_block_elt->ptr) {
-      g_free(hbuf_block_elt);
+      g_slice_free(hbuf_block, hbuf_block_elt);
       return;
     }
     hbuf_block_elt->flags  = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
@@ -206,7 +209,7 @@
                 g_free(hbuf_b_elt->ptr);
               }
             }
-            g_free(hbuf_b_elt);
+            g_slice_free(hbuf_block, hbuf_b_elt);
             hbuf_head = *p_hbuf = g_list_delete_link(hbuf_head, hbuf_elt);
           }
           n--;
@@ -243,7 +246,7 @@
     if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) {
       g_free(hbuf_b_elt->ptr);
     }
-    g_free(hbuf_b_elt);
+    g_slice_free(hbuf_block, hbuf_b_elt);
   }
 
   g_list_free(first_elt);
@@ -273,7 +276,7 @@
     // Is next line not-persistent?
     if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) {
       hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end;
-      g_free(hbuf_b_next);
+      g_slice_free(hbuf_block, hbuf_b_next);
       curr_elt = g_list_delete_link(curr_elt, next_elt);
     } else
       curr_elt = next_elt;
@@ -307,8 +310,8 @@
 //  hbuf_get_lines(hbuf, n)
 // Returns an array of n hbb_line pointers
 // (The first line will be the line currently pointed by hbuf)
-// Note: The caller should free the array, the hbb_line pointers and the
-// text pointers after use.
+// Note: The caller should g_free the array, g_slice_free hbb_line pointers
+// and g_free text pointers after use.
 hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
 {
   unsigned int i;
@@ -347,7 +350,7 @@
 
       blk = (hbuf_block*)(hbuf->data);
       maxlen = blk->ptr_end - blk->ptr;
-      *array_elt = (hbb_line*)g_new(hbb_line, 1);
+      *array_elt = (hbb_line*)g_slice_new(hbb_line);
       (*array_elt)->timestamp  = blk->prefix.timestamp;
       (*array_elt)->flags      = blk->prefix.flags;
       (*array_elt)->mucnicklen = blk->prefix.mucnicklen;
diff -r a18571d686cd mcabber/mcabber/hooks.c
--- a/mcabber/mcabber/hooks.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/hooks.c	Fri Dec 09 01:48:31 2016 +0200
@@ -99,7 +99,7 @@
                      gint priority, gpointer userdata)
 {
   GSList **hqueue = NULL;
-  hook_list_data_t *h = g_new(hook_list_data_t, 1);
+  hook_list_data_t *h = g_slice_new(hook_list_data_t);
 
   h->handler  = handler;
   h->priority = priority;
@@ -148,7 +148,7 @@
   el = g_slist_find_custom(*hqueue, &hid,
                            (GCompareFunc)_hk_queue_search_cb);
   if (el) {
-    g_free(el->data);
+    g_slice_free(hook_list_data_t, el->data);
     *hqueue = g_slist_delete_link(*hqueue, el);
     // Remove hook hash table entry if the hook queue is empty
     if (!*hqueue)
diff -r a18571d686cd mcabber/mcabber/modules.c
--- a/mcabber/mcabber/modules.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/modules.c	Fri Dec 09 01:48:31 2016 +0200
@@ -174,7 +174,7 @@
   }
 
   { // Register module
-    loaded_module_t *module = g_new(loaded_module_t, 1);
+    loaded_module_t *module = g_slice_new(loaded_module_t);
 
     module->refcount     = 1;
     module->locked       = manual;
@@ -265,7 +265,7 @@
   // Output this here, as arg may point to module->name
   scr_LogPrint(LPRINT_LOGNORM, "Unloaded module %s.", module->name);
   g_free(module->name);
-  g_free(module);
+  g_slice_free(loaded_module_t, module);
 
   return NULL;
 }
diff -r a18571d686cd mcabber/mcabber/screen.c
--- a/mcabber/mcabber/screen.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/screen.c	Fri Dec 09 01:48:31 2016 +0200
@@ -1368,7 +1368,7 @@
         wattrset(win_entry->win, get_color(COLOR_GENERAL));
 
       g_free(line->text);
-      g_free(line);
+      g_slice_free(hbb_line, line);
     } else {
       wclrtobot(win_entry->win);
       break;
@@ -1382,7 +1382,7 @@
       scr_buffer_scroll_lock(1);
     }
     g_free(line->text);
-    g_free(line);
+    g_slice_free(hbb_line, line);
   } else if (autolock && win_entry->bd->lock) {
     scr_buffer_scroll_lock(0);
   }
diff -r a18571d686cd mcabber/mcabber/settings.c
--- a/mcabber/mcabber/settings.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/settings.c	Fri Dec 09 01:48:31 2016 +0200
@@ -528,7 +528,7 @@
     // If value is 0, we do not need to create a structure (that's
     // the default value).
     if (value) {
-      pgpdata = g_new0(T_pgpopt, 1);
+      pgpdata = g_slice_new0(T_pgpopt);
       pgpdata->pgp_disabled = value;
       g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
     }
@@ -568,7 +568,7 @@
     // If value is 0, we do not need to create a structure (that's
     // the default value).
     if (value) {
-      pgpdata = g_new0(T_pgpopt, 1);
+      pgpdata = g_slice_new0(T_pgpopt);
       pgpdata->pgp_force = value;
       g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
     }
@@ -608,7 +608,7 @@
     // If keyid is NULL, we do not need to create a structure (that's
     // the default value).
     if (keyid) {
-      pgpdata = g_new0(T_pgpopt, 1);
+      pgpdata = g_slice_new0(T_pgpopt);
       pgpdata->pgp_keyid = g_strdup(keyid);
       g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
     }
diff -r a18571d686cd mcabber/mcabber/xmpp_iq.c
--- a/mcabber/mcabber/xmpp_iq.c	Fri Dec 09 01:38:39 2016 +0200
+++ b/mcabber/mcabber/xmpp_iq.c	Fri Dec 09 01:48:31 2016 +0200
@@ -791,7 +791,7 @@
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   }
 
-  buf = g_new0(char, 512);
+  buf = g_slice_alloc(512);
 
   r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_RESULT);
   query = lm_message_node_add_child(r->node, "query", NULL);
@@ -818,7 +818,7 @@
 
   lm_connection_send(c, r, NULL);
   lm_message_unref(r);
-  g_free(buf);
+  g_slice_free1(512, buf);
   return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }
 
@@ -846,7 +846,7 @@
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   }
 
-  buf = g_new0(char, 512);
+  buf = g_slice_alloc(512);
 
   r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_RESULT);
   query = lm_message_node_add_child(r->node, "time", NULL);
@@ -883,7 +883,7 @@
 
   lm_connection_send(c, r, NULL);
   lm_message_unref(r);
-  g_free(buf);
+  g_slice_free1(512, buf);
   return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }