4 Copyright (C) 2001 Timo Sirainen
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.
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.
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 USA
25 #include "write-buffer.h"
27 #define BUFFER_BLOCK_SIZE 2048
36 static GSList *empty_blocks;
37 static GHashTable *buffers;
38 static int block_count;
40 static int write_buffer_max_blocks;
41 static int timeout_tag;
43 static void write_buffer_new_block(BUFFER_REC *rec)
47 if (empty_blocks == NULL)
48 block = g_malloc(BUFFER_BLOCK_SIZE);
50 block = empty_blocks->data;
51 empty_blocks = g_slist_remove(empty_blocks, block);
55 rec->active_block = block;
56 rec->active_block_pos = 0;
57 rec->blocks = g_slist_append(rec->blocks, block);
60 int write_buffer(int handle, const void *data, int size)
63 const char *cdata = data;
66 if (write_buffer_max_blocks <= 0) {
68 return write(handle, data, size);
74 rec = g_hash_table_lookup(buffers, GINT_TO_POINTER(handle));
76 rec = g_new0(BUFFER_REC, 1);
77 write_buffer_new_block(rec);
78 g_hash_table_insert(buffers, GINT_TO_POINTER(handle), rec);
82 if (rec->active_block_pos == BUFFER_BLOCK_SIZE)
83 write_buffer_new_block(rec);
85 next_size = size < BUFFER_BLOCK_SIZE-rec->active_block_pos ?
86 size : BUFFER_BLOCK_SIZE-rec->active_block_pos;
87 memcpy(rec->active_block+rec->active_block_pos,
90 rec->active_block_pos += next_size;
95 if (block_count > write_buffer_max_blocks)
101 static int write_buffer_flush_rec(void *handlep, BUFFER_REC *rec)
106 handle = GPOINTER_TO_INT(handlep);
107 for (tmp = rec->blocks; tmp != NULL; tmp = tmp->next) {
108 size = tmp->data != rec->active_block ? BUFFER_BLOCK_SIZE :
109 rec->active_block_pos;
110 write(handle, tmp->data, size);
113 empty_blocks = g_slist_concat(empty_blocks, rec->blocks);
118 void write_buffer_flush(void)
120 g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
121 g_slist_free(empty_blocks);
124 g_hash_table_foreach_remove(buffers,
125 (GHRFunc) write_buffer_flush_rec, NULL);
129 static int flush_timeout(void)
131 write_buffer_flush();
135 static void read_settings(void)
137 write_buffer_flush();
139 write_buffer_max_blocks =
140 settings_get_size("write_buffer_size") / BUFFER_BLOCK_SIZE;
142 if (settings_get_time("write_buffer_timeout") > 0) {
143 if (timeout_tag == -1) {
144 timeout_tag = g_timeout_add(settings_get_time("write_buffer_timeout"),
145 (GSourceFunc) flush_timeout,
148 } else if (timeout_tag != -1) {
149 g_source_remove(timeout_tag);
154 static void cmd_flushbuffer(void)
156 write_buffer_flush();
159 void write_buffer_init(void)
161 settings_add_time("misc", "write_buffer_timeout", "0");
162 settings_add_size("misc", "write_buffer_size", "0");
164 buffers = g_hash_table_new((GHashFunc) g_direct_hash,
165 (GCompareFunc) g_direct_equal);
172 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
173 command_bind("flushbuffer", NULL, (SIGNAL_FUNC) cmd_flushbuffer);
176 void write_buffer_deinit(void)
178 if (timeout_tag != -1)
179 g_source_remove(timeout_tag);
181 write_buffer_flush();
182 g_hash_table_destroy(buffers);
184 g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
185 g_slist_free(empty_blocks);
187 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
188 command_unbind("flushbuffer", (SIGNAL_FUNC) cmd_flushbuffer);