atom: don't malloc every node separately Instead of having a darray of pointers to malloc'ed atom_node's, make it a darray of atom_node's directly. This makes the code a bit simpler, saves on some malloc's, and the memory gain/loss even out. Unfortunately, we are no longer Three Star Programmers ;( http://c2.com/cgi/wiki?ThreeStarProgrammer Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
diff --git a/src/atom.c b/src/atom.c
index c56eb92..044f566 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -74,15 +74,15 @@
#include "atom.h"
struct atom_node {
- struct atom_node *left, *right;
+ xkb_atom_t left, right;
xkb_atom_t atom;
unsigned int fingerprint;
char *string;
};
struct atom_table {
- struct atom_node *root;
- darray(struct atom_node *) table;
+ xkb_atom_t root;
+ darray(struct atom_node) table;
};
struct atom_table *
@@ -95,31 +95,22 @@ atom_table_new(void)
return NULL;
darray_init(table->table);
- darray_growalloc(table->table, 100);
- darray_append(table->table, NULL);
+ /* The original throw-away root is here, at the illegal atom 0. */
+ darray_resize0(table->table, 1);
return table;
}
-static void
-free_atom(struct atom_node *patom)
-{
- if (!patom)
- return;
-
- free_atom(patom->left);
- free_atom(patom->right);
- free(patom->string);
- free(patom);
-}
-
void
atom_table_free(struct atom_table *table)
{
+ struct atom_node *node;
+
if (!table)
return;
- free_atom(table->root);
+ darray_foreach(node, table->table)
+ free(node->string);
darray_free(table->table);
free(table);
}
@@ -127,18 +118,17 @@ atom_table_free(struct atom_table *table)
const char *
atom_text(struct atom_table *table, xkb_atom_t atom)
{
- if (atom >= darray_size(table->table) ||
- darray_item(table->table, atom) == NULL)
+ if (atom == XKB_ATOM_NONE || atom >= darray_size(table->table))
return NULL;
- return darray_item(table->table, atom)->string;
+ return darray_item(table->table, atom).string;
}
static bool
-find_node_pointer(struct atom_table *table, const char *string, size_t len,
- struct atom_node ***nodep_out, unsigned int *fingerprint_out)
+find_atom_pointer(struct atom_table *table, const char *string, size_t len,
+ xkb_atom_t **atomp_out, unsigned int *fingerprint_out)
{
- struct atom_node **nodep = &table->root;
+ xkb_atom_t *atomp = &table->root;
unsigned int fingerprint = 0;
bool found = false;
@@ -147,21 +137,23 @@ find_node_pointer(struct atom_table *table, const char *string, size_t len,
fingerprint = fingerprint * 27 + string[len - 1 - i];
}
- while (*nodep) {
- if (fingerprint < (*nodep)->fingerprint) {
- nodep = &(*nodep)->left;
+ while (*atomp != XKB_ATOM_NONE) {
+ struct atom_node *node = &darray_item(table->table, *atomp);
+
+ if (fingerprint < node->fingerprint) {
+ atomp = &node->left;
}
- else if (fingerprint > (*nodep)->fingerprint) {
- nodep = &(*nodep)->right;
+ else if (fingerprint > node->fingerprint) {
+ atomp = &node->right;
}
else {
/* Now start testing the strings. */
- const int cmp = strncmp(string, (*nodep)->string, len);
- if (cmp < 0 || (cmp == 0 && len < strlen((*nodep)->string))) {
- nodep = &(*nodep)->left;
+ const int cmp = strncmp(string, node->string, len);
+ if (cmp < 0 || (cmp == 0 && len < strlen(node->string))) {
+ atomp = &node->left;
}
else if (cmp > 0) {
- nodep = &(*nodep)->right;
+ atomp = &node->right;
}
else {
found = true;
@@ -172,23 +164,23 @@ find_node_pointer(struct atom_table *table, const char *string, size_t len,
if (fingerprint_out)
*fingerprint_out = fingerprint;
- if (nodep_out)
- *nodep_out = nodep;
+ if (atomp_out)
+ *atomp_out = atomp;
return found;
}
xkb_atom_t
atom_lookup(struct atom_table *table, const char *string, size_t len)
{
- struct atom_node **nodep;
+ xkb_atom_t *atomp;
if (!string)
return XKB_ATOM_NONE;
- if (!find_node_pointer(table, string, len, &nodep, NULL))
+ if (!find_atom_pointer(table, string, len, &atomp, NULL))
return XKB_ATOM_NONE;
- return (*nodep)->atom;
+ return *atomp;
}
/*
@@ -200,39 +192,34 @@ xkb_atom_t
atom_intern(struct atom_table *table, const char *string, size_t len,
bool steal)
{
- struct atom_node **nodep;
- struct atom_node *node;
+ xkb_atom_t *atomp;
+ struct atom_node node;
unsigned int fingerprint;
if (!string)
return XKB_ATOM_NONE;
- if (find_node_pointer(table, string, len, &nodep, &fingerprint)) {
+ if (find_atom_pointer(table, string, len, &atomp, &fingerprint)) {
if (steal)
free(UNCONSTIFY(string));
- return (*nodep)->atom;
+ return *atomp;
}
- node = malloc(sizeof(*node));
- if (!node)
- return XKB_ATOM_NONE;
-
if (steal) {
- node->string = UNCONSTIFY(string);
+ node.string = UNCONSTIFY(string);
}
else {
- node->string = strndup(string, len);
- if (!node->string) {
- free(node);
+ node.string = strndup(string, len);
+ if (!node.string)
return XKB_ATOM_NONE;
- }
}
- *nodep = node;
- node->left = node->right = NULL;
- node->fingerprint = fingerprint;
- node->atom = darray_size(table->table);
+ node.left = node.right = XKB_ATOM_NONE;
+ node.fingerprint = fingerprint;
+ node.atom = darray_size(table->table);
+ /* Do this before the append, as it may realloc and change the offsets. */
+ *atomp = node.atom;
darray_append(table->table, node);
- return node->atom;
+ return node.atom;
}