summaryrefslogtreecommitdiff
path: root/src/core/write-buffer.c
blob: 1c3eef824b4a54ebca451ae99ec71d00a71e9e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 write-buffer.c : irssi

    Copyright (C) 2001 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "commands.h"
#include "settings.h"
#include "write-buffer.h"

#define BUFFER_BLOCK_SIZE 2048

typedef struct {
	char *active_block;
        int active_block_pos;

	GSList *blocks;
} BUFFER_REC;

static GSList *empty_blocks;
static GHashTable *buffers;
static int block_count;

static int write_buffer_max_blocks;
static int timeout_tag;

static void write_buffer_new_block(BUFFER_REC *rec)
{
	char *block;

	if (empty_blocks == NULL)
		block = g_malloc(BUFFER_BLOCK_SIZE);
	else {
		block = empty_blocks->data;
                empty_blocks = g_slist_remove(empty_blocks, block);
	}

        block_count++;
	rec->active_block = block;
        rec->active_block_pos = 0;
	rec->blocks = g_slist_append(rec->blocks, block);
}

int write_buffer(int handle, const void *data, int size)
{
	BUFFER_REC *rec;
        const char *cdata = data;
	int next_size;

	if (write_buffer_max_blocks <= 0) {
		/* no write buffer */
                return write(handle, data, size);
	}

	if (size <= 0)
		return size;

	rec = g_hash_table_lookup(buffers, GINT_TO_POINTER(handle));
	if (rec == NULL) {
		rec = g_new0(BUFFER_REC, 1);
                write_buffer_new_block(rec);
		g_hash_table_insert(buffers, GINT_TO_POINTER(handle), rec);
	}

	while (size > 0) {
                if (rec->active_block_pos == BUFFER_BLOCK_SIZE)
			write_buffer_new_block(rec);

		next_size = size < BUFFER_BLOCK_SIZE-rec->active_block_pos ?
			size : BUFFER_BLOCK_SIZE-rec->active_block_pos;
		memcpy(rec->active_block+rec->active_block_pos,
		       cdata, next_size);

		rec->active_block_pos += next_size;
		cdata += next_size;
                size -= next_size;
	}

	if (block_count > write_buffer_max_blocks)
                write_buffer_flush();

        return size;
}

static int write_buffer_flush_rec(void *handlep, BUFFER_REC *rec)
{
	GSList *tmp;
        int handle, size;

        handle = GPOINTER_TO_INT(handlep);
	for (tmp = rec->blocks; tmp != NULL; tmp = tmp->next) {
		size = tmp->data != rec->active_block ? BUFFER_BLOCK_SIZE :
			rec->active_block_pos;
		write(handle, tmp->data, size);
	}

        empty_blocks = g_slist_concat(empty_blocks, rec->blocks);
	g_free(rec);
        return TRUE;
}

void write_buffer_flush(void)
{
	g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
	g_slist_free(empty_blocks);
        empty_blocks = NULL;

	g_hash_table_foreach_remove(buffers,
				    (GHRFunc) write_buffer_flush_rec, NULL);
        block_count = 0;
}

static int flush_timeout(void)
{
	write_buffer_flush();
        return 1;
}

static void read_settings(void)
{
	int msecs;

	write_buffer_flush();

	write_buffer_max_blocks = settings_get_int("write_buffer_kb") *
		1024/BUFFER_BLOCK_SIZE;

	if (settings_get_int("write_buffer_mins") > 0) {
                msecs = settings_get_int("write_buffer_mins")*60*1000;
		if (timeout_tag == -1) {
			timeout_tag = g_timeout_add(msecs,
						    (GSourceFunc) flush_timeout,
						    NULL);
		}
	} else if (timeout_tag != -1) {
		g_source_remove(timeout_tag);
                timeout_tag = -1;
	}
}

static void cmd_flushbuffer(void)
{
        write_buffer_flush();
}

void write_buffer_init(void)
{
	settings_add_int("misc", "write_buffer_mins", 0);
	settings_add_int("misc", "write_buffer_kb", 0);

	buffers = g_hash_table_new((GHashFunc) g_direct_hash,
				   (GCompareFunc) g_direct_equal);

        empty_blocks = NULL;
        block_count = 0;

	timeout_tag = -1;
	read_settings();
	signal_add("setup changed", (SIGNAL_FUNC) read_settings);
        command_bind("flushbuffer", NULL, (SIGNAL_FUNC) cmd_flushbuffer);
}

void write_buffer_deinit(void)
{
	if (timeout_tag != -1)
		g_source_remove(timeout_tag);

        write_buffer_flush();
        g_hash_table_destroy(buffers);

	g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
        g_slist_free(empty_blocks);

	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
	command_unbind("flushbuffer",  (SIGNAL_FUNC) cmd_flushbuffer);
}