~pbatch/patchwerk

patchwerk/pool.w -rw-r--r-- 15.0 KiB
9c265356 — paul plan9 additions from Sigrid 3 months ago
                                                                                
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
@** Buffer Pool.
\mkref{pool}
In Patchwerk, audio-rate signals are read and written to a set
of buffers. The buffer pool implements a scalable memory-efficient approach to
handling buffers.

@<Top@> +=
@<Buffer Pool Top@>

@* Data. The buffer pool data consists of a buffer pool, which is a collection
of underlying buffers.

@<Buffer Pool Top@>+=
@<A Single Buffer@>@/
@<The Buffer Pool@>

@ Both the buffer |pw_buffer| and the buffer pool
|pw_bufferpool| are forward-declared as opaque pointers.
@<Type Declarations@>+=
typedef struct pw_buffer pw_buffer;
typedef struct pw_bufferpool pw_bufferpool;

@ A single buffer contained inside of the buffer pool |pw_bufferpool|
is known as a |pw_buffer|.
@<A Single Buffer@>=
struct pw_buffer {
@<Data for a Single Buffer@>
};

@ Every |pw_buffer| contains an unique |id|, which is used with the
|pw_bufferpool| and |pw_stack| to keep track of the last release buffer.
@<Data for a Single Buffer@>+=
int id;

@ The contents of |pw_buffer| are a pointer to the actual buffer,
as well as a counter variable |read|, which keeps track of how many
cables are actively reading from the particular buffer.

@<Data for a Single Buffer@>+=
int read;
PWFLT *buf;

@ A buffer pool is known as a |pw_bufferpool|.
@<The Buffer Pool@>+=
struct pw_bufferpool {
@<Data for a Buffer Pool@>
};

@ The core element of the buffer pool is an array of |buffers|.
@<Data for a Buffer Pool@>+=
pw_buffer *buffers;


@ The size of the buffers is contained is a signed integer called |size|.
@<Data for a Buffer Pool@>+=
int size;

@ The number of actively used buffers is kept track of in the variable
called |nactive|.
@<Data for a Buffer Pool@>+=
int nactive;

@ To speed up search for next available buffers, the indice for the last
freed buffer is stored in a signed variable called |last_free|. If such
information isn't available, the value is set to be negative.
@<Data for a Buffer Pool@>+=
int last_free;

@* Buffer Functions. The following functions below describe operations on
individual buffers.

@ Since the |pw_buffer| is an opaque type, the size of the
it can be known with the function |pw_buffer_size|


@<A Single Buffer@>+=
size_t pw_buffer_size(void)
{
    return sizeof(pw_buffer);
}

@ @<Header@>+=
size_t pw_buffer_size(void);


@ The function |pw_buffer_alloc| allocates memory for a single buffer with
a provided buffer size.
@<A Single Buffer@>+=
void pw_buffer_alloc(pw_patch *patch, pw_buffer *buf, int size)
{
    pw_memory_alloc(patch, sizeof(PWFLT) * size, (void **)&buf->buf);
}
@ @<Header@>+= void pw_buffer_alloc(pw_patch *patch, pw_buffer *buf, int size);

@ The function |pw_buffer_free| frees memory contained inside of the buffer.
@
@<A Single Buffer@>+=
void pw_buffer_free(pw_patch *patch, pw_buffer *buf)
{
    pw_memory_free(patch, (void **)&buf->buf);
}
@ @<Header@>+= void pw_buffer_free(pw_patch *patch, pw_buffer *buf);

@ The function |pw_buffer_init| initializes a |pw_buffer|. This function
is desgined to be called before or after |pw_buffer_alloc|.
@<A Single Buffer@>+=
void pw_buffer_init(pw_buffer *buf)
{
    buf->id = -1;
    pw_buffer_reinit(buf);
}

@ @<Header@>+= void pw_buffer_init(pw_buffer *buf);

@ A buffer can be re-initialized with |pw_buffer_reinit|.
This will zero out variables while retaining the buffer id.
@<A Single Buffer@>+=
void pw_buffer_reinit(pw_buffer *buf)
{
    buf->read = 0;
}

@ @<Header@>+= void pw_buffer_reinit(pw_buffer *buf);

@ The function |pw_buffer_mark| marks the |read| variable by incrementing it.

