summaryrefslogtreecommitdiff
path: root/src/core/impllist.c
blob: 522c3f749290eeecee81d22434841ef0a68db397 (plain)
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
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <dom/bootstrap/implpriv.h>
#include <dom/core/implementation.h>
#include <dom/core/impllist.h>

/**
 * Claim a reference on a DOM implementation list
 *
 * \param list  The list to claim a reference on
 */
void dom_implementation_list_ref(struct dom_implementation_list *list)
{
	list->refcnt++;
}

/**
 * Release a reference from a DOM implementation list
 *
 * \param list  The list to release the reference from
 *
 * If the reference count reaches zero, any memory claimed by the
 * list will be released
 */
void dom_implementation_list_unref(struct dom_implementation_list *list)
{
	struct dom_implementation_list_item *i, *j;

	if (--list->refcnt == 0) {
		/* Destroy all list entries */
		for (i = list->head; i; i = j) {
			j = i->next;

			/* Unreference the implementation */
			dom_implementation_unref(i->impl);

			/* And free the entry */
			list->alloc(i, 0, list->pw);
		}

		/* Free the list object */
		list->alloc(list, 0, list->pw);
	}
}

/**
 * Retrieve the length of a DOM implementation list
 *
 * \param list    The list to retrieve the length of
 * \param length  Pointer to location to receive result
 * \return DOM_NO_ERR.
 */
dom_exception dom_implementation_list_get_length(
		struct dom_implementation_list *list, unsigned long *length)
{
	unsigned long count = 0;
	struct dom_implementation_list_item *i;

	for (i = list->head; i; i = i->next)
		count++;

	*length = count;

	return DOM_NO_ERR;
}

/**
 * Retrieve an item by index from a DOM implementation list
 *
 * \param list   The list to retrieve the item from
 * \param index  The list index to retrieve
 * \param impl   Pointer to location to receive result
 * \return DOM_NO_ERR.
 *
 * ::index is a zero-based index into ::list.
 * ::index lies in the range [0, length-1]
 *
 * The returned implementation will have had its reference count increased.
 * The client should unref the implementation once it has finished with it.
 */
dom_exception dom_implementation_list_item(
		struct dom_implementation_list *list, unsigned long index,
		struct dom_implementation **impl)
{
	unsigned long idx = 0;
	struct dom_implementation_list_item *i;

	for (i = list->head; i; i = i->next) {
		if (idx == index)
			break;

		idx++;
	}

	if (i == NULL) {
		*impl = NULL;
	} else {
		dom_implementation_ref(i->impl);
		*impl = i->impl;
	}

	return DOM_NO_ERR;
}