# HG changeset patch
# Parent 70b1f1918050fa1126a21f4772d5d644799af32b
[work-in-progress] Use glib slices to allocate constant-size blocks

diff -r 70b1f1918050 mcabber/mcabber/caps.c
--- a/mcabber/mcabber/caps.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/caps.c	Fri Jul 20 17:51:21 2012 +0300
@@ -52,7 +52,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)
@@ -61,14 +61,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)
@@ -97,7 +97,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);
@@ -158,7 +158,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);
@@ -182,7 +182,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 70b1f1918050 mcabber/mcabber/commands.c
--- a/mcabber/mcabber/commands.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/commands.c	Fri Jul 20 17:51:21 2012 +0300
@@ -114,7 +114,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;
@@ -126,7 +126,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;
@@ -1579,7 +1579,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);
 
@@ -1603,7 +1603,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;
   }
 
@@ -1617,7 +1617,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;
   }
 
@@ -1626,7 +1626,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;
 }
 
@@ -1898,7 +1898,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;
@@ -1997,7 +1997,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 ||
@@ -2045,7 +2045,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);
 
@@ -2100,7 +2100,7 @@
     g_free(p_res->data);
   }
   g_slist_free(resources);
-  g_free(buffer);
+  g_slice_free1(4096, buffer);
 }
 
 static void move_group_member(gpointer bud, void *groupnamedata)
@@ -3091,7 +3091,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);
@@ -3121,7 +3121,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 70b1f1918050 mcabber/mcabber/events.c
--- a/mcabber/mcabber/events.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/events.c	Fri Jul 20 17:51:21 2012 +0300
@@ -83,7 +83,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 70b1f1918050 mcabber/mcabber/hbuf.c
--- a/mcabber/mcabber/hbuf.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/hbuf.c	Fri Jul 20 17:51:21 2012 +0300
@@ -29,7 +29,6 @@
 #include "utf8.h"
 #include "screen.h"
 
-
 /* This is a private structure type */
 
 typedef struct {
@@ -59,7 +58,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) {
@@ -95,17 +94,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);
     }
@@ -138,7 +141,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;
@@ -146,7 +149,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;
@@ -208,7 +211,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--;
@@ -245,7 +248,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);
@@ -275,7 +278,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;
@@ -309,8 +312,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;
@@ -349,7 +352,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 70b1f1918050 mcabber/mcabber/hooks.c
--- a/mcabber/mcabber/hooks.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/hooks.c	Fri Jul 20 17:51:21 2012 +0300
@@ -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 70b1f1918050 mcabber/mcabber/modules.c
--- a/mcabber/mcabber/modules.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/modules.c	Fri Jul 20 17:51:21 2012 +0300
@@ -176,7 +176,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;
@@ -267,7 +267,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 70b1f1918050 mcabber/mcabber/screen.c
--- a/mcabber/mcabber/screen.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/screen.c	Fri Jul 20 17:51:21 2012 +0300
@@ -1305,7 +1305,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;
diff -r 70b1f1918050 mcabber/mcabber/settings.c
--- a/mcabber/mcabber/settings.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/settings.c	Fri Jul 20 17:51:21 2012 +0300
@@ -453,7 +453,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);
     }
@@ -493,7 +493,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);
     }
@@ -533,7 +533,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 70b1f1918050 mcabber/mcabber/xmpp_iq.c
--- a/mcabber/mcabber/xmpp_iq.c	Fri Jul 20 17:41:15 2012 +0300
+++ b/mcabber/mcabber/xmpp_iq.c	Fri Jul 20 17:51:21 2012 +0300
@@ -751,7 +751,7 @@
                  lm_message_get_from(m));
   }
 
-  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);
@@ -778,7 +778,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;
 }
 
@@ -801,7 +801,7 @@
                  lm_message_get_from(m));
   }
 
-  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);
@@ -838,7 +838,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;
 }
 
