summaryrefslogtreecommitdiff
path: root/src/utils/resource_mgr.c
blob: ccaf015e3f199f5cbd12000517c06b6f21a6d9ec (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
/*
 * This file is part of libdom.
 * Licensed under the MIT License,
 *			http://www.opensource.org/licenses/mit-license.php
 * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
 */

#include "resource_mgr.h"

#include <string.h>
#include <assert.h>

#include <libwapcaplet/libwapcaplet.h>
#include "core/string.h"

#include "utils/utils.h"

/**
 * Allocate some memory with this allocator
 *
 * \param res   The resource manager
 * \param size  The size of memory to allocate
 * \return the allocated memory pointer.
 */
void *_dom_resource_mgr_alloc(struct dom_resource_mgr *res, void *ptr, 
		size_t size)
{
	return res->alloc(ptr, size, res->pw);
}

/**
 * Create a dom_string using this resource manager
 *
 * \param res     The resource manager
 * \param data    The data pointer
 * \param len     The length of data
 * \param result  The returned dom_string
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_resource_mgr_create_string(struct dom_resource_mgr *res,
		const uint8_t *data, size_t len, dom_string **result)
{
	return dom_string_create(res->alloc, res->pw, data, len, result);
}

/**
 * Create a lwc_string using this resource manager
 *
 * \param res     The resource manager
 * \param data    The data pointer
 * \param len     The length of the data
 * \param result  The returned lwc_string
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_resource_mgr_create_lwcstring(struct dom_resource_mgr *res,
		const uint8_t *data, size_t len, struct lwc_string_s **result)
{
	lwc_error lerr;

	UNUSED(res);

	lerr = lwc_intern_string((const char *) data, len, result);
	
	return _dom_exception_from_lwc_error(lerr);
}

/**
 * Create a dom_string from a lwc_string using this resource manager
 *
 * \param res     The resource manager
 * \param str     The dom_string to intern
 * \param result  The returned lwc_string
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_resource_mgr_create_string_from_lwcstring(
		struct dom_resource_mgr *res, struct lwc_string_s *str, 
		dom_string **result)
{
	return _dom_string_create_from_lwcstring(res->alloc, res->pw, 
				str, result);
}

/**
 * Create a hash table using this resource manager
 * 
 * \param res     The resource manager
 * \param chains  The number of buckets of the hash table
 * \param f       The hash function 
 * \param ht      The returned hash table
 * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
 */
dom_exception _dom_resource_mgr_create_hashtable(struct dom_resource_mgr *res,
		size_t chains, dom_hash_func f, struct dom_hash_table **ht)
{
	struct dom_hash_table *ret;

	ret = _dom_hash_create(chains, f, res->alloc, res->pw);
	if (ret == NULL)
		return DOM_NO_MEM_ERR;
	
	*ht = ret;
	return DOM_NO_ERR;
}