Mercurial > pidgin
annotate src/protocols/simple/sipmsg.c @ 11719:109ee3bfeac5
[gaim-migrate @ 14010]
SF Patch #1333770 from corfe83
"Many times in gaim we use the function
g_slist_remove(list,node->data) to remove an element
from a GSList. If we already have the pointer to the
node we want to delete, it is faster to send it the
pointer to the node to delete rather than the data of
the node (we can do this by calling
g_slist_delete_link(list,node)). This change was made
while looking at glib's documentation and the code in
glib's gslist.c.
This is because as the remove/delete function traverses
each node in the list, it doesn't need to spend an
extra memory access to retrieve the data for each
element in the node it is traversing and then compare,
it can simply compare the pointer. In my tests outside
of gaim, this makes a big difference if the node you
are deleting is at a high index in the list. However,
even if you're deleting the first node, it about breaks
even.
So, I've found each case in gaim where we are calling
g_slist_remove, and we already have the pointer to the
appropriate node to delete (this is often the case when
we're doing a for or while loop on a GSList). I've then
replaced it with the appropriate call to
g_slist_delete_link. I, however, didn't do this in
situations where we are explicitly removing the first
element in the list, because in those situations it is
an unnecessary change.
There should be no difference in behavior, but just in
case I've tried running it with valgrind, which reports
the same number of memory leaks after my patch as
before my patch. Of course, I can't guarantee that my
normal behavior on gaim is hitting all the functions
I've changed, but in general testing it Works For Me (tm)."
As with the last patch, this one may not have a practical performance impact (or maybe it does, I have no idea), but it's not worse for any case. Given two ways of doing things where one is always at least as fast and may be faster under some cases, I like to prefer that faster way. This doesn't make the code any uglier, so I'm applying.
committer: Tailor Script <tailor@pidgin.im>
| author | Richard Laager <rlaager@wiktel.com> |
|---|---|
| date | Sat, 22 Oct 2005 20:48:18 +0000 |
| parents | 0d18fa6c3b41 |
| children | 2cf6d4cf2cb0 |
| rev | line source |
|---|---|
| 11181 | 1 /** |
| 2 * @file sipmsg.c | |
| 3 * | |
| 4 * gaim | |
| 5 * | |
| 6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de> | |
| 7 * | |
| 8 * This program is free software; you can redistribute it and/or modify | |
| 9 * it under the terms of the GNU General Public License as published by | |
| 10 * the Free Software Foundation; either version 2 of the License, or | |
| 11 * (at your option) any later version. | |
| 12 * | |
| 13 * This program is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 * GNU General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU General Public License | |
| 19 * along with this program; if not, write to the Free Software | |
| 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 */ | |
| 22 | |
| 23 #include "internal.h" | |
| 24 | |
| 25 #include "accountopt.h" | |
| 26 #include "blist.h" | |
| 27 #include "conversation.h" | |
| 28 #include "debug.h" | |
| 29 #include "notify.h" | |
| 30 #include "prpl.h" | |
| 31 #include "plugin.h" | |
| 32 #include "util.h" | |
| 33 #include "version.h" | |
| 34 | |
| 35 #include "simple.h" | |
| 36 #include "sipmsg.h" | |
| 37 | |
| 38 struct sipmsg *sipmsg_parse_msg(gchar *msg) { | |
| 39 char *tmp = strstr(msg, "\r\n\r\n"); | |
| 40 struct sipmsg *smsg; | |
| 11398 | 41 if(!tmp) return NULL; |
| 11181 | 42 tmp[0]=0; |
| 43 smsg = sipmsg_parse_header(msg); | |
| 44 tmp[0]='\r'; | |
| 45 smsg->body = g_strdup(tmp+4); | |
| 46 return smsg; | |
| 47 } | |
| 48 | |
| 49 struct sipmsg *sipmsg_parse_header(gchar *header) { | |
| 50 struct sipmsg *msg = g_new0(struct sipmsg,1); | |
| 51 gchar **lines = g_strsplit(header,"\r\n",0); | |
| 52 gchar **parts; | |
| 53 gchar *dummy; | |
| 54 gchar *dummy2; | |
| 55 gchar *tmp; | |
| 56 int i=1; | |
| 11483 | 57 if(!lines[0]) return NULL; |
| 11181 | 58 parts = g_strsplit(lines[0], " ", 3); |
| 59 if(!parts[0] || !parts[1] || !parts[2]) { | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
60 g_strfreev(parts); |
|
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
61 g_strfreev(lines); |
| 11181 | 62 g_free(msg); |
| 63 return NULL; | |
| 64 } | |
| 65 if(strstr(parts[0],"SIP")) { // numeric response | |
| 66 msg->method = g_strdup(parts[2]); | |
| 67 msg->response = strtol(parts[1],NULL,10); | |
| 68 } else { // request | |
| 69 msg->method = g_strdup(parts[0]); | |
| 70 msg->target = g_strdup(parts[1]); | |
| 71 msg->response = 0; | |
| 72 } | |
| 73 g_strfreev(parts); | |
| 74 for(i=1; lines[i] && strlen(lines[i])>2; i++) { | |
| 75 parts = g_strsplit(lines[i], ":", 2); | |
| 76 if(!parts[0] || !parts[1]) { | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
77 g_strfreev(parts); |
|
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
78 g_strfreev(lines); |
| 11181 | 79 g_free(msg); |
| 80 return NULL; | |
| 81 } | |
| 82 dummy = parts[1]; | |
| 83 dummy2 = 0; | |
| 84 while(*dummy==' ' || *dummy=='\t') dummy++; | |
| 85 dummy2 = g_strdup(dummy); | |
| 86 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) { | |
| 87 i++; | |
| 88 dummy = lines[i]; | |
| 89 while(*dummy==' ' || *dummy=='\t') dummy++; | |
| 90 tmp = g_strdup_printf("%s %s",dummy2, dummy); | |
| 91 g_free(dummy2); | |
| 92 dummy2 = tmp; | |
| 93 } | |
| 94 sipmsg_add_header(msg, parts[0], dummy2); | |
| 95 g_strfreev(parts); | |
| 96 } | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
97 g_strfreev(lines); |
| 11181 | 98 msg->bodylen = strtol(sipmsg_find_header(msg, "Content-Length"),NULL,10); |
| 99 if(msg->response) { | |
| 100 tmp = sipmsg_find_header(msg, "CSeq"); | |
| 101 if(!tmp) { | |
| 102 // SHOULD NOT HAPPEN | |
| 103 msg->method = 0; | |
| 104 } else { | |
| 105 parts = g_strsplit(tmp, " ", 2); | |
| 106 msg->method = g_strdup(parts[1]); | |
| 107 g_strfreev(parts); | |
| 108 } | |
| 109 } | |
| 110 return msg; | |
| 111 } | |
| 112 | |
| 113 void sipmsg_print(struct sipmsg *msg) { | |
| 114 GSList *cur; | |
| 115 struct siphdrelement *elem; | |
| 116 gaim_debug(GAIM_DEBUG_MISC, "simple", "SIP MSG\n"); | |
| 117 gaim_debug(GAIM_DEBUG_MISC, "simple", "response: %d\nmethod: %s\nbodylen: %d\n",msg->response,msg->method,msg->bodylen); | |
| 118 if(msg->target) gaim_debug(GAIM_DEBUG_MISC, "simple", "target: %s\n",msg->target); | |
| 119 cur = msg->headers; | |
| 120 while(cur) { | |
| 121 elem = cur->data; | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
122 gaim_debug(GAIM_DEBUG_MISC, "simple", "name: %s value: %s\n",elem->name, elem->value); |
| 11181 | 123 cur = g_slist_next(cur); |
| 124 } | |
| 125 } | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
126 |
| 11181 | 127 char *sipmsg_to_string(struct sipmsg *msg) { |
| 128 gchar *out; | |
| 129 gchar *old; | |
| 130 GSList *cur; | |
| 131 struct siphdrelement *elem; | |
| 11483 | 132 if(msg->response) out = g_strdup_printf("SIP/2.0 %d Unknown\r\n", msg->response); |
| 11181 | 133 else out = g_strdup_printf("%s %s SIP/2.0\r\n",msg->method, msg->target); |
| 134 cur = msg->headers; | |
| 135 while(cur) { | |
| 136 elem = cur->data; | |
| 137 old = out; | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
138 out = g_strdup_printf("%s%s: %s\r\n", out, elem->name, elem->value); |
| 11181 | 139 g_free(old); |
| 140 cur = g_slist_next(cur); | |
| 141 } | |
| 142 old = out; | |
| 143 out = g_strdup_printf("%s\r\n",out); | |
| 144 g_free(old); | |
| 145 | |
| 146 if(msg->bodylen) { | |
| 147 old = out; | |
| 148 out = g_strdup_printf("%s%s",out, msg->body); | |
| 149 g_free(old); | |
| 150 } | |
| 151 return out; | |
| 152 } | |
| 153 void sipmsg_add_header(struct sipmsg *msg, gchar *name, gchar *value) { | |
| 154 struct siphdrelement *element = g_new0(struct siphdrelement,1); | |
| 155 element->name = g_strdup(name); | |
| 156 element->value = g_strdup(value); | |
| 157 msg->headers = g_slist_append(msg->headers, element); | |
| 158 } | |
|
11439
617e67e1c985
[gaim-migrate @ 13676]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11398
diff
changeset
|
159 |
| 11181 | 160 void sipmsg_free(struct sipmsg *msg) { |
| 161 struct siphdrelement *elem; | |
| 162 while(msg->headers) { | |
| 163 elem = msg->headers->data; | |
| 164 msg->headers = g_slist_remove(msg->headers,elem); | |
| 165 g_free(elem->name); | |
| 166 g_free(elem->value); | |
| 167 g_free(elem); | |
| 168 } | |
| 169 if(msg->method) g_free(msg->method); | |
| 170 if(msg->target) g_free(msg->target); | |
| 171 if(msg->body) g_free(msg->body); | |
| 172 g_free(msg); | |
| 173 } | |
| 174 | |
| 175 void sipmsg_remove_header(struct sipmsg *msg, gchar *name) { | |
| 176 struct siphdrelement *elem; | |
| 177 GSList *tmp = msg->headers; | |
| 178 while(tmp) { | |
| 179 elem = tmp->data; | |
| 180 if(strcmp(elem->name, name)==0) { | |
| 181 msg->headers = g_slist_remove(msg->headers, elem); | |
| 182 return; | |
| 183 } | |
| 184 tmp = g_slist_next(tmp); | |
| 185 } | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 gchar *sipmsg_find_header(struct sipmsg *msg, gchar *name) { | |
| 190 GSList *tmp; | |
| 191 struct siphdrelement *elem; | |
| 192 tmp = msg->headers; | |
| 193 while(tmp) { | |
| 194 elem = tmp->data; | |
| 195 if(strcmp(elem->name,name)==0) { | |
| 196 return elem->value; | |
| 197 } | |
| 198 tmp = g_slist_next(tmp); | |
| 199 } | |
| 200 return NULL; | |
| 201 } | |
| 202 |
