5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1998 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 /* Optimized buffer managing routines. These are short inline functions. */
29 SilcBuffer is very simple and easy to use, yet you can do to the
30 buffer almost anything you want with its method functions. The buffer
31 is constructed of four different data sections that in whole creates
32 the allocated data area. Following short description of the fields
37 True length of the buffer. This is set at the allocation of the
38 buffer and it should not be touched after that. This field should
39 be considered read-only.
43 Length of the currently valid data area. Tells the length of the
44 data at the buffer. This is set to zero at the allocation of the
45 buffer and should not be updated by hand. Method functions of the
46 buffer automatically updates this field. However, it is not
47 read-only field and can be updated manually if necessary.
51 Head of the allocated buffer. This is the start of the allocated
52 data area and remains as same throughout the lifetime of the buffer.
53 However, the end of the head area or the start of the currently valid
54 data area is variable.
56 --------------------------------
57 | head | data | tail |
58 --------------------------------
61 Current head section in the buffer is sb->data - sb->head.
65 Currently valid data area. This is the start of the currently valid
66 main data area. The data area is variable in all directions.
68 --------------------------------
69 | head | data | tail |
70 --------------------------------
73 Current valid data area in the buffer is sb->tail - sb->data.
77 Tail of the buffer. This is the end of the currently valid data area
78 or start of the tail area. The start of the tail area is variable.
80 --------------------------------
81 | head | data | tail |
82 --------------------------------
85 Current tail section in the buffer is sb->end - sb->tail.
89 End of the allocated buffer. This is the end of the allocated data
90 area and remains as same throughout the lifetime of the buffer.
91 Usually this field is not needed except when checking the size
94 --------------------------------
95 | head | data | tail |
96 --------------------------------
99 Length of the entire buffer is (ie. truelen) sb->end - sb->head.
101 Currently valid data area is considered to be the main data area in
102 the buffer. However, the entire buffer is of course valid data and can
103 be used as such. Usually head section of the buffer includes different
104 kind of headers or similiar. Data section includes the main data of
105 the buffer. Tail section can be seen as a reserve space of the data
106 section. Tail section can be pulled towards end thus the data section
109 This buffer scheme is based on Linux kernel's Socket Buffer, the
110 idea were taken directly from there and credits should go there.
114 typedef struct SilcBufferStruct {
123 typedef SilcBufferObject *SilcBuffer;
127 /* Returns the true length of the buffer. This is used to pull
128 the buffer area to the end of the buffer. */
129 #define SILC_BUFFER_END(x) ((x)->end - (x)->head)
131 /* Inline functions */
134 SilcBuffer silc_buffer_alloc(uint32 len)
138 /* Allocate new SilcBuffer */
139 sb = (SilcBuffer)silc_calloc(1, sizeof(*sb));
141 /* Allocate the actual data area */
142 sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head));
144 /* Set pointers to the new buffer */
148 sb->end = sb->head + sb->truelen;
153 /* Free's a SilcBuffer */
156 void silc_buffer_free(SilcBuffer sb)
159 memset(sb->head, 'F', sb->truelen);
165 /* Pulls current data area towards end. The length of the currently
166 valid data area is also decremented. Returns pointer to the data
170 ---------------------------------
171 | head | data | tail |
172 ---------------------------------
174 Pulls the start of the data area.
176 ---------------------------------
177 | head | data | tail |
178 ---------------------------------
183 unsigned char *silc_buffer_pull(SilcBuffer sb, uint32 len)
185 unsigned char *old_data = sb->data;
188 assert(len <= (sb->tail - sb->data));
197 /* Pushes current data area towards beginning. Length of the currently
198 valid data area is also incremented. Returns a pointer to the
199 data area before pushing.
202 ---------------------------------
203 | head | data | tail |
204 ---------------------------------
206 Pushes the start of the data area.
208 ---------------------------------
209 | head | data | tail |
210 ---------------------------------
215 unsigned char *silc_buffer_push(SilcBuffer sb, uint32 len)
217 unsigned char *old_data = sb->data;
220 assert((sb->data - len) >= sb->head);
229 /* Pulls current tail section towards end. Length of the current valid
230 data area is also incremented. Returns a pointer to the data area
234 ---------------------------------
235 | head | data | tail |
236 ---------------------------------
238 Pulls the start of the tail section.
240 ---------------------------------
241 | head | data | tail |
242 ---------------------------------
247 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, uint32 len)
249 unsigned char *old_tail = sb->tail;
252 assert((sb->end - sb->tail) >= len);
261 /* Pushes current tail section towards beginning. Length of the current
262 valid data area is also decremented. Returns a pointer to the
263 tail section before pushing.
266 ---------------------------------
267 | head | data | tail |
268 ---------------------------------
270 Pushes the start of the tail section.
272 ---------------------------------
273 | head | data | tail |
274 ---------------------------------
279 unsigned char *silc_buffer_push_tail(SilcBuffer sb, uint32 len)
281 unsigned char *old_tail = sb->tail;
284 assert((sb->tail - len) >= sb->data);
293 /* Puts data at the head of the buffer. Returns pointer to the copied
297 ---------------------------------
298 | head | data | tail |
299 ---------------------------------
301 Puts data to the head section.
305 unsigned char *silc_buffer_put_head(SilcBuffer sb,
310 assert((sb->data - sb->head) >= len);
312 return (unsigned char *)memcpy(sb->head, data, len);
315 /* Puts data at the start of the valid data area. Returns a pointer
316 to the copied data area.
319 ---------------------------------
320 | head | data | tail |
321 ---------------------------------
323 Puts data to the data section.
327 unsigned char *silc_buffer_put(SilcBuffer sb,
332 assert((sb->tail - sb->data) >= len);
334 return (unsigned char *)memcpy(sb->data, data, len);
337 /* Puts data at the tail of the buffer. Returns pointer to the copied
341 ---------------------------------
342 | head | data | tail |
343 ---------------------------------
345 Puts data to the tail section.
349 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
354 assert((sb->end - sb->tail) >= len);
356 return (unsigned char *)memcpy(sb->tail, data, len);