blob: aceaeb13ce1ff146360d688c901a2deec69ecc29 [file] [log] [blame]
Willy Tarreaub7eba102006-12-04 02:20:02 +01001/*
2 * Header indexation functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/config.h>
14#include <types/hdr_idx.h>
15
16
17/*
Willy Tarreaub7eba102006-12-04 02:20:02 +010018 * Add a header entry to <list> after element <after>. <after> is ignored when
19 * the list is empty or full. Common usage is to set <after> to list->tail.
20 *
21 * Returns the position of the new entry in the list (from 1 to size-1), or 0
22 * if the array is already full. An effort is made to fill the array linearly,
23 * but once the last entry has been used, we have to search for unused blocks,
24 * which takes much more time. For this reason, it's important to size is
25 * appropriately.
26 */
27int hdr_idx_add(int len, int cr, struct hdr_idx *list, int after)
28{
29 register struct hdr_idx_elem e = { .len=0, .cr=0, .next=0};
30 int new;
31
32 e.len = len;
33 e.cr = cr;
34
35 if (list->used == list->size) {
36 /* list is full */
37 return -1;
38 }
39
40
41 if (list->last < list->size) {
42 /* list is not completely used, we can fill linearly */
43 new = list->last++;
44 } else {
45 /* That's the worst situation :
46 * we have to scan the list for holes. We know that we
47 * will find a place because the list is not full.
48 */
49 new = 1;
50 while (list->v[new].len)
51 new++;
52 }
53
54 /* insert the new element between <after> and the next one (or end) */
55 e.next = list->v[after].next;
56 list->v[after].next = new;
57
58 list->used++;
59 list->v[new] = e;
60 list->tail = new;
61 return new;
62}
63
64
65/*
66 * Local variables:
67 * c-indent-level: 8
68 * c-basic-offset: 8
69 * End:
70 */