Commit d55ed544 authored by Tim Janik's avatar Tim Janik Committed by Tim Janik

minor optimizations: (g_list_append): `if' optimized for common code path,

Mon Dec 15 19:33:58 1997  Tim Janik  <timj@psynet.net>

        * glist.c: minor optimizations:
        (g_list_append): `if' optimized for common code path, commented out
        unneccessary `assert', saved one variable assignment.
        (g_list_prepend): saved two (conditioned) variable assignment.
        (g_list_insert): saved one  (conditioned) variable assignment,
        saved one variable assignment.
        (g_list_remove): `if' optimized for common code path, saved two
        variable assignments by using `g_list_free_1' (which is even
        faster) instead of `g_list_free'.
        (g_list_reverse): saved allocation of one variable, saved one
        variable assignment.
parent 4d724c25
Mon Dec 15 19:33:58 1997 Tim Janik <timj@psynet.net>
* glist.c: minor optimizations:
(g_list_append): `if' optimized for common code path, commented out
unneccessary `assert', saved one variable assignment.
(g_list_prepend): saved two (conditioned) variable assignment.
(g_list_insert): saved one (conditioned) variable assignment,
saved one variable assignment.
(g_list_remove): `if' optimized for common code path, saved two
variable assignments by using `g_list_free_1' (which is even
faster) instead of `g_list_free'.
(g_list_reverse): saved allocation of one variable, saved one
variable assignment.
Wed Dec 10 23:27:20 1997 Tim Janik <timj@psynet.net> Wed Dec 10 23:27:20 1997 Tim Janik <timj@psynet.net>
* glib_pre1.h: * glib_pre1.h:
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details. * Library General Public License for more details.
* *
* You should have received a copy of the GNU Library General Public * You should have received a copy of the GNU Library General Public
...@@ -23,7 +23,7 @@ typedef struct _GRealListAllocator GRealListAllocator; ...@@ -23,7 +23,7 @@ typedef struct _GRealListAllocator GRealListAllocator;
struct _GRealListAllocator struct _GRealListAllocator
{ {
GMemChunk *list_mem_chunk; GMemChunk *list_mem_chunk;
GList *free_list; GList *free_list;
}; };
...@@ -35,10 +35,10 @@ GListAllocator* ...@@ -35,10 +35,10 @@ GListAllocator*
g_list_allocator_new () g_list_allocator_new ()
{ {
GRealListAllocator* allocator = g_new (GRealListAllocator, 1); GRealListAllocator* allocator = g_new (GRealListAllocator, 1);
allocator->list_mem_chunk = NULL; allocator->list_mem_chunk = NULL;
allocator->free_list = NULL; allocator->free_list = NULL;
return (GListAllocator*) allocator; return (GListAllocator*) allocator;
} }
...@@ -46,7 +46,7 @@ void ...@@ -46,7 +46,7 @@ void
g_list_allocator_free (GListAllocator* fallocator) g_list_allocator_free (GListAllocator* fallocator)
{ {
GRealListAllocator* allocator = (GRealListAllocator *) fallocator; GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
if (allocator && allocator->list_mem_chunk) if (allocator && allocator->list_mem_chunk)
g_mem_chunk_destroy (allocator->list_mem_chunk); g_mem_chunk_destroy (allocator->list_mem_chunk);
if (allocator) if (allocator)
...@@ -58,7 +58,7 @@ g_list_set_allocator (GListAllocator* fallocator) ...@@ -58,7 +58,7 @@ g_list_set_allocator (GListAllocator* fallocator)
{ {
GRealListAllocator* allocator = (GRealListAllocator *) fallocator; GRealListAllocator* allocator = (GRealListAllocator *) fallocator;
GRealListAllocator* old_allocator = current_allocator; GRealListAllocator* old_allocator = current_allocator;
if (allocator) if (allocator)
current_allocator = allocator; current_allocator = allocator;
else else
...@@ -67,13 +67,13 @@ g_list_set_allocator (GListAllocator* fallocator) ...@@ -67,13 +67,13 @@ g_list_set_allocator (GListAllocator* fallocator)
default_allocator = (GRealListAllocator*) g_list_allocator_new (); default_allocator = (GRealListAllocator*) g_list_allocator_new ();
current_allocator = default_allocator; current_allocator = default_allocator;
} }
if (!current_allocator->list_mem_chunk) if (!current_allocator->list_mem_chunk)
current_allocator->list_mem_chunk = g_mem_chunk_new ("list mem chunk", current_allocator->list_mem_chunk = g_mem_chunk_new ("list mem chunk",
sizeof (GList), sizeof (GList),
1024, 1024,
G_ALLOC_ONLY); G_ALLOC_ONLY);
return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator); return (GListAllocator*) (old_allocator == default_allocator ? NULL : old_allocator);
} }
...@@ -82,7 +82,7 @@ GList* ...@@ -82,7 +82,7 @@ GList*
g_list_alloc () g_list_alloc ()
{ {
GList *new_list; GList *new_list;
g_list_set_allocator (NULL); g_list_set_allocator (NULL);
if (current_allocator->free_list) if (current_allocator->free_list)
{ {
...@@ -93,11 +93,11 @@ g_list_alloc () ...@@ -93,11 +93,11 @@ g_list_alloc ()
{ {
new_list = g_chunk_new (GList, current_allocator->list_mem_chunk); new_list = g_chunk_new (GList, current_allocator->list_mem_chunk);
} }
new_list->data = NULL; new_list->data = NULL;
new_list->next = NULL; new_list->next = NULL;
new_list->prev = NULL; new_list->prev = NULL;
return new_list; return new_list;
} }
...@@ -105,7 +105,7 @@ void ...@@ -105,7 +105,7 @@ void
g_list_free (GList *list) g_list_free (GList *list)
{ {
GList *last; GList *last;
if (list) if (list)
{ {
last = g_list_last (list); last = g_list_last (list);
...@@ -125,88 +125,90 @@ g_list_free_1 (GList *list) ...@@ -125,88 +125,90 @@ g_list_free_1 (GList *list)
} }
GList* GList*
g_list_append (GList *list, g_list_append (GList *list,
gpointer data) gpointer data)
{ {
GList *new_list; GList *new_list;
GList *last; GList *last;
new_list = g_list_alloc (); new_list = g_list_alloc ();
new_list->data = data; new_list->data = data;
if (!list) if (list)
{
list = new_list;
}
else
{ {
last = g_list_last (list); last = g_list_last (list);
g_assert (last != NULL); /* g_assert (last != NULL); */
last->next = new_list; last->next = new_list;
new_list->prev = last; new_list->prev = last;
}
return list; return list;
}
else
return new_list;
} }
GList* GList*
g_list_prepend (GList *list, g_list_prepend (GList *list,
gpointer data) gpointer data)
{ {
GList *new_list; GList *new_list;
new_list = g_list_alloc (); new_list = g_list_alloc ();
new_list->data = data; new_list->data = data;
if (list) if (list)
{ {
if (list->prev) if (list->prev)
list->prev->next = new_list; {
new_list->prev = list->prev; list->prev->next = new_list;
new_list->prev = list->prev;
}
list->prev = new_list; list->prev = new_list;
new_list->next = list;
} }
new_list->next = list;
return new_list; return new_list;
} }
GList* GList*
g_list_insert (GList *list, g_list_insert (GList *list,
gpointer data, gpointer data,
gint position) gint position)
{ {
GList *new_list; GList *new_list;
GList *tmp_list; GList *tmp_list;
if (position < 0) if (position < 0)
return g_list_append (list, data); return g_list_append (list, data);
else if (position == 0) else if (position == 0)
return g_list_prepend (list, data); return g_list_prepend (list, data);
tmp_list = g_list_nth (list, position); tmp_list = g_list_nth (list, position);
if (!tmp_list) if (!tmp_list)
return g_list_append (list, data); return g_list_append (list, data);
new_list = g_list_alloc (); new_list = g_list_alloc ();
new_list->data = data; new_list->data = data;
if (tmp_list->prev) if (tmp_list->prev)
tmp_list->prev->next = new_list; {
tmp_list->prev->next = new_list;
new_list->prev = tmp_list->prev;
}
new_list->next = tmp_list; new_list->next = tmp_list;
new_list->prev = tmp_list->prev;
tmp_list->prev = new_list; tmp_list->prev = new_list;
if (tmp_list == list) if (tmp_list == list)
list = new_list; return new_list;
else
return list; return list;
} }
GList * GList *
g_list_concat (GList *list1, GList *list2) g_list_concat (GList *list1, GList *list2)
{ {
GList *tmp_list; GList *tmp_list;
if (list2) if (list2)
{ {
tmp_list = g_list_last (list1); tmp_list = g_list_last (list1);
...@@ -216,37 +218,35 @@ g_list_concat (GList *list1, GList *list2) ...@@ -216,37 +218,35 @@ g_list_concat (GList *list1, GList *list2)
list1 = list2; list1 = list2;
list2->prev = tmp_list; list2->prev = tmp_list;
} }
return list1; return list1;
} }
GList* GList*
g_list_remove (GList *list, g_list_remove (GList *list,
gpointer data) gpointer data)
{ {
GList *tmp; GList *tmp;
tmp = list; tmp = list;
while (tmp) while (tmp)
{ {
if (tmp->data == data) if (tmp->data != data)
tmp = tmp->next;
else
{ {
if (tmp->prev) if (tmp->prev)
tmp->prev->next = tmp->next; tmp->prev->next = tmp->next;
if (tmp->next) if (tmp->next)
tmp->next->prev = tmp->prev; tmp->next->prev = tmp->prev;
if (list == tmp) if (list == tmp)
list = list->next; list = list->next;
tmp->next = NULL; g_list_free_1 (tmp);
tmp->prev = NULL;
g_list_free (tmp);
break; break;
} }
tmp = tmp->next;
} }
return list; return list;
} }
...@@ -261,33 +261,31 @@ g_list_remove_link (GList *list, ...@@ -261,33 +261,31 @@ g_list_remove_link (GList *list,
link->prev->next = link->next; link->prev->next = link->next;
if (link->next) if (link->next)
link->next->prev = link->prev; link->next->prev = link->prev;
if (link == list) if (link == list)
list = list->next; list = list->next;
link->next = NULL; link->next = NULL;
link->prev = NULL; link->prev = NULL;
} }
return list; return list;
} }
GList* GList*
g_list_reverse (GList *list) g_list_reverse (GList *list)
{ {
GList *tmp;
GList *last; GList *last;
last = NULL; last = NULL;
while (list) while (list)
{ {
last = list; last = list;
tmp = list->next; list = last->next;
list->next = list->prev; last->next = last->prev;
list->prev = tmp; last->prev = list;
list = tmp;
} }
return last; return last;
} }
...@@ -297,7 +295,7 @@ g_list_nth (GList *list, ...@@ -297,7 +295,7 @@ g_list_nth (GList *list,
{ {
while ((n-- > 0) && list) while ((n-- > 0) && list)
list = list->next; list = list->next;
return list; return list;
} }
...@@ -311,7 +309,7 @@ g_list_find (GList *list, ...@@ -311,7 +309,7 @@ g_list_find (GList *list,
break; break;
list = list->next; list = list->next;
} }
return list; return list;
} }
...@@ -323,7 +321,7 @@ g_list_last (GList *list) ...@@ -323,7 +321,7 @@ g_list_last (GList *list)
while (list->next) while (list->next)
list = list->next; list = list->next;
} }
return list; return list;
} }
...@@ -333,9 +331,9 @@ g_list_first (GList *list) ...@@ -333,9 +331,9 @@ g_list_first (GList *list)
if (list) if (list)
{ {
while (list->prev) while (list->prev)
list = list->prev; list = list->prev;
} }
return list; return list;
} }
...@@ -343,20 +341,20 @@ gint ...@@ -343,20 +341,20 @@ gint
g_list_length (GList *list) g_list_length (GList *list)
{ {
gint length; gint length;
length = 0; length = 0;
while (list) while (list)
{ {
length++; length++;
list = list->next; list = list->next;
} }
return length; return length;
} }
void void
g_list_foreach (GList *list, g_list_foreach (GList *list,
GFunc func, GFunc func,
gpointer user_data) gpointer user_data)
{ {
while (list) while (list)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment