ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.1.x/src/dbuf.c
Revision: 2917
Committed: Sat Jan 25 21:09:56 2014 UTC (10 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 2715 byte(s)
Log Message:
- Clean up all files in src/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

# Content
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 static struct dbuf_block *
42 dbuf_alloc(struct dbuf_queue *qptr)
43 {
44 struct dbuf_block *block = mp_pool_get(dbuf_pool);
45
46 memset(block, 0, sizeof(*block));
47 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 mp_pool_release(first);
108 }
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

Properties

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