This only will happen when =read= is a positive or zero
value. Negative values are used for buffers that are held
indefinitely.
@<A Single Buffer@>+=
void pw_buffer_mark(pw_buffer *buf)
{
    if (buf->read >= 0) buf->read++;
}
@ @<Header@>+= void pw_buffer_mark(pw_buffer *buf);

@ The function |pw_buffer_unmark| unmarkes the buffer by
decrementing the |read| variable. If it is already zero,
nothing happens and -2 is returned.

The after the read value has been decreased, it will once
again check to see if the |read| variable is zero. A
zero-value indicates that the buffer
is not being ready by any nodes and is a free agent, and
will return the buffer id. This information is useful for
buffer pool and the buffer stack, as recently
freed values can speed the search process for the next
available free. Otherwise, -1 is returned.

@<A Single Buffer@>+=
int pw_buffer_unmark(pw_buffer *buf)
{
    if(buf->read < 0) return -3;
    if(buf->read == 0) return -2;
    buf->read--;
    if(buf->read == 0) return buf->id;
    return -1;
}

@ @<Header@>+= int pw_buffer_unmark(pw_buffer *buf);

@ The function |pw_buffer_data| returns the buffer pointer.
@<A Single Buffer@>+=
PWFLT * pw_buffer_data(pw_buffer *buf)
{
    return buf->buf;
}

@ @<Header@>+= PWFLT* pw_buffer_data(pw_buffer *buf);

@ When a buffer needs to be read indefinitely, it needs
to be what is known as a held buffer. This is done
with |pw_buffer_hold|.

@<Header@>+= void pw_buffer_hold(pw_buffer *buf);

@ Generally speaking, a buffer is marked to be held buffer
by setting the read parameter to be a negative value. The
convention is to use -1.

@<A Single Buffer@>+=
void pw_buffer_hold(pw_buffer *buf)
{
    buf->read = -1;
}

@ User-space held buffers are noted as having a specific
read value of -2. This can be marked using the function
|pw_buffer_holdu|.

@<Header@>+= void pw_buffer_holdu(pw_buffer *buf);

@
@<A Single Buffer@>+=
void pw_buffer_holdu(pw_buffer *buf)
{
    buf->read = -2;
}

@
@<A Single Buffer@>+=
int pw_buffer_unhold(pw_buffer *buf)
{
    if(buf->read < 0) {
        buf->read = 0;
        return 1;
    }  else {
        return 0;
    }
}
@ @<Header@>+= int pw_buffer_unhold(pw_buffer *buf);

@ TODO: write words here.

@<A Single Buffer@>+=
int pw_buffer_id(pw_buffer *buf)
{
    return buf->id;
}

@ @<Header@>+=
int pw_buffer_id(pw_buffer *buf);


@* Intialization Functions. The following functions below describe operations
related to creation and intialization on the buffer pool.
@<The Buffer Pool@>+=
void pw_bufferpool_init(pw_bufferpool *pool)
{
    pool->size = 0;
    pool->nactive = 0;
    pool->usrnactive = 0;
}

@ @<Header@>+=
void pw_bufferpool_init(pw_bufferpool *pool);


@ The function |pw_bufferpool_create| allocates and initializes a buffer pool.
The arguments required are the number of buffers allocated |nbuf|, then the
block size |blksize|.
@<The Buffer Pool@>+=
void pw_bufferpool_create(pw_patch *patch,
    pw_bufferpool *pool, int nbuf, int blksize)
{
    int i;
    pool->size = nbuf;
    pool->nactive = 0;
    pw_memory_alloc(patch,
        sizeof(pw_buffer) * nbuf,
        (void **)&pool->buffers);
    for(i = 0; i < nbuf; i++) {
        pw_buffer_alloc(patch, &pool->buffers[i], blksize);
        pw_buffer_init(&pool->buffers[i]);
        pool->buffers[i].id = i;
    }
}

@ @<Header@>+=
void pw_bufferpool_create(pw_patch *patch,
    pw_bufferpool *pool, int nbuf, int blksize);

@ @<The Buffer Pool@>+=
void pw_bufferpool_reset(pw_bufferpool *pool)
{
    int i;
    pool->last_free = -1;
    pool->nactive = 0;
    for(i = 0; i < pool->size; i++) {
        if(pool->buffers[i].read >= 0) {
            pw_buffer_reinit(&pool->buffers[i]);
        } else {
            pool->nactive++;
        }
    }
}
@ @<Header@>+=
void pw_bufferpool_reset(pw_bufferpool *pool);

