1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file dbuf.c |
23 |
|
|
* \brief Supports dynamic data buffers. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
michael |
1011 |
#include "list.h" |
29 |
adx |
30 |
#include "dbuf.h" |
30 |
|
|
#include "memory.h" |
31 |
michael |
1654 |
#include "mempool.h" |
32 |
adx |
30 |
|
33 |
michael |
1654 |
static mp_pool_t *dbuf_pool; |
34 |
adx |
30 |
|
35 |
|
|
void |
36 |
|
|
dbuf_init(void) |
37 |
|
|
{ |
38 |
michael |
1654 |
dbuf_pool = mp_pool_new(sizeof(struct dbuf_block), MP_CHUNK_SIZE_DBUF); |
39 |
adx |
30 |
} |
40 |
|
|
|
41 |
michael |
3107 |
void |
42 |
|
|
dbuf_add(struct dbuf_queue *qptr, struct dbuf_block *bptr) |
43 |
adx |
30 |
{ |
44 |
michael |
3107 |
assert(bptr->size > 0); |
45 |
|
|
|
46 |
|
|
bptr->refs++; |
47 |
|
|
dlinkAddTail(bptr, make_dlink_node(), &qptr->blocks); |
48 |
|
|
|
49 |
|
|
qptr->total_size += bptr->size; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
void |
53 |
|
|
dbuf_delete(struct dbuf_queue *qptr, size_t count) |
54 |
|
|
{ |
55 |
|
|
assert(qptr->total_size >= count); |
56 |
|
|
|
57 |
|
|
qptr->total_size -= count; |
58 |
|
|
|
59 |
|
|
while (count > 0 && dlink_list_length(&qptr->blocks) > 0) |
60 |
|
|
{ |
61 |
|
|
dlink_node *node = qptr->blocks.head; |
62 |
|
|
struct dbuf_block *buffer = node->data; |
63 |
|
|
size_t avail; |
64 |
|
|
|
65 |
|
|
assert(buffer->size > qptr->pos); |
66 |
|
|
avail = buffer->size - qptr->pos; |
67 |
|
|
|
68 |
|
|
if (count >= avail) |
69 |
|
|
{ |
70 |
|
|
count -= avail; |
71 |
|
|
|
72 |
|
|
dbuf_ref_free(buffer); |
73 |
|
|
|
74 |
|
|
dlinkDelete(node, &qptr->blocks); |
75 |
|
|
free_dlink_node(node); |
76 |
|
|
|
77 |
|
|
qptr->pos = 0; |
78 |
|
|
|
79 |
|
|
} |
80 |
|
|
else |
81 |
|
|
{ |
82 |
|
|
qptr->pos += count; |
83 |
|
|
count = 0; |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
struct dbuf_block * |
89 |
|
|
dbuf_alloc(void) |
90 |
|
|
{ |
91 |
michael |
1654 |
struct dbuf_block *block = mp_pool_get(dbuf_pool); |
92 |
adx |
30 |
|
93 |
michael |
1654 |
memset(block, 0, sizeof(*block)); |
94 |
michael |
3107 |
|
95 |
|
|
++block->refs; |
96 |
adx |
30 |
return block; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
void |
100 |
michael |
3107 |
dbuf_ref_free(struct dbuf_block *bptr) |
101 |
adx |
30 |
{ |
102 |
michael |
3107 |
if (--bptr->refs <= 0) |
103 |
|
|
mp_pool_release(bptr); |
104 |
|
|
} |
105 |
adx |
30 |
|
106 |
michael |
3107 |
void |
107 |
|
|
dbuf_put(struct dbuf_block *dbuf, const char *pattern, ...) |
108 |
|
|
{ |
109 |
|
|
va_list args; |
110 |
adx |
30 |
|
111 |
michael |
3107 |
assert(dbuf->refs == 1); |
112 |
adx |
30 |
|
113 |
michael |
3107 |
va_start(args, pattern); |
114 |
|
|
dbuf_put_args(dbuf, pattern, args); |
115 |
|
|
va_end(args); |
116 |
|
|
} |
117 |
adx |
30 |
|
118 |
michael |
3107 |
void |
119 |
|
|
dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args) |
120 |
|
|
{ |
121 |
|
|
assert(dbuf->refs == 1); |
122 |
adx |
30 |
|
123 |
michael |
3107 |
dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args); |
124 |
adx |
30 |
|
125 |
michael |
3107 |
if (dbuf->size > sizeof(dbuf->data)) |
126 |
|
|
dbuf->size = sizeof(dbuf->data); /* Required by some versions of vsnprintf */ |
127 |
adx |
30 |
} |
128 |
|
|
|
129 |
|
|
void |
130 |
michael |
3107 |
dbuf_put_raw(struct dbuf_queue *qptr, const char *buf, size_t sz) |
131 |
adx |
30 |
{ |
132 |
michael |
3107 |
struct dbuf_block *buffer; |
133 |
adx |
30 |
|
134 |
michael |
3107 |
assert(sz > 0); |
135 |
adx |
30 |
|
136 |
michael |
3107 |
if (dlink_list_length(&qptr->blocks) == 0) |
137 |
adx |
30 |
{ |
138 |
michael |
3107 |
buffer = dbuf_alloc(); |
139 |
|
|
dlinkAddTail(buffer, make_dlink_node(), &qptr->blocks); |
140 |
|
|
assert(qptr->pos == 0); |
141 |
adx |
30 |
} |
142 |
|
|
|
143 |
michael |
3107 |
do |
144 |
|
|
{ |
145 |
|
|
size_t amount; |
146 |
|
|
|
147 |
|
|
buffer = qptr->blocks.tail->data; |
148 |
|
|
amount = sizeof(buffer->data) - buffer->size; |
149 |
|
|
|
150 |
|
|
if (!amount) |
151 |
|
|
{ |
152 |
|
|
buffer = dbuf_alloc(); |
153 |
|
|
dlinkAddTail(buffer, make_dlink_node(), &qptr->blocks); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
if (amount > sz) |
157 |
|
|
amount = sz; |
158 |
|
|
|
159 |
|
|
memcpy(&buffer->data[buffer->size], buf, amount); |
160 |
|
|
buffer->size += amount; |
161 |
|
|
|
162 |
|
|
qptr->total_size += amount; |
163 |
|
|
sz -= amount; |
164 |
|
|
buf += amount; |
165 |
|
|
} while (sz > 0); |
166 |
adx |
30 |
} |
167 |
|
|
|