Changeset 6a5f0c3


Ignore:
Timestamp:
Dec 25, 2013, 3:22:29 PM (10 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Children:
b36b9a6
Parents:
58777e9
git-author:
Anders Kaseorg <andersk@mit.edu> (12/25/13 12:53:27)
git-committer:
Anders Kaseorg <andersk@mit.edu> (12/25/13 15:22:29)
Message:
owl_message_set_attribute: Take a destroy function for the value

In the case of constant strings, and ZNotice_t fields other than the
body, this lets us avoid wasting memory on unnecessary string copies.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • aim.c

    r7dcef03 r6a5f0c3  
    10561056                         OWL_MESSAGE_DIRECTION_IN,
    10571057                         0);
    1058   if (args->icbmflags & AIM_IMFLAGS_AWAY) owl_message_set_attribute(m, "isauto", "");
     1058  if (args->icbmflags & AIM_IMFLAGS_AWAY) owl_message_set_attribute(m, "isauto", "", NULL);
    10591059  owl_global_messagequeue_addmsg(&g, m);
    10601060  g_free(stripmsg);
  • message.c

    r8f95fc4 r6a5f0c3  
    5252 * name already exists, replace the old value with the new value
    5353 */
    54 void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue)
    55 {
    56   g_datalist_set_data_full(&m->attributes, attrname,
    57                            owl_validate_or_convert(attrvalue), g_free);
     54void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue, GDestroyNotify attrdestroy)
     55{
     56  if (owl_needs_convert(attrvalue)) {
     57    g_datalist_set_data_full(&m->attributes, attrname,
     58                             owl_validate_or_convert(attrvalue), g_free);
     59    if (attrdestroy)
     60      attrdestroy((gpointer)attrvalue);
     61  } else {
     62    g_datalist_set_data_full(&m->attributes, attrname, (gpointer)attrvalue, attrdestroy);
     63  }
    5864}
    5965
     
    118124}
    119125
    120 void owl_message_set_class(owl_message *m, const char *class)
    121 {
    122   owl_message_set_attribute(m, "class", class);
    123 }
    124 
    125126const char *owl_message_get_class(const owl_message *m)
    126127{
     
    132133}
    133134
    134 void owl_message_set_instance(owl_message *m, const char *inst)
    135 {
    136   owl_message_set_attribute(m, "instance", inst);
    137 }
    138 
    139135const char *owl_message_get_instance(const owl_message *m)
    140136{
     
    146142}
    147143
    148 void owl_message_set_sender(owl_message *m, const char *sender)
    149 {
    150   owl_message_set_attribute(m, "sender", sender);
    151 }
    152 
    153144const char *owl_message_get_sender(const owl_message *m)
    154145{
     
    160151}
    161152
    162 void owl_message_set_zsig(owl_message *m, const char *zsig)
    163 {
    164   owl_message_set_attribute(m, "zsig", zsig);
    165 }
    166 
    167153const char *owl_message_get_zsig(const owl_message *m)
    168154{
     
    174160}
    175161
    176 void owl_message_set_recipient(owl_message *m, const char *recip)
    177 {
    178   owl_message_set_attribute(m, "recipient", recip);
    179 }
    180 
    181162const char *owl_message_get_recipient(const owl_message *m)
    182163{
     
    190171}
    191172
    192 void owl_message_set_realm(owl_message *m, const char *realm)
    193 {
    194   owl_message_set_attribute(m, "realm", realm);
    195 }
    196 
    197173const char *owl_message_get_realm(const owl_message *m)
    198174{
     
    204180}
    205181
    206 void owl_message_set_body(owl_message *m, const char *body)
    207 {
    208   owl_message_set_attribute(m, "body", body);
    209 }
    210 
    211182const char *owl_message_get_body(const owl_message *m)
    212183{
     
    219190
    220191
    221 void owl_message_set_opcode(owl_message *m, const char *opcode)
    222 {
    223   owl_message_set_attribute(m, "opcode", opcode);
    224 }
    225 
    226192const char *owl_message_get_opcode(const owl_message *m)
    227193{
     
    236202void owl_message_set_islogin(owl_message *m)
    237203{
    238   owl_message_set_attribute(m, "loginout", "login");
     204  owl_message_set_attribute(m, "loginout", "login", NULL);
    239205}
    240206
     
    242208void owl_message_set_islogout(owl_message *m)
    243209{
    244   owl_message_set_attribute(m, "loginout", "logout");
     210  owl_message_set_attribute(m, "loginout", "logout", NULL);
    245211}
    246212
     
    277243void owl_message_set_isprivate(owl_message *m)
    278244{
    279   owl_message_set_attribute(m, "isprivate", "true");
     245  owl_message_set_attribute(m, "isprivate", "true", NULL);
    280246}
    281247
     
    302268void owl_message_set_type_admin(owl_message *m)
    303269{
    304   owl_message_set_attribute(m, "type", "admin");
     270  owl_message_set_attribute(m, "type", "admin", NULL);
    305271}
    306272
    307273void owl_message_set_type_loopback(owl_message *m)
    308274{
    309   owl_message_set_attribute(m, "type", "loopback");
     275  owl_message_set_attribute(m, "type", "loopback", NULL);
    310276}
    311277
    312278void owl_message_set_type_zephyr(owl_message *m)
    313279{
    314   owl_message_set_attribute(m, "type", "zephyr");
     280  owl_message_set_attribute(m, "type", "zephyr", NULL);
    315281}
    316282
    317283void owl_message_set_type_aim(owl_message *m)
    318284{
    319   owl_message_set_attribute(m, "type", "AIM");
    320 }
    321 
    322 void owl_message_set_type(owl_message *m, const char* type)
    323 {
    324   owl_message_set_attribute(m, "type", type);
     285  owl_message_set_attribute(m, "type", "AIM", NULL);
    325286}
    326287
     
    431392  if (!z) return "";
    432393  return z;
    433 }
    434 
    435 void owl_message_set_zwriteline(owl_message *m, const char *line)
    436 {
    437   owl_message_set_attribute(m, "zwriteline", line);
    438394}
    439395
     
    512468
    513469void owl_message_set_isanswered(owl_message *m) {
    514   owl_message_set_attribute(m, "question", "answered");
     470  owl_message_set_attribute(m, "question", "answered", NULL);
    515471}
    516472
     
    644600{
    645601  owl_message_init(m);
    646   owl_message_set_body(m, text);
    647   owl_message_set_sender(m, sender);
    648   owl_message_set_recipient(m, recipient);
     602  owl_message_set_attribute(m, "body", g_strdup(text), g_free);
     603  owl_message_set_attribute(m, "sender", g_strdup(sender), g_free);
     604  owl_message_set_attribute(m, "recipient", g_strdup(recipient), g_free);
    649605  owl_message_set_type_aim(m);
    650606
     
    671627  owl_message_init(m);
    672628  owl_message_set_type_admin(m);
    673   owl_message_set_body(m, text);
    674   owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */
     629  owl_message_set_attribute(m, "body", g_strdup(text), g_free);
     630  owl_message_set_attribute(m, "adminheader", g_strdup(header), g_free); /* just a hack for now */
    675631}
    676632
     
    680636  owl_message_init(m);
    681637  owl_message_set_type_loopback(m);
    682   owl_message_set_body(m, text);
    683   owl_message_set_sender(m, "loopsender");
    684   owl_message_set_recipient(m, "looprecip");
     638  owl_message_set_attribute(m, "body", g_strdup(text), g_free);
     639  owl_message_set_attribute(m, "sender", "loopsender", NULL);
     640  owl_message_set_attribute(m, "recipient", "looprecip", NULL);
    685641  owl_message_set_isprivate(m);
    686642}
     
    716672    }
    717673
    718     owl_message_set_attribute(m, "zephyr_ccs", recips->str);
    719     g_string_free(recips, true);
     674    owl_message_set_attribute(m, "zephyr_ccs", g_string_free(recips, false), g_free);
    720675  }
    721676}
     
    729684  struct hostent *hent;
    730685#endif /* ZNOTICE_SOCKADDR */
    731   char *tmp, *tmp2;
     686  char *tmp;
    732687  int len;
    733688
     
    751706
    752707  /* set other info */
    753   owl_message_set_sender(m, n->z_sender);
    754   owl_message_set_class(m, n->z_class);
    755   owl_message_set_instance(m, n->z_class_inst);
    756   owl_message_set_recipient(m, n->z_recipient);
     708  owl_message_set_attribute(m, "sender", n->z_sender, NULL);
     709  owl_message_set_attribute(m, "class", n->z_class, NULL);
     710  owl_message_set_attribute(m, "instance", n->z_class_inst, NULL);
     711  owl_message_set_attribute(m, "recipient", n->z_recipient, NULL);
    757712  if (n->z_opcode) {
    758     owl_message_set_opcode(m, n->z_opcode);
     713    owl_message_set_attribute(m, "opcode", n->z_opcode, NULL);
    759714  } else {
    760     owl_message_set_opcode(m, "");
    761   }
    762   owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len));
    763 
    764   owl_message_set_realm(m, zuser_realm(n->z_recipient));
     715    owl_message_set_attribute(m, "opcode", "", NULL);
     716  }
     717  owl_message_set_attribute(m, "zsig", owl_zephyr_get_zsig(n, &len), NULL);
     718
     719  owl_message_set_attribute(m, "realm", zuser_realm(n->z_recipient), NULL);
    765720
    766721  /* Set the "isloginout" attribute if it's a login message */
    767722  if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) {
    768723    if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) {
    769       tmp=owl_zephyr_get_field(n, 1);
    770       owl_message_set_attribute(m, "loginhost", tmp);
    771       g_free(tmp);
    772 
    773       tmp=owl_zephyr_get_field(n, 3);
    774       owl_message_set_attribute(m, "logintty", tmp);
    775       g_free(tmp);
     724      owl_message_set_attribute(m, "loginhost", owl_zephyr_get_field(n, 1), g_free);
     725      owl_message_set_attribute(m, "logintty", owl_zephyr_get_field(n, 3), g_free);
    776726    }
    777727
     
    792742  if (!strcasecmp(n->z_message, "Automated reply:") ||
    793743      !strcasecmp(n->z_opcode, "auto")) {
    794     owl_message_set_attribute(m, "isauto", "");
     744    owl_message_set_attribute(m, "isauto", "", NULL);
    795745  }
    796746
     
    812762  tmp=owl_zephyr_get_message(n, m);
    813763  if (owl_global_is_newlinestrip(&g)) {
    814     tmp2=owl_util_stripnewlines(tmp);
    815     owl_message_set_body(m, tmp2);
    816     g_free(tmp2);
     764    owl_message_set_attribute(m, "body", owl_util_stripnewlines(tmp), g_free);
     765    g_free(tmp);
    817766  } else {
    818     owl_message_set_body(m, tmp);
    819   }
    820   g_free(tmp);
     767    owl_message_set_attribute(m, "body", tmp, g_free);
     768  }
    821769
    822770  /* if zcrypt is enabled try to decrypt the message */
     
    845793        out[len - 8] = 0;
    846794      }
    847       owl_message_set_body(m, out);
     795      owl_message_set_attribute(m, "body", out, g_free);
    848796    } else {
    849797      /* Replace the opcode. Otherwise the UI and other bits of code think the
    850798       * message was encrypted. */
    851       owl_message_set_opcode(m, "failed-decrypt");
     799      owl_message_set_attribute(m, "opcode", "failed-decrypt", NULL);
     800      g_free(out);
    852801    }
    853     g_free(out);
    854802  }
    855803
     
    878826  owl_message_set_direction_in(m);
    879827
    880   owl_message_set_attribute(m, "pseudo", "");
    881   owl_message_set_attribute(m, "loginhost", host ? host : "");
    882   owl_message_set_attribute(m, "logintty", tty ? tty : "");
    883 
    884   owl_message_set_sender(m, longuser);
    885   owl_message_set_class(m, "LOGIN");
    886   owl_message_set_instance(m, longuser);
    887   owl_message_set_recipient(m, "");
     828  owl_message_set_attribute(m, "pseudo", "", NULL);
     829  owl_message_set_attribute(m, "loginhost", g_strdup(host ? host : ""), g_free);
     830  owl_message_set_attribute(m, "logintty", g_strdup(tty ? tty : ""), g_free);
     831
     832  owl_message_set_attribute(m, "sender", g_strdup(longuser), g_free);
     833  owl_message_set_attribute(m, "class", "LOGIN", NULL);
     834  owl_message_set_attribute(m, "instance", g_strdup(longuser), g_free);
     835  owl_message_set_attribute(m, "recipient", "", NULL);
    888836  if (direction==0) {
    889     owl_message_set_opcode(m, "USER_LOGIN");
     837    owl_message_set_attribute(m, "opcode", "USER_LOGIN", NULL);
    890838    owl_message_set_islogin(m);
    891839  } else if (direction==1) {
    892     owl_message_set_opcode(m, "USER_LOGOUT");
     840    owl_message_set_attribute(m, "opcode", "USER_LOGOUT", NULL);
    893841    owl_message_set_islogout(m);
    894842  }
    895843
    896   owl_message_set_realm(m, zuser_realm(longuser));
    897 
    898   owl_message_set_body(m, "<uninitialized>");
     844  owl_message_set_attribute(m, "realm", g_strdup(zuser_realm(longuser)), g_free);
     845
     846  owl_message_set_attribute(m, "body", "<uninitialized>", NULL);
    899847
    900848  /* save the hostname */
     
    913861  owl_message_set_direction_out(m);
    914862  owl_message_set_type_zephyr(m);
    915   owl_message_set_sender(m, owl_zephyr_get_sender());
    916   owl_message_set_class(m, owl_zwrite_get_class(z));
    917   owl_message_set_instance(m, owl_zwrite_get_instance(z));
     863  owl_message_set_attribute(m, "sender", g_strdup(owl_zephyr_get_sender()), g_free);
     864  owl_message_set_attribute(m, "class", g_strdup(owl_zwrite_get_class(z)), g_free);
     865  owl_message_set_attribute(m, "instance", g_strdup(owl_zwrite_get_instance(z)), g_free);
    918866  if (recip_index < owl_zwrite_get_numrecips(z)) {
    919867    char *zuser = owl_zwrite_get_recip_n_with_realm(z, recip_index);
    920868    char *longzuser = long_zuser(zuser);
    921     owl_message_set_recipient(m, longzuser);
    922     owl_message_set_realm(m, zuser_realm(longzuser));
    923     g_free(longzuser);
     869    owl_message_set_attribute(m, "realm", g_strdup(zuser_realm(longzuser)), g_free);
     870    owl_message_set_attribute(m, "recipient", longzuser, g_free);
    924871    g_free(zuser);
    925872  } else {
     
    928875     * anyway. */
    929876    const char *realm = owl_zwrite_get_realm(z);
    930     owl_message_set_realm(m, realm[0] ? realm : owl_zephyr_get_realm());
    931   }
    932   owl_message_set_opcode(m, owl_zwrite_get_opcode(z));
     877    owl_message_set_attribute(m, "realm", g_strdup(realm[0] ? realm : owl_zephyr_get_realm()), g_free);
     878  }
     879  owl_message_set_attribute(m, "opcode", g_strdup(owl_zwrite_get_opcode(z)), g_free);
    933880
    934881  /* Although not strictly the zwriteline, anyone using the unsantized version
    935882   * of it probably has a bug. */
    936883  replyline = owl_zwrite_get_replyline(z, recip_index);
    937   owl_message_set_zwriteline(m, replyline);
     884  owl_message_set_attribute(m, "zwriteline", g_strdup(replyline), g_free);
    938885  g_free(replyline);
    939886
    940   owl_message_set_body(m, body);
    941   owl_message_set_zsig(m, owl_zwrite_get_zsig(z));
     887  owl_message_set_attribute(m, "body", g_strdup(body), g_free);
     888  owl_message_set_attribute(m, "zsig", g_strdup(owl_zwrite_get_zsig(z)), g_free);
    942889 
    943890  /* save the hostname */
  • perlconfig.c

    r8f95fc4 r6a5f0c3  
    195195    key = hv_iterkey(ent, &len);
    196196    val = SvPV_nolen(hv_iterval(hash, ent));
    197     if(!strcmp(key, "type")) {
    198       owl_message_set_type(m, val);
    199     } else if(!strcmp(key, "direction")) {
     197    if (!strcmp(key, "direction")) {
    200198      owl_message_set_direction(m, owl_message_parse_direction(val));
    201199    } else if(!strcmp(key, "private")) {
     
    206204    } else if (!strcmp(key, "hostname")) {
    207205      owl_message_set_hostname(m, val);
    208     } else if (!strcmp(key, "zwriteline")) {
    209       owl_message_set_zwriteline(m, val);
    210206    } else if (!strcmp(key, "time")) {
    211207      g_free(m->timestr);
     
    214210      m->time = mktime(&tm);
    215211    } else {
    216       owl_message_set_attribute(m, key, val);
     212      owl_message_set_attribute(m, key, g_strdup(val), g_free);
    217213    }
    218214  }
    219215  if(owl_message_is_type_admin(m)) {
    220216    if(!owl_message_get_attribute_value(m, "adminheader"))
    221       owl_message_set_attribute(m, "adminheader", "");
     217      owl_message_set_attribute(m, "adminheader", "", NULL);
    222218  }
    223219  return m;
  • tester.c

    r21dc927 r6a5f0c3  
    464464  owl_message_set_type_zephyr(&m);
    465465  owl_message_set_direction_in(&m);
    466   owl_message_set_class(&m, "owl");
    467   owl_message_set_instance(&m, "tester");
    468   owl_message_set_sender(&m, "owl-user");
    469   owl_message_set_recipient(&m, "joe");
    470   owl_message_set_attribute(&m, "foo", "bar");
     466  owl_message_set_attribute(&m, "class", "owl", NULL);
     467  owl_message_set_attribute(&m, "instance", "tester", NULL);
     468  owl_message_set_attribute(&m, "sender", "owl-user", NULL);
     469  owl_message_set_attribute(&m, "recipient", "joe", NULL);
     470  owl_message_set_attribute(&m, "foo", "bar", NULL);
    471471
    472472#define TEST_FILTER(f, e) do {                          \
  • util.c

    r7b89e8c r6a5f0c3  
    607607}
    608608
     609/* Check whether in is not valid UTF-8 or has format characters. */
     610bool owl_needs_convert(const char *in)
     611{
     612  const char *p;
     613  if (!g_utf8_validate(in, -1, NULL))
     614    return true;
     615  for (p = strchr(in, OWL_FMTEXT_UC_STARTBYTE_UTF8);
     616       p != NULL;
     617       p = strchr(p + 1, OWL_FMTEXT_UC_STARTBYTE_UTF8)) {
     618    if (owl_fmtext_is_format_char(g_utf8_get_char(p)))
     619      return true;
     620  }
     621  return false;
     622}
     623
    609624/* If in is not UTF-8, convert from ISO-8859-1. We may want to allow
    610625 * the caller to specify an alternative in the future. We also strip
Note: See TracChangeset for help on using the changeset viewer.