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 |
struct dbuf_block * |
42 |
dbuf_alloc(void) |
43 |
{ |
44 |
struct dbuf_block *block = mp_pool_get(dbuf_pool); |
45 |
|
46 |
memset(block, 0, sizeof(*block)); |
47 |
|
48 |
++block->refs; |
49 |
return block; |
50 |
} |
51 |
|
52 |
void |
53 |
dbuf_ref_free(struct dbuf_block *block) |
54 |
{ |
55 |
if (--block->refs <= 0) |
56 |
mp_pool_release(block); |
57 |
} |
58 |
|
59 |
void |
60 |
dbuf_add(struct dbuf_queue *queue, struct dbuf_block *block) |
61 |
{ |
62 |
block->refs++; |
63 |
dlinkAddTail(block, make_dlink_node(), &queue->blocks); |
64 |
queue->total_size += block->size; |
65 |
} |
66 |
|
67 |
void |
68 |
dbuf_delete(struct dbuf_queue *queue, size_t count) |
69 |
{ |
70 |
while (count > 0 && dbuf_length(queue) > 0) |
71 |
{ |
72 |
dlink_node *node = queue->blocks.head; |
73 |
struct dbuf_block *block = node->data; |
74 |
size_t avail = block->size - queue->pos; |
75 |
|
76 |
if (count >= avail) |
77 |
{ |
78 |
count -= avail; |
79 |
queue->total_size -= avail; |
80 |
|
81 |
dbuf_ref_free(block); |
82 |
|
83 |
dlinkDelete(node, &queue->blocks); |
84 |
free_dlink_node(node); |
85 |
|
86 |
queue->pos = 0; |
87 |
} |
88 |
else |
89 |
{ |
90 |
queue->pos += count; |
91 |
|
92 |
queue->total_size -= count; |
93 |
count -= count; |
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
void |
99 |
dbuf_put_fmt(struct dbuf_block *dbuf, const char *pattern, ...) |
100 |
{ |
101 |
va_list args; |
102 |
|
103 |
assert(dbuf->refs == 1); |
104 |
|
105 |
va_start(args, pattern); |
106 |
dbuf_put_args(dbuf, pattern, args); |
107 |
va_end(args); |
108 |
} |
109 |
|
110 |
void |
111 |
dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args) |
112 |
{ |
113 |
assert(dbuf->refs == 1); |
114 |
|
115 |
dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args); |
116 |
if (dbuf->size > sizeof(dbuf->data)) |
117 |
dbuf->size = sizeof(dbuf->data); /* required by some versions of vsnprintf */ |
118 |
} |
119 |
|
120 |
void |
121 |
dbuf_put(struct dbuf_queue *queue, const char *buf, size_t sz) |
122 |
{ |
123 |
while (sz > 0) |
124 |
{ |
125 |
size_t avail; |
126 |
struct dbuf_block *block = dbuf_length(queue) ? queue->blocks.tail->data : NULL; |
127 |
|
128 |
if (block == NULL || sizeof(block->data) - block->size == 0) |
129 |
{ |
130 |
block = dbuf_alloc(); |
131 |
dlinkAddTail(block, make_dlink_node(), &queue->blocks); |
132 |
} |
133 |
|
134 |
avail = sizeof(block->data) - block->size; |
135 |
if (avail > sz) |
136 |
avail = sz; |
137 |
|
138 |
memcpy(&block->data[block->size], buf, avail); |
139 |
block->size += avail; |
140 |
|
141 |
queue->total_size += avail; |
142 |
|
143 |
sz -= avail; |
144 |
buf += avail; |
145 |
} |
146 |
} |