aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver/fd-table.c
blob: 1344a0eee7c038e55ddad29fe540917de2d0228b (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015, 2016, 2017  Mattias Andrée (m@maandree.se)
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include "fd-table.h"

#include "macros.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>


/**
 * Create a fd table
 * 
 * @param   this              Memory slot in which to store the new fd table
 * @param   initial_capacity  The initial capacity of the table
 * @return                    Non-zero on error, `errno` will have been set accordingly
 */
int
fd_table_create_tuned(fd_table_t *restrict this, size_t initial_capacity)
{
	size_t bitcap;

	this->capacity = initial_capacity ? initial_capacity : 1;
	this->size = 0;

	this->values = NULL;
	this->used = NULL;
	this->value_comparator = NULL;

	/* It is important that both allocations are done with calloc:
	   `this->used` must set all keys as unused at the initial state,
	   `this->values` must be initialised for marshaling and it helps
	   the time overhead of `fd_table_contains_value`. */

	bitcap = (this->capacity + 63) / 64;
	fail_if (xcalloc(this->used,   bitcap,         size_t));
	fail_if (xcalloc(this->values, this->capacity, size_t));

	return 0;
fail:
	return -1;
}


/**
 * Release all resources in a fd table, should
 * be done even if construction fails
 * 
 * @param  this          The fd table
 * @param  keys_freer    Function that frees a key, `NULL` if keys should not be freed
 * @param  values_freer  Function that frees a value, `NULL` if value should not be freed
 */
void
fd_table_destroy(fd_table_t *restrict this, free_func *key_freer, free_func *value_freer)
{
	size_t i;
	if ((key_freer || value_freer) && this->used && this->values) {
		for (i = 0; i < this->capacity; i++) {
			if (this->used[i / 64] & ((uint64_t)1 << (i % 64))) {
				if (key_freer)   key_freer(i);
				if (value_freer) value_freer(this->values[i]);
			}
		}
	}
	free(this->values);
	free(this->used);
}


/**
 * Check whether a value is stored in the table
 * 
 * @param   this   The fd table
 * @param   value  The value
 * @return         Whether the value is stored in the table
 */
int
fd_table_contains_value(const fd_table_t *restrict this, size_t value)
{
	size_t i;
	if (!this->value_comparator) {
		for (i = 0; i < this->capacity; i++)
			if (this->values[i] == value)
				if (this->used[i / 64] & ((uint64_t)1 << (i % 64)))
					return 1;
	} else {
		for (i = 0; i < this->capacity; i++)
			if (this->used[i / 64] & ((uint64_t)1 << (i % 64)))
				if (this->value_comparator(this->values[i], value))
					return 1;
	}
	return 0;
}


/**
 * Check whether a key is used in the table
 * 
 * @param   this  The fd table
 * @param   key   The key
 * @return        Whether the key is used
 */
int
fd_table_contains_key(const fd_table_t *restrict this, int key)
{
	return (size_t)key < this->capacity && (this->used[key / 64] & ((uint64_t)1 << (key % 64)));
}


/**
 * Look up a value in the table
 * 
 * @param   this  The fd table
 * @param   key   The key associated with the value
 * @return        The value associated with the key, 0 if the key was not used
 */
size_t
fd_table_get(const fd_table_t *restrict this, int key)
{
	return fd_table_contains_key(this, key) ? this->values[key] : 0;
}


/**
 * Add an entry to the table
 * 
 * @param   this   The fd table
 * @param   key    The key of the entry to add
 * @param   value  The value of the entry to add
 * @return         The previous value associated with the key, 0 if the key was not used.
 *                 0 will also be returned on error, check the `errno` variable.
 */
size_t
fd_table_put(fd_table_t *restrict this, int key, size_t value)
{
	size_t rc, old_bitcap, new_bitcap, *old_values;
	uint64_t *old_used;

	/* Override current value if the key is already used. */
	if (fd_table_contains_key(this, key)) {
		rc = fd_table_get(this, key);
		this->values[key] = value;
		return rc;
	}

	/* Grow the table if it is too small. */
	errno = 0;
	if ((size_t)key >= this->capacity) {
		old_values = this->values;
		if (xrealloc(this->values, this->capacity << 1, size_t)) {
			this->values = old_values;
			fail_if (1);
		}

		memset(this->values + this->capacity, 0, this->capacity * sizeof(size_t));
      
		old_bitcap = (this->capacity + 63) / 64;
		this->capacity <<= 1;
		new_bitcap = (this->capacity + 63) / 64;

		if (new_bitcap > old_bitcap) {
			old_used = this->used;
			if (xrealloc(this->used, new_bitcap, size_t)) {
				this->used = old_used;
				this->capacity >>= 1;
				fail_if (1);
			}

			memset(this->used + old_bitcap, 0, (new_bitcap - old_bitcap) * sizeof(uint64_t));
		}
	}

	/* Store the entry. */
	this->used[key / 64] |= (uint64_t)1 << (key % 64);
	this->values[key] = value;
	this->size++;
	return 0;
fail:
	return 0;
}


/**
 * Remove an entry in the table
 * 
 * @param   this  The fd table
 * @param   key   The key of the entry to remove
 * @return        The previous value associated with the key, 0 if the key was not used
 */
size_t
fd_table_remove(fd_table_t *restrict this, int key)
{
	size_t rc = fd_table_get(this, key);
	if (rc && fd_table_contains_key(this, key)) {
		this->used[key / 64] &= ~((uint64_t)1 << (key % 64));
		this->size--;
	}
	return rc;
}


/**
 * Remove all entries in the table
 * 
 * @param  this  The fd table
 */
void
fd_table_clear(fd_table_t *restrict this)
{
	size_t bitcap;
	this->size = 0;
	bitcap = (this->capacity + 63) / 64;
	memset(this->used, 0, bitcap * sizeof(uint64_t));
}


/**
 * Calculate the buffer size need to marshal a fd table
 * 
 * @param   this  The fd table
 * @return        The number of bytes to allocate to the output buffer
 */
size_t
fd_table_marshal_size(const fd_table_t *restrict this)
{
	size_t bitcap = (this->capacity + 63) / 64;
	return (this->capacity + 2) * sizeof(size_t) + bitcap * sizeof(uint64_t) + sizeof(int);
}


/**
 * Marshals a fd table
 * 
 * @param  this  The fd table
 * @param  data  Output buffer for the marshalled data
 */
void
fd_table_marshal(const fd_table_t *restrict this, char *restrict data)
{
	size_t bitcap = (this->capacity + 63) / 64;

	buf_set_next(data, int, FD_TABLE_T_VERSION);
	buf_set_next(data, size_t, this->capacity);
	buf_set_next(data, size_t, this->size);

	memcpy(data, this->values, this->capacity * sizeof(size_t));
	buf_next(data, size_t, this->capacity);

	memcpy(data, this->used, bitcap * sizeof(uint64_t));
}


/**
 * Unmarshals a fd table
 * 
 * @param   this      Memory slot in which to store the new fd table
 * @param   data      In buffer with the marshalled data
 * @param   remapper  Function that translates values, `NULL` if not translation takes place
 * @return            Non-zero on error, `errno` will be set accordingly.
 *                    Destroy the table on error.
 */
int
fd_table_unmarshal(fd_table_t *restrict this, char *restrict data, remap_func *remapper)
{
	size_t bitcap;
	size_t i;

	/* buf_get(data, int, 0, FD_TABLE_T_VERSION) */
	buf_next(data, int, 1);

	buf_get_next(data, size_t, this->capacity);
	buf_get_next(data, size_t, this->size);

	this->values           = NULL;
	this->used             = NULL;
	this->value_comparator = NULL;

	fail_if (xmalloc(this->values, this->capacity, size_t));

	bitcap = (this->capacity + 63) / 64;
	fail_if (xmalloc(this->used, bitcap, size_t));

	memcpy(this->values, data, this->capacity * sizeof(size_t));
	buf_next(data, size_t, this->capacity);

	memcpy(this->used, data, bitcap * sizeof(uint64_t));

	if (remapper)
		for (i = 0; i < this->capacity; i++)
			if (this->used[i / 64] & ((uint64_t)1 << (i % 64)))
				this->values[i] = remapper(this->values[i]);

	return 0;
fail:
	return -1;
}