2 * mono-mlist.c: Managed object list implementation
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2006-2009 Novell, Inc (http://www.novell.com)
10 #include "mono/metadata/mono-mlist.h"
11 #include "mono/metadata/appdomain.h"
12 #include "mono/metadata/class-internals.h"
13 #include "mono/metadata/object-internals.h"
15 /* matches the System.MonoListItem object*/
23 * note: we only allocate in the root domain: this lists are
24 * not exposed to managed code
26 static MonoVTable *monolist_item_vtable = NULL;
30 * @data: object to use as data
32 * Allocates a new managed list node with @data as the contents.
33 * A managed list node also represents a singly-linked list.
34 * Managed lists are garbage collected, so there is no free routine
35 * and the user is required to keep references to the managed list
36 * to prevent it from being garbage collected.
39 mono_mlist_alloc (MonoObject *data)
43 if (!monolist_item_vtable) {
44 MonoClass *klass = mono_class_load_from_name (mono_defaults.corlib, "System", "MonoListItem");
45 monolist_item_vtable = mono_class_vtable (mono_get_root_domain (), klass);
46 g_assert (monolist_item_vtable);
48 res = (MonoMList*)mono_object_new_fast_checked (monolist_item_vtable, &error);
49 mono_error_raise_exception (&error);
50 MONO_OBJECT_SETREF (res, data, data);
55 * mono_mlist_get_data:
56 * @list: the managed list node
58 * Get the object stored in the list node @list.
61 mono_mlist_get_data (MonoMList* list)
67 * mono_mlist_set_data:
68 * @list: the managed list node
70 * Set the object content in the list node @list.
73 mono_mlist_set_data (MonoMList* list, MonoObject *data)
75 MONO_OBJECT_SETREF (list, data, data);
79 * mono_mlist_set_next:
80 * @list: a managed list node
81 * @next: list node that will be next for the @list node.
83 * Set next node for @list to @next.
86 mono_mlist_set_next (MonoMList* list, MonoMList *next)
91 MONO_OBJECT_SETREF (list, next, next);
97 * @list: the managed list
99 * Get the number of items in the list @list.
100 * Since managed lists are singly-linked, this operation takes O(n) time.
103 mono_mlist_length (MonoMList* list)
115 * @list: the managed list node
117 * Returns the next managed list node starting from @list.
120 mono_mlist_next (MonoMList* list)
127 * @list: the managed list node
129 * Returns the last managed list node in list @list.
130 * Since managed lists are singly-linked, this operation takes O(n) time.
133 mono_mlist_last (MonoMList* list)
144 * mono_mlist_prepend:
145 * @list: the managed list
146 * @data: the object to add to the list
148 * Allocate a new list node with @data as content and prepend it
149 * to the list @list. @list can be NULL.
152 mono_mlist_prepend (MonoMList* list, MonoObject *data)
154 MonoMList* res = mono_mlist_alloc (data);
156 MONO_OBJECT_SETREF (res, next, list);
162 * @list: the managed list
163 * @data: the object to add to the list
165 * Allocate a new list node with @data as content and append it
166 * to the list @list. @list can be NULL.
167 * Since managed lists are singly-linked, this operation takes O(n) time.
170 mono_mlist_append (MonoMList* list, MonoObject *data)
172 MonoMList* res = mono_mlist_alloc (data);
174 MonoMList* last = mono_mlist_last (list);
175 MONO_OBJECT_SETREF (last, next, res);
183 find_prev (MonoMList* list, MonoMList *item)
185 MonoMList* prev = NULL;
196 * mono_mlist_remove_item:
197 * @list: the managed list
198 * @data: the object to remove from the list
200 * Remove the list node @item from the managed list @list.
201 * Since managed lists are singly-linked, this operation can take O(n) time.
204 mono_mlist_remove_item (MonoMList* list, MonoMList *item)
212 prev = find_prev (list, item);
214 MONO_OBJECT_SETREF (prev, next, item->next);