@ The function |pw_bufferpool_destroy| frees any memory allocated by
|pw_bufferpool_create|.
@<The Buffer Pool@>+=
void pw_bufferpool_destroy(pw_patch *patch, pw_bufferpool *pool)
{
    int i;
    for(i = 0; i < pool->size; i++) {
        pw_buffer_free(patch, &pool->buffers[i]);
    }

    if(pool->size > 0) {
        pw_memory_free(patch, (void **)&pool->buffers);
    }
}

@ @<Header@>+=
void pw_bufferpool_destroy(pw_patch *patch, pw_bufferpool *pool);

@ The function |pw_bufferpool_nactive| returns the number of active
buffers in the buffer pool.
@<The Buffer Pool@>+=
int pw_bufferpool_nactive(pw_bufferpool *pool)
{
    return pool->nactive;
}

@ @<Header@>+=
int pw_bufferpool_nactive(pw_bufferpool *pool);

@ The function |pw_bufferpool_unhold| is a wrapper around |pw_buffer_unhold|.
It is used to both unhold the buffer, and update the number of active buffers
in the buffer pool.

Some buffers are not managed by a pool. When they are
initialized, they are given a negative value. Any buffers
with a non-zero id will be skipped.

{\bf Note:} At the moment there are no checks on if the buffer actually
belongs to the buffer pool. The function assumes that the buffer is valid,
making this procedure fragile in the current state.

% TODO: add bufferpool parent inside of pw_buffer and do a pointer comparison

@<The Buffer Pool@>+=
int pw_bufferpool_unhold(pw_bufferpool *pool, pw_buffer *buf)
{
    if (buf->id < 0) return 0;
    if (pw_buffer_unhold(buf)) {
        pool->nactive--;
        return 1;
    } else {
        return 0;
    }
}

@ @<Header@>+=
int pw_bufferpool_unhold(pw_bufferpool *pool, pw_buffer *buf);

@* Finding the next free buffer. The function |pw_bufferpool_nextfree| returns
the next free buffer. This function will return |PW_OK| on success and
|PW_NOT_OK| on failure.
% TODO: add more specific error handling...

@<Header@>+=
int pw_bufferpool_nextfree(pw_bufferpool *pool, pw_buffer **buf);

@
@<The Buffer Pool@>+=
int pw_bufferpool_nextfree(pw_bufferpool *pool, pw_buffer **buf)
{
    int i;
    if(@<Check if Buffer Pool...@>){
        return PW_POOL_FULL;
    } else if(@<Check for previously freed buffer@>) {
        @/@<Use recently freed buffer@>@/
    } else {
        @/@<Use brute force to find next free buffer@>@/
    }

    @<Successful Finalization@>@/
    return PW_OK;
}

@ If the pool is full when the number of actively used buffers
matches the size.

@<Check if Buffer Pool is Full@>=
pool->nactive >= pool->size

@ To avoid brute force lookup found in |@<Use brute force...@>|, there
is a check for any recently freed-up buffers. This is done by checking
the |last_free| variable for a indice greater than or equal to zero.

@<Check for prev...@>=
pool->last_free >= 0

@ Assuming there is indeed a valid value stored in |last_free|, the
buffer from that indice is used as the next free value. Since it is claimed,
it is no longer free, so |last_free| is set to be an arbitray negative value.

@<Use recent...@>=
*buf = &pool->buffers[pool->last_free];
pool->last_free = -1;


@ If all else fails, brute force is used to find the next value. The program
steps through the array of buffers and checks for the next available free
buffer.  A buffer is considered free if the |read| variable is 0.
@<Use brute f...@>=
for(i = 0; i < pool->size; i++) {
    if(pool->buffers[i].read == 0) {
        *buf = &pool->buffers[i];
        break;
    }
}

@ On successful completion of finding an available buffer, the number
of active buffers is incremented by one, and the buffer is marked as
read.
@<Successful Finalization@>=
pool->nactive++;
pw_buffer_mark(*buf);

@* User-space buffers.

Userspace buffers refer to buffers that are explicitely
held at user-level. At the time of writing, this refers to using the
Runt patchwerk interface.

