2 * mono-mlist.c: Managed object list implementation
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2006-2009 Novell, Inc (http://www.novell.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #include "mono/metadata/mono-mlist.h"
12 #include "mono/metadata/appdomain.h"
13 #include "mono/metadata/class-internals.h"
14 #include "mono/metadata/object-internals.h"
16 /* matches the System.MonoListItem object*/
24 * note: we only allocate in the root domain: this lists are
25 * not exposed to managed code
27 static MonoVTable *monolist_item_vtable = NULL;
31 * @data: object to use as data
33 * Allocates a new managed list node with @data as the contents.
34 * A managed list node also represents a singly-linked list.
35 * Managed lists are garbage collected, so there is no free routine
36 * and the user is required to keep references to the managed list
37 * to prevent it from being garbage collected.
40 mono_mlist_alloc (MonoObject *data)
44 if (!monolist_item_vtable) {
45 MonoClass *klass = mono_class_load_from_name (mono_defaults.corlib, "System", "MonoListItem");
46 monolist_item_vtable = mono_class_vtable (mono_get_root_domain (), klass);
47 g_assert (monolist_item_vtable);
49 res = (MonoMList*)mono_object_new_fast_checked (monolist_item_vtable, &error);
50 mono_error_raise_exception (&error);
51 MONO_OBJECT_SETREF (res, data, data);
56 * mono_mlist_get_data:
57 * @list: the managed list node
59 * Get the object stored in the list node @list.
62 mono_mlist_get_data (MonoMList* list)
68 * mono_mlist_set_data:
69 * @list: the managed list node
71 * Set the object content in the list node @list.
74 mono_mlist_set_data (MonoMList* list, MonoObject *data)
76 MONO_OBJECT_SETREF (list, data, data);
80 * mono_mlist_set_next:
81 * @list: a managed list node
82 * @next: list node that will be next for the @list node.
84 * Set next node for @list to @next.
87 mono_mlist_set_next (MonoMList* list, MonoMList *next)
92 MONO_OBJECT_SETREF (list, next, next);
98 * @list: the managed list
100 * Get the number of items in the list @list.
101 * Since managed lists are singly-linked, this operation takes O(n) time.
104 mono_mlist_length (MonoMList* list)
116 * @list: the managed list node
118 * Returns the next managed list node starting from @list.
121 mono_mlist_next (MonoMList* list)
128 * @list: the managed list node
130 * Returns the last managed list node in list @list.
131 * Since managed lists are singly-linked, this operation takes O(n) time.
134 mono_mlist_last (MonoMList* list)
145 * mono_mlist_prepend:
146 * @list: the managed list
147 * @data: the object to add to the list
149 * Allocate a new list node with @data as content and prepend it
150 * to the list @list. @list can be NULL.
153 mono_mlist_prepend (MonoMList* list, MonoObject *data)
155 MonoMList* res = mono_mlist_alloc (data);
157 MONO_OBJECT_SETREF (res, next, list);
163 * @list: the managed list
164 * @data: the object to add to the list
166 * Allocate a new list node with @data as content and append it
167 * to the list @list. @list can be NULL.
168 * Since managed lists are singly-linked, this operation takes O(n) time.
171 mono_mlist_append (MonoMList* list, MonoObject *data)
173 MonoMList* res = mono_mlist_alloc (data);
175 MonoMList* last = mono_mlist_last (list);
176 MONO_OBJECT_SETREF (last, next, res);
184 find_prev (MonoMList* list, MonoMList *item)
186 MonoMList* prev = NULL;
197 * mono_mlist_remove_item:
198 * @list: the managed list
199 * @data: the object to remove from the list
201 * Remove the list node @item from the managed list @list.
202 * Since managed lists are singly-linked, this operation can take O(n) time.
205 mono_mlist_remove_item (MonoMList* list, MonoMList *item)
213 prev = find_prev (list, item);
215 MONO_OBJECT_SETREF (prev, next, item->next);