File: bufferpool.c

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (361 lines) | stat: -rw-r--r-- 9,135 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "bufferpool.h"
#include <glib.h>
#include <stdbool.h>
#include "obj.h"

static_assert((BUFFERPOOL_SHARD_SIZE & (BUFFERPOOL_SHARD_SIZE - 1)) == 0,
		"BUFFERPOOL_SHARD_SIZE is not a power of two");

struct bpool_shard;

struct bufferpool {
	void *(*alloc)(void);
	void (*dealloc)(void *);
	unsigned int refs; // sum of all refs from shards, plus the handle itself

	rwlock_t shards_lock;
	struct bpool_shard **shards;
	unsigned int num_shards;
	unsigned int max_shards;
	unsigned int empty_shard_idx;
};

struct bpool_shard {
	struct bufferpool *bp;
	unsigned int refs;
	void *buf; // actual head of buffer, given to free()
	void *empty; // head of usable buffer, head == empty if empty
	void *end;
	void *head;
	unsigned int (*recycle)(void *);
	void *arg;
	bool full;
};

struct bufferpool *bufferpool_new(void *(*alloc)(void), void (*dealloc)(void *)) {
	struct bufferpool *ret = g_new0(__typeof(*ret), 1);
	ret->alloc = alloc;
	ret->dealloc = dealloc;
	ret->refs = 1; // count the bufferpool handle itself as a reference
	rwlock_init(&ret->shards_lock);
	ret->max_shards = 8;
	ret->shards = g_new0(struct bpool_shard *, ret->max_shards);
	return ret;
}

// shard has zero refs and is marked as full
static void bufferpool_recycle(struct bpool_shard *shard) {
	struct bufferpool *bp = shard->bp;
	atomic_set_na(&shard->head, shard->empty);

	unsigned int refs = 0;
	if (shard->recycle)
		refs = shard->recycle(shard->arg);

	if (refs) {
		atomic_add(&shard->refs, refs);
		atomic_add(&bp->refs, refs);
	}
	else
		atomic_set(&shard->full, false);
}

static void bufferpool_dealloc(struct bpool_shard *shard) {
	struct bufferpool *bp = shard->bp;
	bp->dealloc(shard->buf);
}

static struct bpool_shard *bufferpool_new_shard(struct bufferpool *bp) {
	void *buf = bp->alloc();
	if (!buf)
		return NULL;

	// all bottom bits must be zero
	assert(((size_t) buf & BUFFERPOOL_BOTTOM_MASK) == 0);

	struct bpool_shard *ret = g_new0(__typeof(*ret), 1);
	ret->bp = bp;
	ret->buf = buf;
	ret->end = buf + BUFFERPOOL_SHARD_SIZE;

	struct bpool_shard **head = buf;
	*head = ret;

	static_assert(BUFFERPOOL_ALIGN(sizeof(void *)) == BUFFERPOOL_OVERHEAD,
			"wrong BUFFERPOOL_OVERHEAD size");
	buf += BUFFERPOOL_ALIGN(sizeof(void *));

	ret->empty = buf;
	ret->head = buf;

	return ret;
}

static void bpool_shard_destroy(struct bpool_shard *shard) {
	bufferpool_dealloc(shard);
	g_free(shard);
}

// called when references drop to zero
static void __bufferpool_destroy(struct bufferpool *bp) {
	for (unsigned int i = 0; i < bp->num_shards; i++) {
		struct bpool_shard *shard = bp->shards[i];
		bpool_shard_destroy(shard);
	}

	g_free(bp->shards);
	rwlock_destroy(&bp->shards_lock);
	g_free(bp);
}

// may destroy bufferpool
static inline void __bufferpool_unref_n(struct bufferpool *bp, unsigned int n) {
	assert(atomic_get_na(&bp->refs) >= n);

	unsigned int refs = atomic_sub(&bp->refs, n);
	if (refs != n)
		return;

	// no more references
	__bufferpool_destroy(bp);
}

static inline void __bufferpool_unref(struct bufferpool *bp) {
	__bufferpool_unref_n(bp, 1);
}

static void bufferpool_shard_unref(struct bpool_shard *shard) {
	assert(atomic_get_na(&shard->refs) != 0);

	bool full = atomic_get(&shard->full);

	unsigned int refs = atomic_dec(&shard->refs);
	// if shard was set to full and this was the last reference, we can recycle
	if (!full || refs != 1)
		return;

	// return shard to empty list (or reserve again)
	bufferpool_recycle(shard);
}

// must hold reference on the bufferpool
// must hold the lock in R, may intermittently be released
static struct bpool_shard *bufferpool_make_shard(struct bufferpool *bp) {
	struct bpool_shard *shard = bufferpool_new_shard(bp);
	if (!shard) // epic fail
		return NULL;

	// Find a place to insert it
	while (true) {
		unsigned int idx = atomic_get_na(&bp->num_shards);

		// Is there room to insert?
		if (idx < bp->max_shards) {
			// Attempt to insert. Slot must be empty
			struct bpool_shard *expected = NULL;
			if (!atomic_compare_exchange(&bp->shards[idx], &expected, shard))
				continue; // Somebody beat us to it. Try again

			// Success. Record the new count
			atomic_set(&bp->num_shards, idx + 1);

			// We now definitely have a new empty shard. Tell everybody to use it
			// and return success
			atomic_set_na(&bp->empty_shard_idx, idx);

			return shard;
		}

		// Out of room. Now it gets difficult. We must resize
		unsigned int old_size = bp->max_shards;

		rwlock_unlock_r(&bp->shards_lock);

		// Allocate new array first
		unsigned int new_size = old_size * 2;
		struct bpool_shard **new_shards = g_new(struct bpool_shard *, new_size);

		rwlock_lock_w(&bp->shards_lock);

		// Double check, somebody might have beaten us
		if (bp->max_shards != old_size) {
			// OK, just try again
			rwlock_unlock_w(&bp->shards_lock);
			g_free(new_shards);
			rwlock_lock_r(&bp->shards_lock);
			continue;
		}

		// Copy, initialise, and swap
		memcpy(new_shards, bp->shards, sizeof(*bp->shards) * old_size);
		memset(new_shards + old_size, 0, sizeof(*bp->shards) * (new_size - old_size));
		struct bpool_shard **old_shards = bp->shards;
		bp->shards = new_shards;
		bp->max_shards = new_size;

		rwlock_unlock_w(&bp->shards_lock);

		g_free(old_shards);

		// OK, now try again
		rwlock_lock_r(&bp->shards_lock);
	}
}

void *bufferpool_alloc(struct bufferpool *bp, size_t len) {
	len = BUFFERPOOL_ALIGN(len);

	if (len > BUFFERPOOL_SHARD_SIZE - BUFFERPOOL_OVERHEAD)
		return NULL;

	atomic_inc(&bp->refs);

	// Check existing shards if one has enough room. If not, create a new one

	rwlock_lock_r(&bp->shards_lock);

	// Outer loop: To retry after creating a new shard if it was needed
	while (true) {
		unsigned int idx = atomic_get_na(&bp->empty_shard_idx);
		unsigned int start = idx;

		// Inner loop: To cycle through all existing shards, looking for room
		while (true) {
			if (idx >= atomic_get_na(&bp->num_shards)) {
				if (idx == 0)
					break; // we don't have any shards
				if (start == 0)
					break; // circled around, found nothing
				idx = 0;
			}

			struct bpool_shard *shard = atomic_get_na(&bp->shards[idx]);

			// Only attempt allocation if known not to be full. This comes first
			if (!atomic_get(&shard->full)) {
				// Register as a reference
				atomic_inc(&shard->refs);

				// Attempt to allocate
				void *ret = atomic_add(&shard->head, len);

				// Was the allocation successful? (Shard not full)
				if (ret + len <= shard->end) {
					rwlock_unlock_r(&bp->shards_lock);

					// remember empty index for next user
					if (idx != start)
						atomic_set_na(&bp->empty_shard_idx, idx);
					return ret;
				}

				// Shard full. Go to next one and try again
				// Set to full first, then drop reference
				atomic_set(&shard->full, true);
				bufferpool_shard_unref(shard);
			}

			idx++;
			if (idx == start)
				break; // exhausted all our options
		}

		// Found nothing. Must create new shard and put it into the array
		if (!bufferpool_make_shard(bp)) {
			// disaster struck
			rwlock_unlock_r(&bp->shards_lock);
			__bufferpool_unref(bp);
			return NULL;
		}
	}
}

// Get a completely empty shard. Create one if needed.
// XXX can be improved to avoid always using entire shards?
// XXX doesn't currently mix with alloc/unref because of "full" being racy
void *bufferpool_reserve(struct bufferpool *bp, unsigned int refs, unsigned int (*recycle)(void *), void *arg) {
	atomic_add(&bp->refs, refs);
	rwlock_lock_r(&bp->shards_lock);

	struct bpool_shard *shard = bufferpool_make_shard(bp);

	if (!shard) {
		// disaster struck
		rwlock_unlock_r(&bp->shards_lock);
		__bufferpool_unref(bp);
		return NULL;
	}

	// set references, set recycle callback
	assert(atomic_get_na(&shard->refs) == 0);
	atomic_set(&shard->refs, refs);

	atomic_set(&shard->full, true);
	shard->recycle = recycle;
	shard->arg = arg;

	return shard->empty;
}

static struct bpool_shard *bpool_find_shard(void *p) {
	struct bpool_shard **head = (struct bpool_shard **) ((size_t) p & BUFFERPOOL_TOP_MASK);
	return *head;
}

void bufferpool_unref(void *p) {
	if (!p)
		return;

	struct bpool_shard *shard = bpool_find_shard(p);
	if (!shard) // should only happen during shutdown
		return;

	bufferpool_shard_unref(shard);
	__bufferpool_unref(shard->bp);
}

// currently called from synchronous context relative to bufferpool_destroy, so no need
// to check for delayed destruction
void bufferpool_release(void *p) {
	if (!p)
		return;

	struct bpool_shard *shard = bpool_find_shard(p);

	unsigned int refs = atomic_exchange_na(&shard->refs, 0);
	__bufferpool_unref_n(shard->bp, refs);
}

void *bufferpool_ref(void *p) {
	if (!p)
		return NULL;

	struct bpool_shard *shard = bpool_find_shard(p);

	assert(atomic_get_na(&shard->refs) != 0);

	atomic_inc(&shard->refs);
	atomic_inc(&shard->bp->refs);

	return p;
}

void bufferpool_destroy(struct bufferpool *bp) {
	__bufferpool_unref(bp);
}

void bufferpool_init(void) {
}

void bufferpool_cleanup(void) {
}

void *bufferpool_aligned_alloc(void) {
	void *m = aligned_alloc(BUFFERPOOL_SHARD_SIZE, BUFFERPOOL_SHARD_SIZE);
	assert(m != NULL);
	return m;
}

void bufferpool_aligned_free(void *p) {
	free(p);
}