Buffers that are held cannot be re-used until they are explicitely
unheld.
While this is helpful for constructing more complicating patches,
it can be easy enough to forget about a unholding a buffer. If a buffer
fails to be unheld before the end of a patch, that buffer becomes permenantely
lost, and a resource leak occurs as a result of this.

To mitigate so-called "buffer leaks", a user-space buffer is explicitely kept
track of during the population of a new patch. If at the end of a patch
there are still active user-space buffers, then patchwerk has the ability to
issue a warning and potentially close those buffers.

This section outlines the specific data structures and functions associated
with user-space buffers in the buffer pool.

@ A user-space buffer is just like any other buffer, except that it is
kept track of in a special counter variable. This variable is called
|usernactive|.

@<Data for a Buffer Pool@>+=
int usrnactive;

@ The function |pw_bufferpool_holdu| marks a buffer as being held at
user-level. This will be kept track of, and marked separately from other
held buffers. The |usrnactive| counter will move upwards, in addition
to the regular |nactive| counter.

@<The Buffer Pool@>+=
int pw_bufferpool_holdu(pw_bufferpool *pool, pw_buffer *buf)
{
    if(buf == NULL) return PW_NULL_VALUE;
    if(buf->read >= 0) { /* make sure the buffer isn't marked already */
        pw_buffer_holdu(buf);
        pool->usrnactive++;
        pool->nactive++;
        return PW_OK;
    }
    return PW_INVALID_BUFFER;
}

@ @<Header@>+=
int pw_bufferpool_holdu(pw_bufferpool *pool, pw_buffer *buf);

@ The function |pw_bufferpool_unholdu| unholds a buffer being held at
user-level. If the buffer is not marked as being a user-level buffer, the
function returns |PW_INVALID_BUFFER|. Otherwise the buffer is unheld, and the
|nactive| and |usrnactive| fields are respectively decreased.

When buffers have an initialized value of -1, they are
assumed to be self-managed and are ignored. For now, this
will return |PW_OK|. In the future, a |PW_IGNORE| error
code may be introduced if needed.

@<The Buffer Pool@>+=
int pw_bufferpool_unholdu(pw_bufferpool *pool, pw_buffer *buf)
{
    if(buf == NULL) return PW_NULL_VALUE;
    if(buf->id == -1) return PW_OK;
    if(buf->read != -2) return PW_INVALID_BUFFER;
    if(!pw_buffer_unhold(buf)) return PW_NOT_OK;
    pool->nactive--;
    pool->usrnactive--;
    return PW_OK;
}

@ @<Header@>+=
int pw_bufferpool_unholdu(pw_bufferpool *pool, pw_buffer *buf);

@ The function |pw_bufferpool_unholdu_all| unholds all the buffers marked
as being held by the user. If there are no user-level buffers being held,
then the function immediately returns. At the moment, this is a rather
brute-force O(n) operation that simply iterates through all the buffers.

Note that |pw_bufferpool_unholdu| is called on every buffer. This should
be harmless, since the function immediately returns if the buffer is
not a marked user-space buffer.

% TODO: add NOTHING_TO_DO error code when there are no user-level buffers.

@<The Buffer Pool@>+=

int pw_bufferpool_unholdu_all(pw_bufferpool *pool)
{
    int i;
    if(pool->usrnactive == 0) return PW_NOT_OK; /* TODO: better error handling */
    for(i = 0; i < pool->size; i++) {
        pw_bufferpool_unholdu(pool, &pool->buffers[i]);
    }
    return PW_OK;
}

@ @<Header@>+=
int pw_bufferpool_unholdu_all(pw_bufferpool *pool);

@ The function |pw_bufferpool_uactive| returns the number of currently active
userspace buffers.

@<The Buffer Pool@>+=
int pw_bufferpool_uactive(pw_bufferpool *pool)
{
    return pool->usrnactive;
}

@ @<Header@>+=
int pw_bufferpool_uactive(pw_bufferpool *pool);

@ The function |pw_bufferpool_clear_last_free| sets the |last_free| value
in the pool to be negative. This is called by the bhold word in Runt after
popping the buffer off the stack. This is done so that a held buffer is
taken out of the buffer pool.

% TODO: perhaps there is a more streamlined approach for doing this?

@<The Buffer Pool@>+=
void pw_bufferpool_clear_last_free(pw_bufferpool *pool)
{
    pool->last_free = -1;
}

@ @<Header@>+=
void pw_bufferpool_clear_last_free(pw_bufferpool *pool);