1 |
adx |
30 |
/* |
2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
|
|
* dbuf.c: Supports dynamic data buffers. |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
|
|
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
|
|
* USA |
21 |
|
|
* |
22 |
knight |
31 |
* $Id$ |
23 |
adx |
30 |
*/ |
24 |
|
|
|
25 |
|
|
#include "stdinc.h" |
26 |
|
|
#include "balloc.h" |
27 |
|
|
#include "common.h" |
28 |
|
|
#include "dbuf.h" |
29 |
|
|
#include "list.h" |
30 |
|
|
#include "tools.h" |
31 |
|
|
#include "memory.h" |
32 |
|
|
|
33 |
|
|
static BlockHeap *dbuf_heap; |
34 |
|
|
|
35 |
|
|
void |
36 |
|
|
dbuf_init(void) |
37 |
|
|
{ |
38 |
|
|
dbuf_heap = BlockHeapCreate("dbuf", sizeof(struct dbuf_block), DBUF_HEAP_SIZE); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
static struct dbuf_block * |
42 |
|
|
dbuf_alloc(struct dbuf_queue *qptr) |
43 |
|
|
{ |
44 |
|
|
struct dbuf_block *block = BlockHeapAlloc(dbuf_heap); |
45 |
|
|
|
46 |
|
|
dlinkAddTail(block, make_dlink_node(), &qptr->blocks); |
47 |
|
|
return block; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
void |
51 |
|
|
dbuf_put(struct dbuf_queue *qptr, char *data, size_t count) |
52 |
|
|
{ |
53 |
|
|
struct dbuf_block *last; |
54 |
|
|
size_t amount; |
55 |
|
|
|
56 |
|
|
assert(count > 0); |
57 |
|
|
if (qptr->blocks.tail == NULL) |
58 |
|
|
dbuf_alloc(qptr); |
59 |
|
|
|
60 |
|
|
do { |
61 |
|
|
last = qptr->blocks.tail->data; |
62 |
|
|
|
63 |
|
|
amount = DBUF_BLOCK_SIZE - last->size; |
64 |
|
|
if (!amount) |
65 |
|
|
{ |
66 |
|
|
last = dbuf_alloc(qptr); |
67 |
|
|
amount = DBUF_BLOCK_SIZE; |
68 |
|
|
} |
69 |
|
|
if (amount > count) |
70 |
|
|
amount = count; |
71 |
|
|
|
72 |
|
|
memcpy((void *) &last->data[last->size], data, amount); |
73 |
|
|
count -= amount; |
74 |
|
|
last->size += amount; |
75 |
|
|
qptr->total_size += amount; |
76 |
|
|
|
77 |
|
|
data += amount; |
78 |
|
|
|
79 |
|
|
} while (count > 0); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
void |
83 |
|
|
dbuf_delete(struct dbuf_queue *qptr, size_t count) |
84 |
|
|
{ |
85 |
|
|
dlink_node *ptr; |
86 |
|
|
struct dbuf_block *first; |
87 |
|
|
|
88 |
|
|
assert(qptr->total_size >= count); |
89 |
|
|
if (count == 0) |
90 |
|
|
return; |
91 |
|
|
|
92 |
|
|
/* free whole blocks first.. */ |
93 |
|
|
while (1) |
94 |
|
|
{ |
95 |
|
|
if (!count) |
96 |
|
|
return; |
97 |
|
|
ptr = qptr->blocks.head; |
98 |
|
|
first = ptr->data; |
99 |
|
|
if (count < first->size) |
100 |
|
|
break; |
101 |
|
|
|
102 |
|
|
qptr->total_size -= first->size; |
103 |
|
|
count -= first->size; |
104 |
|
|
dlinkDelete(ptr, &qptr->blocks); |
105 |
|
|
free_dlink_node(ptr); |
106 |
|
|
BlockHeapFree(dbuf_heap, first); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/* ..then remove data from the beginning of the queue */ |
110 |
|
|
first->size -= count; |
111 |
|
|
qptr->total_size -= count; |
112 |
|
|
memmove((void *) &first->data, (void *) &first->data[count], first->size); |
113 |
|
|
} |
114 |
|
|
|