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 |
|
|
static struct dbuf_block * |
42 |
|
|
dbuf_alloc(struct dbuf_queue *qptr) |
43 |
|
|
{ |
44 |
michael |
1654 |
struct dbuf_block *block = mp_pool_get(dbuf_pool); |
45 |
adx |
30 |
|
46 |
michael |
1654 |
memset(block, 0, sizeof(*block)); |
47 |
adx |
30 |
dlinkAddTail(block, make_dlink_node(), &qptr->blocks); |
48 |
|
|
return block; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
void |
52 |
|
|
dbuf_put(struct dbuf_queue *qptr, char *data, size_t count) |
53 |
|
|
{ |
54 |
|
|
struct dbuf_block *last; |
55 |
|
|
size_t amount; |
56 |
|
|
|
57 |
|
|
assert(count > 0); |
58 |
|
|
if (qptr->blocks.tail == NULL) |
59 |
|
|
dbuf_alloc(qptr); |
60 |
|
|
|
61 |
|
|
do { |
62 |
|
|
last = qptr->blocks.tail->data; |
63 |
|
|
|
64 |
|
|
amount = DBUF_BLOCK_SIZE - last->size; |
65 |
|
|
if (!amount) |
66 |
|
|
{ |
67 |
|
|
last = dbuf_alloc(qptr); |
68 |
|
|
amount = DBUF_BLOCK_SIZE; |
69 |
|
|
} |
70 |
|
|
if (amount > count) |
71 |
|
|
amount = count; |
72 |
|
|
|
73 |
|
|
memcpy((void *) &last->data[last->size], data, amount); |
74 |
|
|
count -= amount; |
75 |
|
|
last->size += amount; |
76 |
|
|
qptr->total_size += amount; |
77 |
|
|
|
78 |
|
|
data += amount; |
79 |
|
|
|
80 |
|
|
} while (count > 0); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
void |
84 |
|
|
dbuf_delete(struct dbuf_queue *qptr, size_t count) |
85 |
|
|
{ |
86 |
|
|
dlink_node *ptr; |
87 |
|
|
struct dbuf_block *first; |
88 |
|
|
|
89 |
|
|
assert(qptr->total_size >= count); |
90 |
|
|
if (count == 0) |
91 |
|
|
return; |
92 |
|
|
|
93 |
|
|
/* free whole blocks first.. */ |
94 |
|
|
while (1) |
95 |
|
|
{ |
96 |
|
|
if (!count) |
97 |
|
|
return; |
98 |
|
|
ptr = qptr->blocks.head; |
99 |
|
|
first = ptr->data; |
100 |
|
|
if (count < first->size) |
101 |
|
|
break; |
102 |
|
|
|
103 |
|
|
qptr->total_size -= first->size; |
104 |
|
|
count -= first->size; |
105 |
|
|
dlinkDelete(ptr, &qptr->blocks); |
106 |
|
|
free_dlink_node(ptr); |
107 |
michael |
1654 |
mp_pool_release(first); |
108 |
adx |
30 |
} |
109 |
|
|
|
110 |
|
|
/* ..then remove data from the beginning of the queue */ |
111 |
|
|
first->size -= count; |
112 |
|
|
qptr->total_size -= count; |
113 |
|
|
memmove((void *) &first->data, (void *) &first->data[count], first->size); |
114 |
|
|
} |
115 |
|
|
|