| 1 |
adx |
30 |
/* |
| 2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
|
|
* dbuf.c: Supports dynamic data buffers. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
|
|
* |
| 7 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU General Public License as published by |
| 9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
* (at your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* This program is distributed in the hope that it will be useful, |
| 13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
* GNU General Public License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU General Public License |
| 18 |
|
|
* along with this program; if not, write to the Free Software |
| 19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
|
|
* USA |
| 21 |
|
|
* |
| 22 |
knight |
31 |
* $Id$ |
| 23 |
adx |
30 |
*/ |
| 24 |
|
|
|
| 25 |
|
|
#include "stdinc.h" |
| 26 |
michael |
1011 |
#include "list.h" |
| 27 |
adx |
30 |
#include "balloc.h" |
| 28 |
|
|
#include "common.h" |
| 29 |
|
|
#include "dbuf.h" |
| 30 |
|
|
#include "memory.h" |
| 31 |
|
|
|
| 32 |
|
|
static BlockHeap *dbuf_heap; |
| 33 |
|
|
|
| 34 |
|
|
void |
| 35 |
|
|
dbuf_init(void) |
| 36 |
|
|
{ |
| 37 |
|
|
dbuf_heap = BlockHeapCreate("dbuf", sizeof(struct dbuf_block), DBUF_HEAP_SIZE); |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
|
static struct dbuf_block * |
| 41 |
|
|
dbuf_alloc(struct dbuf_queue *qptr) |
| 42 |
|
|
{ |
| 43 |
|
|
struct dbuf_block *block = BlockHeapAlloc(dbuf_heap); |
| 44 |
|
|
|
| 45 |
|
|
dlinkAddTail(block, make_dlink_node(), &qptr->blocks); |
| 46 |
|
|
return block; |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
void |
| 50 |
|
|
dbuf_put(struct dbuf_queue *qptr, char *data, size_t count) |
| 51 |
|
|
{ |
| 52 |
|
|
struct dbuf_block *last; |
| 53 |
|
|
size_t amount; |
| 54 |
|
|
|
| 55 |
|
|
assert(count > 0); |
| 56 |
|
|
if (qptr->blocks.tail == NULL) |
| 57 |
|
|
dbuf_alloc(qptr); |
| 58 |
|
|
|
| 59 |
|
|
do { |
| 60 |
|
|
last = qptr->blocks.tail->data; |
| 61 |
|
|
|
| 62 |
|
|
amount = DBUF_BLOCK_SIZE - last->size; |
| 63 |
|
|
if (!amount) |
| 64 |
|
|
{ |
| 65 |
|
|
last = dbuf_alloc(qptr); |
| 66 |
|
|
amount = DBUF_BLOCK_SIZE; |
| 67 |
|
|
} |
| 68 |
|
|
if (amount > count) |
| 69 |
|
|
amount = count; |
| 70 |
|
|
|
| 71 |
|
|
memcpy((void *) &last->data[last->size], data, amount); |
| 72 |
|
|
count -= amount; |
| 73 |
|
|
last->size += amount; |
| 74 |
|
|
qptr->total_size += amount; |
| 75 |
|
|
|
| 76 |
|
|
data += amount; |
| 77 |
|
|
|
| 78 |
|
|
} while (count > 0); |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
void |
| 82 |
|
|
dbuf_delete(struct dbuf_queue *qptr, size_t count) |
| 83 |
|
|
{ |
| 84 |
|
|
dlink_node *ptr; |
| 85 |
|
|
struct dbuf_block *first; |
| 86 |
|
|
|
| 87 |
|
|
assert(qptr->total_size >= count); |
| 88 |
|
|
if (count == 0) |
| 89 |
|
|
return; |
| 90 |
|
|
|
| 91 |
|
|
/* free whole blocks first.. */ |
| 92 |
|
|
while (1) |
| 93 |
|
|
{ |
| 94 |
|
|
if (!count) |
| 95 |
|
|
return; |
| 96 |
|
|
ptr = qptr->blocks.head; |
| 97 |
|
|
first = ptr->data; |
| 98 |
|
|
if (count < first->size) |
| 99 |
|
|
break; |
| 100 |
|
|
|
| 101 |
|
|
qptr->total_size -= first->size; |
| 102 |
|
|
count -= first->size; |
| 103 |
|
|
dlinkDelete(ptr, &qptr->blocks); |
| 104 |
|
|
free_dlink_node(ptr); |
| 105 |
|
|
BlockHeapFree(dbuf_heap, first); |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
/* ..then remove data from the beginning of the queue */ |
| 109 |
|
|
first->size -= count; |
| 110 |
|
|
qptr->total_size -= count; |
| 111 |
|
|
memmove((void *) &first->data, (void *) &first->data[count], first->size); |
| 112 |
|
|
} |
| 113 |
|
|
|