Willy Tarreau | b7eba10 | 2006-12-04 02:20:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | /* |
| 18 | * Initialize the list pointers. |
| 19 | * list->size must already be set. If list->size is set and list->v is |
| 20 | * non-null, list->v is also initialized.. |
| 21 | */ |
| 22 | static inline void hdr_idx_init(struct hdr_idx *list) |
| 23 | { |
| 24 | |
| 25 | if (list->size && list->v) { |
| 26 | register struct hdr_idx_elem e = { .len=0, .cr=0, .next=0}; |
| 27 | list->v[0] = e; |
| 28 | } |
| 29 | list->tail = 0; |
| 30 | list->used = list->last = 1; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Add a header entry to <list> after element <after>. <after> is ignored when |
| 35 | * the list is empty or full. Common usage is to set <after> to list->tail. |
| 36 | * |
| 37 | * Returns the position of the new entry in the list (from 1 to size-1), or 0 |
| 38 | * if the array is already full. An effort is made to fill the array linearly, |
| 39 | * but once the last entry has been used, we have to search for unused blocks, |
| 40 | * which takes much more time. For this reason, it's important to size is |
| 41 | * appropriately. |
| 42 | */ |
| 43 | int hdr_idx_add(int len, int cr, struct hdr_idx *list, int after) |
| 44 | { |
| 45 | register struct hdr_idx_elem e = { .len=0, .cr=0, .next=0}; |
| 46 | int new; |
| 47 | |
| 48 | e.len = len; |
| 49 | e.cr = cr; |
| 50 | |
| 51 | if (list->used == list->size) { |
| 52 | /* list is full */ |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | if (list->last < list->size) { |
| 58 | /* list is not completely used, we can fill linearly */ |
| 59 | new = list->last++; |
| 60 | } else { |
| 61 | /* That's the worst situation : |
| 62 | * we have to scan the list for holes. We know that we |
| 63 | * will find a place because the list is not full. |
| 64 | */ |
| 65 | new = 1; |
| 66 | while (list->v[new].len) |
| 67 | new++; |
| 68 | } |
| 69 | |
| 70 | /* insert the new element between <after> and the next one (or end) */ |
| 71 | e.next = list->v[after].next; |
| 72 | list->v[after].next = new; |
| 73 | |
| 74 | list->used++; |
| 75 | list->v[new] = e; |
| 76 | list->tail = new; |
| 77 | return new; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* |
| 82 | * Local variables: |
| 83 | * c-indent-level: 8 |
| 84 | * c-basic-offset: 8 |
| 85 | * End: |
| 86 | */ |