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)
139 write_buffer_flush();
141 write_buffer_max_blocks = settings_get_int("write_buffer_kb") *
142 1024/BUFFER_BLOCK_SIZE;
144 if (settings_get_int("write_buffer_mins") > 0) {
145 msecs = settings_get_int("write_buffer_mins")*60*1000;
146 if (timeout_tag == -1) {
147 timeout_tag = g_timeout_add(msecs,
148 (GSourceFunc) flush_timeout,
151 } else if (timeout_tag != -1) {
152 g_source_remove(timeout_tag);
157 static void cmd_flushbuffer(void)
159 write_buffer_flush();
162 void write_buffer_init(void)
164 settings_add_int("misc", "write_buffer_mins", 0);
165 settings_add_int("misc", "write_buffer_kb", 0);
167 buffers = g_hash_table_new((GHashFunc) g_direct_hash,
168 (GCompareFunc) g_direct_equal);
175 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
176 command_bind("flushbuffer", NULL, (SIGNAL_FUNC) cmd_flushbuffer);
179 void write_buffer_deinit(void)
181 if (timeout_tag != -1)
182 g_source_remove(timeout_tag);
184 write_buffer_flush();
185 g_hash_table_destroy(buffers);
187 g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
188 g_slist_free(empty_blocks);
190 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
191 command_unbind("flushbuffer", (SIGNAL_FUNC) cmd_flushbuffer);