Mercurial > pidgin
annotate src/xmlnode.h @ 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 | b7af9100af6c |
| children | 02833a0ae716 |
| rev | line source |
|---|---|
| 7131 | 1 /** |
| 2 * @file xmlnode.h XML DOM functions | |
| 10327 | 3 * @ingroup core |
| 7131 | 4 * |
| 5 * gaim | |
| 6 * | |
| 8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 * source distribution. | |
| 7131 | 10 * |
| 11 * This program is free software; you can redistribute it and/or modify | |
| 12 * it under the terms of the GNU General Public License as published by | |
| 13 * the Free Software Foundation; either version 2 of the License, or | |
| 14 * (at your option) any later version. | |
| 15 * | |
| 16 * This program is distributed in the hope that it will be useful, | |
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 * GNU General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU General Public License | |
| 22 * along with this program; if not, write to the Free Software | |
| 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 24 */ | |
| 25 #ifndef _GAIM_XMLNODE_H_ | |
| 26 #define _GAIM_XMLNODE_H_ | |
| 27 | |
| 10327 | 28 /** |
| 29 * The valid types for an xmlnode | |
| 30 */ | |
| 8135 | 31 typedef enum _XMLNodeType |
| 7131 | 32 { |
| 10327 | 33 XMLNODE_TYPE_TAG, /**< Just a tag */ |
| 34 XMLNODE_TYPE_ATTRIB, /**< Has attributes */ | |
| 35 XMLNODE_TYPE_DATA /**< Has data */ | |
| 8135 | 36 } XMLNodeType; |
| 7131 | 37 |
| 10327 | 38 /** |
| 39 * An xmlnode. | |
| 40 */ | |
| 7131 | 41 typedef struct _xmlnode |
| 42 { | |
| 10327 | 43 char *name; /**< The name of the node. */ |
| 44 XMLNodeType type; /**< The type of the node. */ | |
| 45 char *data; /**< The data for the node. */ | |
| 46 size_t data_sz; /**< The size of the data. */ | |
| 47 struct _xmlnode *parent; /**< The parent node or @c NULL.*/ | |
| 48 struct _xmlnode *child; /**< The child node or @c NULL.*/ | |
| 49 struct _xmlnode *next; /**< The next node or @c NULL. */ | |
| 7131 | 50 } xmlnode; |
| 51 | |
| 10327 | 52 /** |
| 53 * Creates a new xmlnode. | |
| 54 * | |
| 55 * @param name The name of the node. | |
| 56 * | |
| 57 * @return The new node. | |
| 58 */ | |
| 7131 | 59 xmlnode *xmlnode_new(const char *name); |
| 10327 | 60 |
| 61 /** | |
| 62 * Creates a new xmlnode child. | |
| 63 * | |
| 64 * @param parent The parent node. | |
| 65 * @param name The name of the child node. | |
| 66 * | |
| 67 * @return The new child node. | |
| 68 */ | |
| 7131 | 69 xmlnode *xmlnode_new_child(xmlnode *parent, const char *name); |
| 10327 | 70 |
| 71 /** | |
| 72 * Inserts a node into a node as a child. | |
| 73 * | |
| 74 * @param parent The parent node to insert child into. | |
| 75 * @param child The child node to insert into parent. | |
| 76 */ | |
| 7131 | 77 void xmlnode_insert_child(xmlnode *parent, xmlnode *child); |
| 10327 | 78 |
| 79 /** | |
| 80 * Gets a child node named name. | |
| 81 * | |
| 82 * @param parent The parent node. | |
| 83 * @param name The child's name. | |
| 84 * | |
| 85 * @return The child or NULL. | |
| 86 */ | |
| 10736 | 87 xmlnode *xmlnode_get_child(const xmlnode *parent, const char *name); |
| 10327 | 88 |
| 89 /** | |
| 90 * Gets a child node named name in a namespace. | |
| 91 * | |
| 92 * @param parent The parent node. | |
| 93 * @param name The child's name. | |
| 94 * @param xmlns The namespace. | |
| 95 * | |
| 96 * @return The child or NULL. | |
| 97 */ | |
| 10736 | 98 xmlnode *xmlnode_get_child_with_namespace(const xmlnode *parent, const char *name, const char *xmlns); |
| 10327 | 99 |
| 100 /** | |
| 101 * Gets the next node with the same name as node. | |
| 102 * | |
| 103 * @param node The node of a twin to find. | |
| 104 * | |
| 105 * @return The twin of node or NULL. | |
| 106 */ | |
| 8135 | 107 xmlnode *xmlnode_get_next_twin(xmlnode *node); |
| 10327 | 108 |
| 109 /** | |
| 110 * Inserts data into a node. | |
| 111 * | |
| 10415 | 112 * @param node The node to insert data into. |
| 10327 | 113 * @param data The data to insert. |
| 10415 | 114 * @param size The size of the data to insert. If data is |
| 115 * null-terminated you can pass in -1. | |
| 10327 | 116 */ |
| 10848 | 117 void xmlnode_insert_data(xmlnode *node, const char *data, gssize size); |
| 10327 | 118 |
| 119 /** | |
| 120 * Gets data from a node. | |
| 121 * | |
| 122 * @param node The node to get data from. | |
| 123 * | |
| 124 * @return The data from the node. | |
| 125 */ | |
| 7131 | 126 char *xmlnode_get_data(xmlnode *node); |
| 10327 | 127 |
| 128 /** | |
| 129 * Sets an attribute for a node. | |
| 130 * | |
| 131 * @param node The node to set an attribute for. | |
| 132 * @param attr The name of the attribute. | |
| 133 * @param value The value of the attribute. | |
| 134 */ | |
| 7131 | 135 void xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value); |
| 10327 | 136 |
| 137 /** | |
| 138 * Gets an attribute from a node. | |
| 139 * | |
| 140 * @param node The node to get an attribute from. | |
| 141 * @param attr The attribute to get. | |
| 142 * | |
| 143 * @return The value of the attribute. | |
| 144 */ | |
| 7131 | 145 const char *xmlnode_get_attrib(xmlnode *node, const char *attr); |
| 10327 | 146 |
| 147 /** | |
| 148 * Removes an attribute from a node. | |
| 149 * | |
| 150 * @param node The node to remove an attribute from. | |
| 151 * @param attr The attribute to remove. | |
| 152 */ | |
| 7131 | 153 void xmlnode_remove_attrib(xmlnode *node, const char *attr); |
| 10327 | 154 |
| 155 /** | |
| 156 * Returns the node in a string of xml. | |
| 157 * | |
| 158 * @param node The starting node to output. | |
| 159 * @param len Address for the size of the string. | |
| 160 * | |
| 10415 | 161 * @return The node repersented as a string. You must |
| 162 * g_free this string when finished using it. | |
| 10327 | 163 */ |
| 10425 | 164 char *xmlnode_to_str(xmlnode *node, int *len); |
| 10327 | 165 |
| 166 /** | |
| 167 * Returns the node in a string of human readable xml. | |
| 168 * | |
| 169 * @param node The starting node to output. | |
| 170 * @param len Address for the size of the string. | |
| 171 * | |
| 172 * @return The node as human readable string including | |
| 10415 | 173 * tab and new line characters. You must |
| 174 * g_free this string when finished using it. | |
| 10327 | 175 */ |
| 10425 | 176 char *xmlnode_to_formatted_str(xmlnode *node, int *len); |
| 10327 | 177 |
| 178 /** | |
| 10337 | 179 * Creates a node from a string of XML. Calling this on the |
| 180 * root node of an XML document will parse the entire document | |
| 181 * into a tree of nodes, and return the xmlnode of the root. | |
| 10327 | 182 * |
| 183 * @param str The string of xml. | |
|
11707
b7af9100af6c
[gaim-migrate @ 13998]
Richard Laager <rlaager@wiktel.com>
parents:
10848
diff
changeset
|
184 * @param size The size of the string, or -1 if @a str is |
|
b7af9100af6c
[gaim-migrate @ 13998]
Richard Laager <rlaager@wiktel.com>
parents:
10848
diff
changeset
|
185 * NUL-terminated. |
| 10327 | 186 * |
| 187 * @return The new node. | |
| 188 */ | |
| 10848 | 189 xmlnode *xmlnode_from_str(const char *str, gssize size); |
| 10327 | 190 |
| 191 /** | |
| 192 * Creates a new node from the source node. | |
| 193 * | |
| 194 * @param src The node to copy. | |
| 195 * | |
| 196 * @return A new copy of the src node. | |
| 197 */ | |
| 8167 | 198 xmlnode *xmlnode_copy(xmlnode *src); |
| 7131 | 199 |
| 10327 | 200 /** |
| 201 * Frees a node and all of it's children. | |
| 202 * | |
| 203 * @param node The node to free. | |
| 204 */ | |
| 7131 | 205 void xmlnode_free(xmlnode *node); |
| 206 | |
| 207 #endif /* _GAIM_XMLNODE_H_ */ |
