forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapipe.c
615 lines (522 loc) · 14.6 KB
/
datapipe.c
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/**
* @file datapipe.c
* This file implements the sinmple datapipe framework;
* this can be used to filter data and to setup data triggers
* <p>
* Copyright © 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include "datapipe.h"
#include "mce-log.h" /* mce_log(), LL_* */
/**
* Execute the input triggers of a datapipe
*
* @param datapipe The datapipe to execute
* @param indata The input data to run through the datapipe
* @param use_cache USE_CACHE to use data from cache,
* USE_INDATA to use indata
* @param cache_indata CACHE_INDATA to cache the indata,
* DONT_CACHE_INDATA to keep the old data
*/
void execute_datapipe_input_triggers(datapipe_struct *const datapipe,
gpointer const indata,
const data_source_t use_cache,
const caching_policy_t cache_indata)
{
void (*trigger)(gconstpointer const input);
gpointer data;
gint i;
if (datapipe == NULL) {
/* Potential memory leak! */
mce_log(LL_ERR,
"execute_datapipe_input_triggers() called "
"without a valid datapipe");
goto EXIT;
}
data = (use_cache == USE_CACHE) ? datapipe->cached_data : indata;
if (cache_indata == CACHE_INDATA) {
if (use_cache == USE_INDATA) {
if (datapipe->free_cache == FREE_CACHE)
g_free(datapipe->cached_data);
datapipe->cached_data = data;
}
}
for (i = 0; (trigger = g_slist_nth_data(datapipe->input_triggers,
i)) != NULL; i++) {
trigger(data);
}
EXIT:
return;
}
/**
* Execute the filters of a datapipe
*
* @param datapipe The datapipe to execute
* @param indata The input data to run through the datapipe
* @param use_cache USE_CACHE to use data from cache,
* USE_INDATA to use indata
* @return The processed data
*/
gconstpointer execute_datapipe_filters(datapipe_struct *const datapipe,
gpointer indata,
const data_source_t use_cache)
{
gpointer (*filter)(gpointer input);
gpointer data;
gconstpointer retval = NULL;
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"execute_datapipe_filters() called "
"without a valid datapipe");
goto EXIT;
}
data = (use_cache == USE_CACHE) ? datapipe->cached_data : indata;
for (i = 0; (filter = g_slist_nth_data(datapipe->filters,
i)) != NULL; i++) {
gpointer tmp = filter(data);
/* If the data needs to be freed, and this isn't the indata,
* or if we're not using the cache, then free the data
*/
if ((datapipe->free_cache == FREE_CACHE) &&
((i > 0) || (use_cache == USE_INDATA)))
g_free(data);
data = tmp;
}
retval = data;
EXIT:
return retval;
}
/**
* Execute the output triggers of a datapipe
*
* @param datapipe The datapipe to execute
* @param indata The input data to run through the datapipe
* @param use_cache USE_CACHE to use data from cache,
* USE_INDATA to use indata
*/
void execute_datapipe_output_triggers(const datapipe_struct *const datapipe,
gconstpointer indata,
const data_source_t use_cache)
{
void (*trigger)(gconstpointer input);
gconstpointer data;
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"execute_datapipe_output_triggers() called "
"without a valid datapipe");
goto EXIT;
}
data = (use_cache == USE_CACHE) ? datapipe->cached_data : indata;
for (i = 0; (trigger = g_slist_nth_data(datapipe->output_triggers,
i)) != NULL; i++) {
trigger(data);
}
EXIT:
return;
}
/**
* Execute the datapipe
*
* @param datapipe The datapipe to execute
* @param indata The input data to run through the datapipe
* @param use_cache USE_CACHE to use data from cache,
* USE_INDATA to use indata
* @param cache_indata CACHE_INDATA to cache the indata,
* DONT_CACHE_INDATA to keep the old data
* @return The processed data
*/
gconstpointer execute_datapipe(datapipe_struct *const datapipe,
gpointer indata,
const data_source_t use_cache,
const caching_policy_t cache_indata)
{
gconstpointer data = NULL;
if (datapipe == NULL) {
mce_log(LL_ERR,
"execute_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
execute_datapipe_input_triggers(datapipe, indata, use_cache,
cache_indata);
if (datapipe->read_only == READ_ONLY) {
data = indata;
} else {
data = execute_datapipe_filters(datapipe, indata, use_cache);
}
execute_datapipe_output_triggers(datapipe, data, USE_INDATA);
EXIT:
return data;
}
/**
* Append a filter to an existing datapipe
*
* @param datapipe The datapipe to manipulate
* @param filter The filter to add to the datapipe
*/
void append_filter_to_datapipe(datapipe_struct *const datapipe,
gpointer (*filter)(gpointer data))
{
void (*refcount_trigger)(void);
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"append_filter_to_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (filter == NULL) {
mce_log(LL_ERR,
"append_filter_to_datapipe() called "
"without a valid filter");
goto EXIT;
}
if (datapipe->read_only == READ_ONLY) {
mce_log(LL_ERR,
"append_filter_to_datapipe() called "
"on read only datapipe");
goto EXIT;
}
datapipe->filters = g_slist_append(datapipe->filters, filter);
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Remove a filter from an existing datapipe
* Non-existing filters are ignored
*
* @param datapipe The datapipe to manipulate
* @param filter The filter to remove from the datapipe
*/
void remove_filter_from_datapipe(datapipe_struct *const datapipe,
gpointer (*filter)(gpointer data))
{
void (*refcount_trigger)(void);
guint oldlen;
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"remove_filter_from_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (filter == NULL) {
mce_log(LL_ERR,
"remove_filter_from_datapipe() called "
"without a valid filter");
goto EXIT;
}
if (datapipe->read_only == READ_ONLY) {
mce_log(LL_ERR,
"remove_filter_from_datapipe() called "
"on read only datapipe");
goto EXIT;
}
oldlen = g_slist_length(datapipe->filters);
datapipe->filters = g_slist_remove(datapipe->filters, filter);
/* Did we remove any entry? */
if (oldlen == g_slist_length(datapipe->filters)) {
mce_log(LL_DEBUG,
"Trying to remove non-existing filter");
goto EXIT;
}
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Append an input trigger to an existing datapipe
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to add to the datapipe
*/
void append_input_trigger_to_datapipe(datapipe_struct *const datapipe,
void (*trigger)(gconstpointer data))
{
void (*refcount_trigger)(void);
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"append_input_trigger_to_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"append_input_trigger_to_datapipe() called "
"without a valid trigger");
goto EXIT;
}
datapipe->input_triggers = g_slist_append(datapipe->input_triggers,
trigger);
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Remove an input trigger from an existing datapipe
* Non-existing triggers are ignored
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to remove from the datapipe
*/
void remove_input_trigger_from_datapipe(datapipe_struct *const datapipe,
void (*trigger)(gconstpointer data))
{
void (*refcount_trigger)(void);
guint oldlen;
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"remove_input_trigger_from_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"remove_input_trigger_from_datapipe() called "
"without a valid trigger");
goto EXIT;
}
oldlen = g_slist_length(datapipe->input_triggers);
datapipe->input_triggers = g_slist_remove(datapipe->input_triggers,
trigger);
/* Did we remove any entry? */
if (oldlen == g_slist_length(datapipe->input_triggers)) {
mce_log(LL_DEBUG,
"Trying to remove non-existing input trigger");
goto EXIT;
}
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Append an output trigger to an existing datapipe
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to add to the datapipe
*/
void append_output_trigger_to_datapipe(datapipe_struct *const datapipe,
void (*trigger)(gconstpointer data))
{
void (*refcount_trigger)(void);
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"append_output_trigger_to_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"append_output_trigger_to_datapipe() called "
"without a valid trigger");
goto EXIT;
}
datapipe->output_triggers = g_slist_append(datapipe->output_triggers,
trigger);
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Remove an output trigger from an existing datapipe
* Non-existing triggers are ignored
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to remove from the datapipe
*/
void remove_output_trigger_from_datapipe(datapipe_struct *const datapipe,
void (*trigger)(gconstpointer data))
{
void (*refcount_trigger)(void);
guint oldlen;
gint i;
if (datapipe == NULL) {
mce_log(LL_ERR,
"remove_output_trigger_from_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"remove_output_trigger_from_datapipe() called "
"without a valid trigger");
goto EXIT;
}
oldlen = g_slist_length(datapipe->output_triggers);
datapipe->output_triggers = g_slist_remove(datapipe->output_triggers,
trigger);
/* Did we remove any entry? */
if (oldlen == g_slist_length(datapipe->output_triggers)) {
mce_log(LL_DEBUG,
"Trying to remove non-existing output trigger");
goto EXIT;
}
for (i = 0; (refcount_trigger = g_slist_nth_data(datapipe->refcount_triggers, i)) != NULL; i++) {
refcount_trigger();
}
EXIT:
return;
}
/**
* Append a reference count trigger to an existing datapipe
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to add to the datapipe
*/
void append_refcount_trigger_to_datapipe(datapipe_struct *const datapipe,
void (*trigger)(void))
{
if (datapipe == NULL) {
mce_log(LL_ERR,
"append_refcount_trigger_to_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"append_refcount_trigger_to_datapipe() called "
"without a valid trigger");
goto EXIT;
}
datapipe->refcount_triggers = g_slist_append(datapipe->refcount_triggers, trigger);
EXIT:
return;
}
/**
* Remove a reference count trigger from an existing datapipe
* Non-existing triggers are ignored
*
* @param datapipe The datapipe to manipulate
* @param trigger The trigger to remove from the datapipe
*/
void remove_refcount_trigger_from_datapipe(datapipe_struct *const datapipe,
void (*trigger)(void))
{
guint oldlen;
if (datapipe == NULL) {
mce_log(LL_ERR,
"remove_refcount_trigger_from_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
if (trigger == NULL) {
mce_log(LL_ERR,
"remove_refcount_trigger_from_datapipe() called "
"without a valid trigger");
goto EXIT;
}
oldlen = g_slist_length(datapipe->refcount_triggers);
datapipe->refcount_triggers = g_slist_remove(datapipe->refcount_triggers, trigger);
/* Did we remove any entry? */
if (oldlen == g_slist_length(datapipe->refcount_triggers)) {
mce_log(LL_DEBUG,
"Trying to remove non-existing refcount trigger");
goto EXIT;
}
EXIT:
return;
}
/**
* Initialise a datapipe
*
* @param datapipe The datapipe to manipulate
* @param read_only READ_ONLY if the datapipe is read only,
* READ_WRITE if it's read/write
* @param free_cache FREE_CACHE if the cached data needs to be freed,
* DONT_FREE_CACHE if the cache data should not be freed
* @param datasize Pass size of memory to copy,
* or 0 if only passing pointers or data as pointers
* @param initial_data Initial cache content
*/
void setup_datapipe(datapipe_struct *const datapipe,
const read_only_policy_t read_only,
const cache_free_policy_t free_cache,
const gsize datasize, gpointer initial_data)
{
if (datapipe == NULL) {
mce_log(LL_ERR,
"setup_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
datapipe->filters = NULL;
datapipe->input_triggers = NULL;
datapipe->output_triggers = NULL;
datapipe->refcount_triggers = NULL;
datapipe->datasize = datasize;
datapipe->read_only = read_only;
datapipe->free_cache = free_cache;
datapipe->cached_data = initial_data;
EXIT:
return;
}
/**
* Deinitialize a datapipe
*
* @param datapipe The datapipe to manipulate
*/
void free_datapipe(datapipe_struct *const datapipe)
{
if (datapipe == NULL) {
mce_log(LL_ERR,
"free_datapipe() called "
"without a valid datapipe");
goto EXIT;
}
/* Warn about still registered filters/triggers */
if (datapipe->filters != NULL) {
mce_log(LL_INFO,
"free_datapipe() called on a datapipe that "
"still has registered filter(s)");
}
if (datapipe->input_triggers != NULL) {
mce_log(LL_INFO,
"free_datapipe() called on a datapipe that "
"still has registered input_trigger(s)");
}
if (datapipe->output_triggers != NULL) {
mce_log(LL_INFO,
"free_datapipe() called on a datapipe that "
"still has registered output_trigger(s)");
}
if (datapipe->refcount_triggers != NULL) {
mce_log(LL_INFO,
"free_datapipe() called on a datapipe that "
"still has registered refcount_trigger(s)");
}
if (datapipe->free_cache == FREE_CACHE) {
g_free(datapipe->cached_data);
}
EXIT:
return;
}