summaryrefslogtreecommitdiff
path: root/src/hdr_idx.c
blob: a8c595d0e67e913fa2557a036bc00fe4e82b0782 (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
/*
 * Header indexation functions.
 *
 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

#include <common/config.h>
#include <proto/hdr_idx.h>


/*
 * Add a header entry to <list> after element <after>. <after> is ignored when
 * the list is empty or full. Common usage is to set <after> to list->tail.
 *
 * Returns the position of the new entry in the list (from 1 to size-1), or 0
 * if the array is already full. An effort is made to fill the array linearly,
 * but once the last entry has been used, we have to search for unused blocks,
 * which takes much more time. For this reason, it's important to size is
 * appropriately.
 */
int hdr_idx_add(int len, int cr, struct hdr_idx *list, int after)
{
	register struct hdr_idx_elem e = { .len=0, .cr=0, .next=0};
	int new;

	e.len = len;
	e.cr = cr;

	if (list->used == list->size) {
		/* list is full */
		return -1;
	}


	if (list->last < list->size) {
		/* list is not completely used, we can fill linearly */
		new = list->last++;
	} else {
		/* That's the worst situation :
		 * we have to scan the list for holes. We know that we
		 * will find a place because the list is not full.
		 */
		new = 1;
		while (list->v[new].len)
			new++;
	}
	
	/* insert the new element between <after> and the next one (or end) */
	e.next = list->v[after].next;
	list->v[after].next = new;

	list->used++;
	list->v[new] = e;
	list->tail = new;
	return new;
}


/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 * End:
 */