1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
* |
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 |
/*! \file dbuf.c |
23 |
* \brief Supports dynamic data buffers. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "dbuf.h" |
30 |
#include "memory.h" |
31 |
#include "mempool.h" |
32 |
|
33 |
static mp_pool_t *dbuf_pool; |
34 |
|
35 |
void |
36 |
dbuf_init(void) |
37 |
{ |
38 |
dbuf_pool = mp_pool_new(sizeof(struct dbuf_block), MP_CHUNK_SIZE_DBUF); |
39 |
} |
40 |
|
41 |
void |
42 |
dbuf_add(struct dbuf_queue *qptr, struct dbuf_block *bptr) |
43 |
{ |
44 |
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 |
struct dbuf_block *block = mp_pool_get(dbuf_pool); |
92 |
|
93 |
memset(block, 0, sizeof(*block)); |
94 |
|
95 |
++block->refs; |
96 |
return block; |
97 |
} |
98 |
|
99 |
void |
100 |
dbuf_ref_free(struct dbuf_block *bptr) |
101 |
{ |
102 |
if (--bptr->refs <= 0) |
103 |
mp_pool_release(bptr); |
104 |
} |
105 |
|
106 |
void |
107 |
dbuf_put(struct dbuf_block *dbuf, const char *pattern, ...) |
108 |
{ |
109 |
va_list args; |
110 |
|
111 |
assert(dbuf->refs == 1); |
112 |
|
113 |
va_start(args, pattern); |
114 |
dbuf_put_args(dbuf, pattern, args); |
115 |
va_end(args); |
116 |
} |
117 |
|
118 |
void |
119 |
dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args) |
120 |
{ |
121 |
assert(dbuf->refs == 1); |
122 |
|
123 |
dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args); |
124 |
|
125 |
if (dbuf->size > sizeof(dbuf->data)) |
126 |
dbuf->size = sizeof(dbuf->data); /* Required by some versions of vsnprintf */ |
127 |
} |
128 |
|
129 |
void |
130 |
dbuf_put_raw(struct dbuf_queue *qptr, const char *buf, size_t sz) |
131 |
{ |
132 |
struct dbuf_block *buffer; |
133 |
|
134 |
assert(sz > 0); |
135 |
|
136 |
if (dlink_list_length(&qptr->blocks) == 0) |
137 |
{ |
138 |
buffer = dbuf_alloc(); |
139 |
dlinkAddTail(buffer, make_dlink_node(), &qptr->blocks); |
140 |
assert(qptr->pos == 0); |
141 |
} |
142 |
|
143 |
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 |
} |
167 |
|