/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is s_free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
//#include "glib.h"
#include "gtypes.h"
#include "glist.h"
#include "add_types.h"
#include "memmgr.h"
#include <s_string.h>
#include "assert.h"
//void g_list_push_allocator (gpointer dummy) { /* present for binary compat only */ }
//void g_list_pop_allocator (void) { /* present for binary compat only */ }
//#define g_list_alloc() g_slice_new (GList)
//#define _g_list_alloc0() g_slice_new0 (GList)
//#define _g_list_free1(list) g_slice_free (GList, list)
GList*
g_list_alloc (void) {
//return _g_list_alloc0 ();
return (GList *)s_malloc(sizeof(GList) );
}
GList*
g_list_alloc0 (void) {
GList *list = (GList *)s_malloc(sizeof(GList) );
s_memset(list, 0, sizeof(*list) );
return list;
}
/**
* g_list_free:
* @list: a #GList
*
* Frees all of the memory used by a #GList.
* The freed elements are returned to the slice allocator.
*
* <note><para>
* If list elements contain dynamically-allocated memory,
* they should be freed first.
* </para></note>
*/
void
g_list_free (GList *list, GFreeFunc func) {
//g_slice_free_chain (GList, list, next);
while (list) {
GList *next = list->next;
assert(list->data);
if (func) func(list->data);
s_free(list);
list = next;
}
assert(NULL == list);
}
/**
* g_list_free_1:
* @list: a #GList element
*
* Frees one #GList element.
* It is usually used after g_list_remove_link().
*/
void
g_list_free1 (GList *list) {
//_g_list_free1 (list);
s_free(list);
}
/**
* g_list_append:
* @list: a pointer to a #GList
* @data: the data for the new element
*
* Adds a new element on to the end of the list.
*
* <note><para>
* The return value is the new start of the list, which
* may have changed, so make sure you store the new value.
* </para></note>
*
* <note><para>
* Note that g_list_append() has to traverse the entire list
* to find the end, which is inefficient when adding multiple
* elements. A common idiom to avoid the inefficiency is to prepend
* the elements and reverse the list when all elements have been added.
* </para></note>
*
* |[
* /* Notice that these are initialized to the empty list. */
* GList *list = NULL, *number_list = NULL;
*
* /* This is a list of strings. */
* list = g_list_append (list, "first");
* list = g_list_append (list, "second");
*
* /* This is a list of integers. */
* number_list = g_list_append (number_list, GINT_TO_POINTER (27));
* number_list = g_list_append (number_list, GINT_TO_POINTER (14));
* ]|
*
* Returns: the new start of the #GList
*/
GList*
g_list_append (GList *list,
gpointer data) {
GList *new_list;
GList *last;
new_list = g_list_alloc ();
new_list->data = data;
new_list->next = NULL;
if (list) {
last = g_list_last (list);
/* g_assert (last != NULL); */
last->next = new_list;
new_list->prev = last;
return list;
} else {
new_list->prev = NULL;
return new_list;
}
}
/**
* g_list_prepend:
* @list: a pointer to a #GList
* @data: the data for the new element
*
* Adds a new element on to the start of the list.
*
* <note><para>
* The return value is the new start of the list, which
* may have changed, so make sure you store the new value.
* </para></note>
*
* |[
* /* Notice that it is initialized to the empty list. */
* GList *list = NULL;
* list = g_list_prepend (list, "last");
* list = g_list_prepend (list, "first");
* ]|
*
* Returns: the new start of the #GList
*/
GList*
g_list_prepend (GList *list,
gpointer data) {
GList *new_list;
new_list = g_list_alloc ();
new_list->data = data;
new_list->next = list;
if (list) {
new_list->prev = list->prev;
if (list->prev)
list->prev->next = new_list;
list->prev = new_list;
} else
new_list->prev = NULL;
return new_list;
}
/**
* g_list_insert:
* @list: a pointer to a #GList
* @data: the data for the new element
* @position: the position to insert the element. If this is
* negative, or is larger than the number of elements in the
* list, the new element is added on to the end of the list.
*
* Inserts a new element into the list at the given position.
*
* Returns: the new start of the #GList
*/
GList*
g_list_insert (GList *list,
gpointer data,
gint position) {
GList *new_list;
GList *tmp_list;
if (position < 0)
return g_list_append (list, data);
else if (position == 0)
return g_list_prepend (list, data);
tmp_list = g_list_nth (list, position);
if (!tmp_list)
return g_list_append (list, data);
new_list = g_list_alloc ();
new_list->data = data;
new_list->prev = tmp_list->prev;
if (tmp_list->prev)
tmp_list->prev->next = new_list;
new_list->next = tmp_list;
tmp_list->prev = new_list;
if (tmp_list == list)
return new_list;
else
return list;
}
/**
* g_list_insert_before:
* @list: a pointer to a #GList
* @sibling: the list element before which the new element
* is inserted or %NULL to insert at the end of the list
* @data: the data for the new element
*
* Inserts a new element into the list before the given position.
*
* Returns: the new start of the #GList
*/
GList*
g_list_insert_before (GList *list,
GList *sibling,
gpointer data) {
if (!list) {
list = g_list_alloc ();
list->data = data;
if (sibling != NULL) return list;
return list;
} else if (sibling) {
GList *node;
node = g_list_alloc ();
node->data = data;
node->prev = sibling->prev;
node->next = sibling;
sibling->prev = node;
if (node->prev) {
node->prev->next = node;
return list;
} else {
if (sibling != list) return node;
return node;
}
} else {
GList *last;
last = list;
while (last->next)
last = last->next;
last->next = g_list_alloc ();
last->next->data = data;
last->next->prev = last;
last->next->next = NULL;
return list;
}
}
/**
* g_list_concat:
* @list1: a #GList
* @list2: the #GList to add to the end of the first #GList
*
* Adds the second #GList onto the end of the first #GList.
* Note that the elements of the second #GList are not copied.
* They are used directly.
*
* Returns: the start of the new #GList
*/
GList *
g_list_concat (GList *list1, GList *list2) {
GList *tmp_list;
if (list2) {
tmp_list = g_list_last (list1);
if (tmp_list)
tmp_list->next = list2;
else
list1 = list2;
list2->prev = tmp_list;
}
return list1;
}
/**
* g_list_remove:
* @list: a #GList
* @data: the data of the element to remove
*
*