use-gslice.diff
changeset 36 b8854e2fe147
parent 33 ce47dc7fc6c0
child 85 93c3cc0d7891
equal deleted inserted replaced
35:c80eb5663234 36:b8854e2fe147
       
     1 # HG changeset patch
       
     2 # Parent 70b1f1918050fa1126a21f4772d5d644799af32b
       
     3 [work-in-progress] Use glib slices to allocate constant-size blocks
       
     4 
       
     5 diff -r 70b1f1918050 mcabber/mcabber/caps.c
       
     6 --- a/mcabber/mcabber/caps.c	Fri Jul 20 17:41:15 2012 +0300
       
     7 +++ b/mcabber/mcabber/caps.c	Fri Jul 20 17:51:21 2012 +0300
       
     8 @@ -52,7 +52,7 @@
       
     9    g_hash_table_destroy(c->identities);
       
    10    g_hash_table_destroy(c->features);
       
    11    g_hash_table_destroy(c->forms);
       
    12 -  g_free(c);
       
    13 +  g_slice_free(caps, c);
       
    14  }
       
    15  
       
    16  void identity_destroy(gpointer data)
       
    17 @@ -61,14 +61,14 @@
       
    18    g_free(i->category);
       
    19    g_free(i->type);
       
    20    g_free(i->name);
       
    21 -  g_free(i);
       
    22 +  g_slice_free(identity, i);
       
    23  }
       
    24  
       
    25  void form_destroy(gpointer data)
       
    26  {
       
    27    dataform *f = data;
       
    28    g_hash_table_destroy(f->fields);
       
    29 -  g_free(f);
       
    30 +  g_slice_free(dataform, f);
       
    31  }
       
    32  
       
    33  void field_destroy(gpointer data)
       
    34 @@ -97,7 +97,7 @@
       
    35  {
       
    36    if (!hash)
       
    37      return;
       
    38 -  caps *c = g_new0(caps, 1);
       
    39 +  caps *c = g_slice_new(caps);
       
    40    c->features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
       
    41    c->identities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, identity_destroy);
       
    42    c->forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, form_destroy);
       
    43 @@ -158,7 +158,7 @@
       
    44  
       
    45    c = g_hash_table_lookup(caps_cache, hash);
       
    46    if (c) {
       
    47 -    identity *i = g_new0(identity, 1);
       
    48 +    identity *i = g_slice_new(identity);
       
    49  
       
    50      i->category = g_strdup(category);
       
    51      i->name = g_strdup(name);
       
    52 @@ -182,7 +182,7 @@
       
    53      return;
       
    54    c = g_hash_table_lookup(caps_cache, hash);
       
    55    if (c) {
       
    56 -    dataform *d = g_new0(dataform, 1);
       
    57 +    dataform *d = g_slice_new(dataform);
       
    58      char *f = g_strdup(formtype);
       
    59  
       
    60      d->fields = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, field_destroy);
       
    61 diff -r 70b1f1918050 mcabber/mcabber/commands.c
       
    62 --- a/mcabber/mcabber/commands.c	Fri Jul 20 17:41:15 2012 +0300
       
    63 +++ b/mcabber/mcabber/commands.c	Fri Jul 20 17:51:21 2012 +0300
       
    64 @@ -114,7 +114,7 @@
       
    65        gpointer userdata = command->userdata;
       
    66        Commands = g_slist_delete_link(Commands, sl_cmd);
       
    67        compl_del_category_word(COMPL_CMD, command->name);
       
    68 -      g_free(command);
       
    69 +      g_slice_free(cmd, command);
       
    70        return userdata;
       
    71      }
       
    72    return NULL;
       
    73 @@ -126,7 +126,7 @@
       
    74  gpointer cmd_add(const char *name, const char *help, guint flags_row1,
       
    75                   guint flags_row2, void (*f)(char*), gpointer userdata)
       
    76  {
       
    77 -  cmd *n_cmd = g_new0(cmd, 1);
       
    78 +  cmd *n_cmd = g_slice_new0(cmd);
       
    79    strncpy(n_cmd->name, name, 32-1);
       
    80    n_cmd->help = help;
       
    81    n_cmd->completion_flags[0] = flags_row1;
       
    82 @@ -1579,7 +1579,7 @@
       
    83      return NULL;
       
    84    }
       
    85  
       
    86 -  msgbuf = g_new0(char, HBB_BLOCKSIZE);
       
    87 +  msgbuf = g_slice_alloc0(HBB_BLOCKSIZE);
       
    88    len = fread(msgbuf, 1, HBB_BLOCKSIZE-1, fd);
       
    89    fclose(fd);
       
    90  
       
    91 @@ -1603,7 +1603,7 @@
       
    92    if (*p || (size_t)(p-msgbuf) != len) { // We're not at the End Of Line...
       
    93      scr_LogPrint(LPRINT_LOGNORM, "Message file contains "
       
    94                   "invalid characters (%s)", filename);
       
    95 -    g_free(msgbuf);
       
    96 +    g_slice_free1(HBB_BLOCKSIZE, msgbuf);
       
    97      return NULL;
       
    98    }
       
    99  
       
   100 @@ -1617,7 +1617,7 @@
       
   101    // It could be empty, once the trailing newlines are gone
       
   102    if (p == msgbuf && *p == '\n') {
       
   103      scr_LogPrint(LPRINT_LOGNORM, "Message file is empty (%s)", filename);
       
   104 -    g_free(msgbuf);
       
   105 +    g_slice_free1(HBB_BLOCKSIZE, msgbuf);
       
   106      return NULL;
       
   107    }
       
   108  
       
   109 @@ -1626,7 +1626,7 @@
       
   110    if (!msgbuf_utf8 && msgbuf)
       
   111      scr_LogPrint(LPRINT_LOGNORM, "Message file charset conversion error (%s)",
       
   112                   filename);
       
   113 -  g_free(msgbuf);
       
   114 +  g_slice_free1(HBB_BLOCKSIZE, msgbuf);
       
   115    return msgbuf_utf8;
       
   116  }
       
   117  
       
   118 @@ -1898,7 +1898,7 @@
       
   119    esub   = buddy_getsubscription(bud);
       
   120    on_srv = buddy_getonserverflag(bud);
       
   121  
       
   122 -  buffer = g_new(char, 4096);
       
   123 +  buffer = g_slice_alloc(4096);
       
   124  
       
   125    if (bjid) {
       
   126      GSList *resources, *p_res;
       
   127 @@ -1997,7 +1997,7 @@
       
   128                   type == ROSTER_TYPE_GROUP ? "group" :
       
   129                   (type == ROSTER_TYPE_SPECIAL ? "special" : "unknown"));
       
   130    }
       
   131 -  g_free(buffer);
       
   132 +  g_slice_free1(4096, buffer);
       
   133  
       
   134    // Tell the user if this item has an annotation.
       
   135    if (type == ROSTER_TYPE_USER ||
       
   136 @@ -2045,7 +2045,7 @@
       
   137  
       
   138    bjid = buddy_getjid(bud);
       
   139  
       
   140 -  buffer = g_new(char, 4096);
       
   141 +  buffer = g_slice_alloc(4096);
       
   142    strncpy(buffer, "Room members:", 127);
       
   143    scr_WriteIncomingMessage(bjid, buffer, 0, HBB_PREFIX_INFO, 0);
       
   144  
       
   145 @@ -2100,7 +2100,7 @@
       
   146      g_free(p_res->data);
       
   147    }
       
   148    g_slist_free(resources);
       
   149 -  g_free(buffer);
       
   150 +  g_slice_free1(4096, buffer);
       
   151  }
       
   152  
       
   153  static void move_group_member(gpointer bud, void *groupnamedata)
       
   154 @@ -3091,7 +3091,7 @@
       
   155    affil = buddy_getaffil(bud, nick);
       
   156    realjid = buddy_getrjid(bud, nick);
       
   157  
       
   158 -  buffer = g_new(char, 4096);
       
   159 +  buffer = g_slice_alloc(4096);
       
   160  
       
   161    snprintf(buffer, 4095, "Whois [%s]", nick);
       
   162    scr_WriteIncomingMessage(bjid, buffer, 0, msg_flag, 0);
       
   163 @@ -3121,7 +3121,7 @@
       
   164  
       
   165    scr_WriteIncomingMessage(bjid, "End of WHOIS", 0, msg_flag, 0);
       
   166  
       
   167 -  g_free(buffer);
       
   168 +  g_slice_free1(4096, buffer);
       
   169    g_free(nick);
       
   170    if (paramlst)
       
   171      free_arg_lst(paramlst);
       
   172 diff -r 70b1f1918050 mcabber/mcabber/events.c
       
   173 --- a/mcabber/mcabber/events.c	Fri Jul 20 17:41:15 2012 +0300
       
   174 +++ b/mcabber/mcabber/events.c	Fri Jul 20 17:51:21 2012 +0300
       
   175 @@ -83,7 +83,7 @@
       
   176      return NULL;
       
   177    }
       
   178  
       
   179 -  event = g_new(evs_t, 1);
       
   180 +  event = g_slice_new(evs_t);
       
   181  
       
   182    event->id          = stridn;
       
   183    event->description = g_strdup(desc);
       
   184 @@ -134,7 +134,7 @@
       
   185    evs_list = g_slist_remove(evs_list, event);
       
   186    g_free(event->id);
       
   187    g_free(event->description);
       
   188 -  g_free(event);
       
   189 +  g_slice_free(evs_t, event);
       
   190  
       
   191    return 0; // Ok, deleted
       
   192  }
       
   193 @@ -213,7 +213,7 @@
       
   194      evs_list = g_slist_remove(evs_list, event);
       
   195      g_free(event->id);
       
   196      g_free(event->description);
       
   197 -    g_free(event);
       
   198 +    g_slice_free(evs_t, event);
       
   199    }
       
   200    g_slist_free(evs_list);
       
   201    evs_list = NULL;
       
   202 diff -r 70b1f1918050 mcabber/mcabber/hbuf.c
       
   203 --- a/mcabber/mcabber/hbuf.c	Fri Jul 20 17:41:15 2012 +0300
       
   204 +++ b/mcabber/mcabber/hbuf.c	Fri Jul 20 17:51:21 2012 +0300
       
   205 @@ -29,7 +29,6 @@
       
   206  #include "utf8.h"
       
   207  #include "screen.h"
       
   208  
       
   209 -
       
   210  /* This is a private structure type */
       
   211  
       
   212  typedef struct {
       
   213 @@ -59,7 +58,7 @@
       
   214  {
       
   215    GList *curr_elt = first_hbuf_elt;
       
   216  
       
   217 -  // Let's add non-persistent blocs if necessary
       
   218 +  // Let's add non-persistent blocks if necessary
       
   219    // - If there are '\n' in the string
       
   220    // - If length > width (and width != 0)
       
   221    while (curr_elt) {
       
   222 @@ -95,17 +94,21 @@
       
   223        end = hbuf_b_curr->ptr_end;
       
   224        hbuf_b_curr->ptr_end = br;
       
   225        // Create another block
       
   226 -      hbuf_b_curr = g_new0(hbuf_block, 1);
       
   227 +      hbuf_b_curr = g_slice_new(hbuf_block);
       
   228        // The block must be persistent after a CR
       
   229        if (cr) {
       
   230          hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end + 1; // == cr+1
       
   231          hbuf_b_curr->flags  = HBB_FLAG_PERSISTENT;
       
   232        } else {
       
   233          hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end; // == br
       
   234 -        hbuf_b_curr->flags    = 0;
       
   235 +        hbuf_b_curr->flags  = 0;
       
   236        }
       
   237 -      hbuf_b_curr->ptr_end  = end;
       
   238 +      hbuf_b_curr->ptr_end       = end;
       
   239        hbuf_b_curr->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
       
   240 +      hbuf_b_curr->prefix.timestamp  = 0;
       
   241 +      hbuf_b_curr->prefix.flags      = 0;
       
   242 +      hbuf_b_curr->prefix.mucnicklen = 0;
       
   243 +      hbuf_b_curr->prefix.xep184     = NULL;
       
   244        // This is OK because insert_before(NULL) == append():
       
   245        *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr);
       
   246      }
       
   247 @@ -138,7 +141,7 @@
       
   248    textlen = strlen(text);
       
   249    hbb_blocksize = MAX(textlen+1, HBB_BLOCKSIZE);
       
   250  
       
   251 -  hbuf_block_elt = g_new0(hbuf_block, 1);
       
   252 +  hbuf_block_elt = g_slice_new(hbuf_block);
       
   253    hbuf_block_elt->prefix.timestamp  = timestamp;
       
   254    hbuf_block_elt->prefix.flags      = prefix_flags;
       
   255    hbuf_block_elt->prefix.mucnicklen = mucnicklen;
       
   256 @@ -146,7 +149,7 @@
       
   257    if (!*p_hbuf) {
       
   258      hbuf_block_elt->ptr  = g_new(char, hbb_blocksize);
       
   259      if (!hbuf_block_elt->ptr) {
       
   260 -      g_free(hbuf_block_elt);
       
   261 +      g_slice_free(hbuf_block, hbuf_block_elt);
       
   262        return;
       
   263      }
       
   264      hbuf_block_elt->flags  = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
       
   265 @@ -208,7 +211,7 @@
       
   266                  g_free(hbuf_b_elt->ptr);
       
   267                }
       
   268              }
       
   269 -            g_free(hbuf_b_elt);
       
   270 +            g_slice_free(hbuf_block, hbuf_b_elt);
       
   271              hbuf_head = *p_hbuf = g_list_delete_link(hbuf_head, hbuf_elt);
       
   272            }
       
   273            n--;
       
   274 @@ -245,7 +248,7 @@
       
   275      if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) {
       
   276        g_free(hbuf_b_elt->ptr);
       
   277      }
       
   278 -    g_free(hbuf_b_elt);
       
   279 +    g_slice_free(hbuf_block, hbuf_b_elt);
       
   280    }
       
   281  
       
   282    g_list_free(first_elt);
       
   283 @@ -275,7 +278,7 @@
       
   284      // Is next line not-persistent?
       
   285      if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) {
       
   286        hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end;
       
   287 -      g_free(hbuf_b_next);
       
   288 +      g_slice_free(hbuf_block, hbuf_b_next);
       
   289        curr_elt = g_list_delete_link(curr_elt, next_elt);
       
   290      } else
       
   291        curr_elt = next_elt;
       
   292 @@ -309,8 +312,8 @@
       
   293  //  hbuf_get_lines(hbuf, n)
       
   294  // Returns an array of n hbb_line pointers
       
   295  // (The first line will be the line currently pointed by hbuf)
       
   296 -// Note: The caller should free the array, the hbb_line pointers and the
       
   297 -// text pointers after use.
       
   298 +// Note: The caller should g_free the array, g_slice_free hbb_line pointers
       
   299 +// and g_free text pointers after use.
       
   300  hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
       
   301  {
       
   302    unsigned int i;
       
   303 @@ -349,7 +352,7 @@
       
   304  
       
   305        blk = (hbuf_block*)(hbuf->data);
       
   306        maxlen = blk->ptr_end - blk->ptr;
       
   307 -      *array_elt = (hbb_line*)g_new(hbb_line, 1);
       
   308 +      *array_elt = (hbb_line*)g_slice_new(hbb_line);
       
   309        (*array_elt)->timestamp  = blk->prefix.timestamp;
       
   310        (*array_elt)->flags      = blk->prefix.flags;
       
   311        (*array_elt)->mucnicklen = blk->prefix.mucnicklen;
       
   312 diff -r 70b1f1918050 mcabber/mcabber/hooks.c
       
   313 --- a/mcabber/mcabber/hooks.c	Fri Jul 20 17:41:15 2012 +0300
       
   314 +++ b/mcabber/mcabber/hooks.c	Fri Jul 20 17:51:21 2012 +0300
       
   315 @@ -99,7 +99,7 @@
       
   316                       gint priority, gpointer userdata)
       
   317  {
       
   318    GSList **hqueue = NULL;
       
   319 -  hook_list_data_t *h = g_new(hook_list_data_t, 1);
       
   320 +  hook_list_data_t *h = g_slice_new(hook_list_data_t);
       
   321  
       
   322    h->handler  = handler;
       
   323    h->priority = priority;
       
   324 @@ -148,7 +148,7 @@
       
   325    el = g_slist_find_custom(*hqueue, &hid,
       
   326                             (GCompareFunc)_hk_queue_search_cb);
       
   327    if (el) {
       
   328 -    g_free(el->data);
       
   329 +    g_slice_free(hook_list_data_t, el->data);
       
   330      *hqueue = g_slist_delete_link(*hqueue, el);
       
   331      // Remove hook hash table entry if the hook queue is empty
       
   332      if (!*hqueue)
       
   333 diff -r 70b1f1918050 mcabber/mcabber/modules.c
       
   334 --- a/mcabber/mcabber/modules.c	Fri Jul 20 17:41:15 2012 +0300
       
   335 +++ b/mcabber/mcabber/modules.c	Fri Jul 20 17:51:21 2012 +0300
       
   336 @@ -176,7 +176,7 @@
       
   337    }
       
   338  
       
   339    { // Register module
       
   340 -    loaded_module_t *module = g_new(loaded_module_t, 1);
       
   341 +    loaded_module_t *module = g_slice_new(loaded_module_t);
       
   342  
       
   343      module->refcount     = 1;
       
   344      module->locked       = manual;
       
   345 @@ -267,7 +267,7 @@
       
   346    // Output this here, as arg may point to module->name
       
   347    scr_LogPrint(LPRINT_LOGNORM, "Unloaded module %s.", module->name);
       
   348    g_free(module->name);
       
   349 -  g_free(module);
       
   350 +  g_slice_free(loaded_module_t, module);
       
   351  
       
   352    return NULL;
       
   353  }
       
   354 diff -r 70b1f1918050 mcabber/mcabber/screen.c
       
   355 --- a/mcabber/mcabber/screen.c	Fri Jul 20 17:41:15 2012 +0300
       
   356 +++ b/mcabber/mcabber/screen.c	Fri Jul 20 17:51:21 2012 +0300
       
   357 @@ -1305,7 +1305,7 @@
       
   358          wattrset(win_entry->win, get_color(COLOR_GENERAL));
       
   359  
       
   360        g_free(line->text);
       
   361 -      g_free(line);
       
   362 +      g_slice_free(hbb_line, line);
       
   363      } else {
       
   364        wclrtobot(win_entry->win);
       
   365        break;
       
   366 diff -r 70b1f1918050 mcabber/mcabber/settings.c
       
   367 --- a/mcabber/mcabber/settings.c	Fri Jul 20 17:41:15 2012 +0300
       
   368 +++ b/mcabber/mcabber/settings.c	Fri Jul 20 17:51:21 2012 +0300
       
   369 @@ -453,7 +453,7 @@
       
   370      // If value is 0, we do not need to create a structure (that's
       
   371      // the default value).
       
   372      if (value) {
       
   373 -      pgpdata = g_new0(T_pgpopt, 1);
       
   374 +      pgpdata = g_slice_new0(T_pgpopt);
       
   375        pgpdata->pgp_disabled = value;
       
   376        g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
       
   377      }
       
   378 @@ -493,7 +493,7 @@
       
   379      // If value is 0, we do not need to create a structure (that's
       
   380      // the default value).
       
   381      if (value) {
       
   382 -      pgpdata = g_new0(T_pgpopt, 1);
       
   383 +      pgpdata = g_slice_new0(T_pgpopt);
       
   384        pgpdata->pgp_force = value;
       
   385        g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
       
   386      }
       
   387 @@ -533,7 +533,7 @@
       
   388      // If keyid is NULL, we do not need to create a structure (that's
       
   389      // the default value).
       
   390      if (keyid) {
       
   391 -      pgpdata = g_new0(T_pgpopt, 1);
       
   392 +      pgpdata = g_slice_new0(T_pgpopt);
       
   393        pgpdata->pgp_keyid = g_strdup(keyid);
       
   394        g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
       
   395      }
       
   396 diff -r 70b1f1918050 mcabber/mcabber/xmpp_iq.c
       
   397 --- a/mcabber/mcabber/xmpp_iq.c	Fri Jul 20 17:41:15 2012 +0300
       
   398 +++ b/mcabber/mcabber/xmpp_iq.c	Fri Jul 20 17:51:21 2012 +0300
       
   399 @@ -751,7 +751,7 @@
       
   400                   lm_message_get_from(m));
       
   401    }
       
   402  
       
   403 -  buf = g_new0(char, 512);
       
   404 +  buf = g_slice_alloc(512);
       
   405  
       
   406    r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_RESULT);
       
   407    query = lm_message_node_add_child(r->node, "query", NULL);
       
   408 @@ -778,7 +778,7 @@
       
   409  
       
   410    lm_connection_send(c, r, NULL);
       
   411    lm_message_unref(r);
       
   412 -  g_free(buf);
       
   413 +  g_slice_free1(512, buf);
       
   414    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
       
   415  }
       
   416  
       
   417 @@ -801,7 +801,7 @@
       
   418                   lm_message_get_from(m));
       
   419    }
       
   420  
       
   421 -  buf = g_new0(char, 512);
       
   422 +  buf = g_slice_alloc(512);
       
   423  
       
   424    r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_RESULT);
       
   425    query = lm_message_node_add_child(r->node, "time", NULL);
       
   426 @@ -838,7 +838,7 @@
       
   427  
       
   428    lm_connection_send(c, r, NULL);
       
   429    lm_message_unref(r);
       
   430 -  g_free(buf);
       
   431 +  g_slice_free1(512, buf);
       
   432    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
       
   433  }
       
   434