summaryrefslogtreecommitdiff
path: root/src/utils/chunkarray.c
blob: ef3fb60477004911952c8c4920afad24065885fe (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <stdio.h>
#include <string.h>

#include "utils/chunkarray.h"
#include "utils/utils.h"

typedef struct chunk chunk;

struct chunk {
#define CHUNK_SIZE (4096)
	chunk *next;

	uint32_t used;

	uint8_t data[CHUNK_SIZE];
};

struct parserutils_chunkarray {
	chunk *used_chunks;

	chunk *free_chunks;

	parserutils_alloc alloc;
	void *pw;
};

/**
 * Create a chunked array
 *
 * \param alloc  Memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \param array  Pointer to location to receive array instance
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_chunkarray_create(parserutils_alloc alloc, 
		void *pw, parserutils_chunkarray **array)
{
	parserutils_chunkarray *c;

	if (alloc == NULL || array == NULL)
		return PARSERUTILS_BADPARM;

	c = alloc(0, sizeof(parserutils_chunkarray), pw);
	if (c == NULL)
		return PARSERUTILS_NOMEM;

	c->used_chunks = NULL;
	c->free_chunks = NULL;

	c->alloc = alloc;
	c->pw = pw;

	*array = c;

	return PARSERUTILS_OK;
}

/**
 * Destroy a chunked array
 *
 * \param array  The array to destroy
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_chunkarray_destroy(parserutils_chunkarray *array)
{
	chunk *c, *d;

	if (array == NULL)
		return PARSERUTILS_BADPARM;

	for (c = array->used_chunks; c != NULL; c = d) {
		d = c->next;
		array->alloc(c, 0, array->pw);
	}

	for (c = array->free_chunks; c != NULL; c = d) {
		d = c->next;
		array->alloc(c, 0, array->pw);
	}

	array->alloc(array, 0, array->pw);

	return PARSERUTILS_OK;
}

/**
 * Insert an item into an array
 *
 * \param array     The array to insert into
 * \param data      Pointer to data to insert
 * \param len       Length, in bytes, of data
 * \param inserted  Pointer to location to receive pointer to inserted data
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_chunkarray_insert(parserutils_chunkarray *array,
		const void *data, uint16_t len, 
		const parserutils_chunkarray_entry **inserted)
{
	parserutils_chunkarray_entry *entry;
	size_t required_length;

	if (array == NULL || data == NULL || inserted == NULL)
		return PARSERUTILS_BADPARM;

	/* Calculate the required length. 
	 * We require that each entry be an exact multiple of 4. */
	required_length = ALIGN(sizeof(parserutils_chunkarray_entry) + len);

	/* If we're trying to insert data larger than CHUNK_SIZE, then it 
	 * gets its own chunk. */
	if (required_length > CHUNK_SIZE) {
		chunk *c = array->alloc(0, 
				sizeof(chunk) + required_length - CHUNK_SIZE,
				array->pw);
		if (c == NULL)
			return PARSERUTILS_NOMEM;

		/* Populate chunk */
		entry = (parserutils_chunkarray_entry *) (void *) c->data;
		memcpy(entry->data, data, len);
		entry->length = len;
		c->used = required_length;

		/* And put it into the used list */
		c->next = array->used_chunks;
		array->used_chunks = c;
	} else {
		/* Scan the free list until we find a chunk with enough space */
		chunk *c, *prev;

		for (prev = NULL, c = array->free_chunks; c != NULL; 
				prev = c, c = c->next) {
			if (CHUNK_SIZE - c->used >= required_length)
				break;
		}

		if (c == NULL) {
			/* None big enough, create a new one */
			c = array->alloc(0, sizeof(chunk), array->pw);
			if (c == NULL)
				return PARSERUTILS_NOMEM;

			c->used = 0;

			/* Insert it at the head of the free list */
			c->next = array->free_chunks;
			array->free_chunks = c;

			/* And ensure that prev is kept in sync */
			prev = NULL;
		}

		/* Populate chunk */
		entry = (parserutils_chunkarray_entry *) 
				(void *) (c->data + c->used);
		memcpy(entry->data, data, len);
		entry->length = len;
		c->used += required_length;

		/* If we've now filled the chunk, move it to the used list */
		if (c->used == CHUNK_SIZE) {
			if (prev != NULL)
				prev->next = c->next;
			else
				array->free_chunks = c->next;

			c->next = array->used_chunks;
			array->used_chunks = c;
		}
	}

	(*inserted) = entry;

	return PARSERUTILS_OK;
}

/******************************************************************************
 * Private functions                                                          *
 ******************************************************************************/

extern void parserutils_chunkarray_dump(parserutils_chunkarray *array);

/**
 * Dump details of a chunked array to stdout
 *
 * \param array  The array to dump
 */
void parserutils_chunkarray_dump(parserutils_chunkarray *array)
{
	uint32_t n = 0;
	size_t count = 0;
	size_t total = sizeof(parserutils_chunkarray);

	for (chunk *c = array->used_chunks; c != NULL; c = c->next) {
		n++;
		count += c->used;
		if (c->used == CHUNK_SIZE)
			total += sizeof(chunk);
		else
			total += sizeof(chunk) + c->used - CHUNK_SIZE;
	}

	printf("%u full blocks: %zu bytes\n", n, count);

	n = 0;
	count = 0;

	for (chunk *c = array->free_chunks; c != NULL; c = c->next) {
		n++;
		count += c->used;
		total += sizeof(chunk);
	}

	printf("%u partial blocks: %zu bytes (of %u => %f%%)\n", n, count, 
			n * CHUNK_SIZE, 
			(count * 100) / ((float) n * CHUNK_SIZE));

	printf("Total: %zu (%u) (%u)\n",  total, (unsigned int) sizeof(chunk), 
			(unsigned int) sizeof(parserutils_chunkarray));
}