1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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 |
|
32 |
|
33 |
struct dbuf_block * |
34 |
dbuf_alloc(void) |
35 |
{ |
36 |
struct dbuf_block *block = xcalloc(sizeof(*block)); |
37 |
|
38 |
++block->refs; |
39 |
return block; |
40 |
} |
41 |
|
42 |
void |
43 |
dbuf_ref_free(struct dbuf_block *block) |
44 |
{ |
45 |
if (--block->refs <= 0) |
46 |
xfree(block); |
47 |
} |
48 |
|
49 |
void |
50 |
dbuf_add(struct dbuf_queue *queue, struct dbuf_block *block) |
51 |
{ |
52 |
block->refs++; |
53 |
dlinkAddTail(block, make_dlink_node(), &queue->blocks); |
54 |
queue->total_size += block->size; |
55 |
} |
56 |
|
57 |
void |
58 |
dbuf_delete(struct dbuf_queue *queue, size_t count) |
59 |
{ |
60 |
while (count > 0 && dbuf_length(queue) > 0) |
61 |
{ |
62 |
dlink_node *node = queue->blocks.head; |
63 |
struct dbuf_block *block = node->data; |
64 |
size_t avail = block->size - queue->pos; |
65 |
|
66 |
if (count >= avail) |
67 |
{ |
68 |
count -= avail; |
69 |
queue->total_size -= avail; |
70 |
|
71 |
dbuf_ref_free(block); |
72 |
|
73 |
dlinkDelete(node, &queue->blocks); |
74 |
free_dlink_node(node); |
75 |
|
76 |
queue->pos = 0; |
77 |
} |
78 |
else |
79 |
{ |
80 |
queue->pos += count; |
81 |
|
82 |
queue->total_size -= count; |
83 |
count -= count; |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
void |
89 |
dbuf_put_fmt(struct dbuf_block *dbuf, const char *pattern, ...) |
90 |
{ |
91 |
va_list args; |
92 |
|
93 |
assert(dbuf->refs == 1); |
94 |
|
95 |
va_start(args, pattern); |
96 |
dbuf_put_args(dbuf, pattern, args); |
97 |
va_end(args); |
98 |
} |
99 |
|
100 |
void |
101 |
dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args) |
102 |
{ |
103 |
assert(dbuf->refs == 1); |
104 |
|
105 |
dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args); |
106 |
|
107 |
/* As per C99, (v)snprintf returns the length the resulting string would be */ |
108 |
if (dbuf->size > sizeof(dbuf->data)) |
109 |
dbuf->size = sizeof(dbuf->data); |
110 |
} |
111 |
|
112 |
void |
113 |
dbuf_put(struct dbuf_queue *queue, const char *buf, size_t sz) |
114 |
{ |
115 |
while (sz > 0) |
116 |
{ |
117 |
struct dbuf_block *block = dbuf_length(queue) ? queue->blocks.tail->data : NULL; |
118 |
|
119 |
if (block == NULL || sizeof(block->data) - block->size == 0) |
120 |
{ |
121 |
block = dbuf_alloc(); |
122 |
dlinkAddTail(block, make_dlink_node(), &queue->blocks); |
123 |
} |
124 |
|
125 |
size_t avail = sizeof(block->data) - block->size; |
126 |
if (avail > sz) |
127 |
avail = sz; |
128 |
|
129 |
memcpy(&block->data[block->size], buf, avail); |
130 |
block->size += avail; |
131 |
|
132 |
queue->total_size += avail; |
133 |
|
134 |
sz -= avail; |
135 |
buf += avail; |
136 |
} |
137 |
} |