ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 2675 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

File Contents

# Content
1 /*
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 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "list.h"
27 #include "balloc.h"
28 #include "dbuf.h"
29 #include "memory.h"
30
31 static BlockHeap *dbuf_heap;
32
33 void
34 dbuf_init(void)
35 {
36 dbuf_heap = BlockHeapCreate("dbuf", sizeof(struct dbuf_block), DBUF_HEAP_SIZE);
37 }
38
39 static struct dbuf_block *
40 dbuf_alloc(struct dbuf_queue *qptr)
41 {
42 struct dbuf_block *block = BlockHeapAlloc(dbuf_heap);
43
44 dlinkAddTail(block, make_dlink_node(), &qptr->blocks);
45 return block;
46 }
47
48 void
49 dbuf_put(struct dbuf_queue *qptr, char *data, size_t count)
50 {
51 struct dbuf_block *last;
52 size_t amount;
53
54 assert(count > 0);
55 if (qptr->blocks.tail == NULL)
56 dbuf_alloc(qptr);
57
58 do {
59 last = qptr->blocks.tail->data;
60
61 amount = DBUF_BLOCK_SIZE - last->size;
62 if (!amount)
63 {
64 last = dbuf_alloc(qptr);
65 amount = DBUF_BLOCK_SIZE;
66 }
67 if (amount > count)
68 amount = count;
69
70 memcpy((void *) &last->data[last->size], data, amount);
71 count -= amount;
72 last->size += amount;
73 qptr->total_size += amount;
74
75 data += amount;
76
77 } while (count > 0);
78 }
79
80 void
81 dbuf_delete(struct dbuf_queue *qptr, size_t count)
82 {
83 dlink_node *ptr;
84 struct dbuf_block *first;
85
86 assert(qptr->total_size >= count);
87 if (count == 0)
88 return;
89
90 /* free whole blocks first.. */
91 while (1)
92 {
93 if (!count)
94 return;
95 ptr = qptr->blocks.head;
96 first = ptr->data;
97 if (count < first->size)
98 break;
99
100 qptr->total_size -= first->size;
101 count -= first->size;
102 dlinkDelete(ptr, &qptr->blocks);
103 free_dlink_node(ptr);
104 BlockHeapFree(dbuf_heap, first);
105 }
106
107 /* ..then remove data from the beginning of the queue */
108 first->size -= count;
109 qptr->total_size -= count;
110 memmove((void *) &first->data, (void *) &first->data[count], first->size);
111 }
112

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision