-
Notifications
You must be signed in to change notification settings - Fork 1
/
CTI.c
853 lines (695 loc) · 20.7 KB
/
CTI.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#include <stdio.h> /* fprintf */
#include <stdlib.h> /* calloc */
#include <stdint.h> /* uint8_t */
#include <string.h> /* strcmp */
#include <unistd.h> /* sleep */
#include <time.h> /* nanosleep */
#include <sys/time.h> /* gettimeofday */
#include <stdarg.h> /* vprintf */
#ifdef __linux__
#include <sys/prctl.h> /* prctl */
#endif
#ifndef streq
#define streq(a, b) (strcmp(a, b) == 0)
#endif
#include "CTI.h"
#include "Mem.h"
#include "Cfg.h"
int (*ui_main)(int argc, char *argv[]) = NULL;
pthread_key_t instance_key;
int instance_key_initialized;
int g_synchronous = 0; /* Can be toggled in ScriptV00.c */
/* instance_key_init() should be called only once, by the main thread
in a CTI application, before any instance threads are
created. Following these rules obviates the need for
pthread_once(). */
void instance_key_init(void)
{
pthread_key_create(&instance_key, NULL); /* No destructor function. */
pthread_setspecific(instance_key, NULL); /* Pass NULL for main thread. */
instance_key_initialized = 1;
}
void CTI_register_instance(Instance *pi)
{
#ifdef __linux__
prctl(PR_SET_NAME, pi->label);
#endif
pthread_setspecific(instance_key, (void*)pi); /* For later retrieval */
}
static void * Instance_thread_main(void *vp)
{
Instance *pi = vp;
CTI_register_instance(pi);
while (1) {
pi->tick(pi);
}
return 0L;
}
void Instance_loop_thread(Instance *pi)
{
/* FIXME: Abstract this out one layer... */
pthread_t thread;
pthread_create(&thread, NULL, Instance_thread_main, (void*)pi);
pthread_detach(thread);
}
void PostDataGetResult(void *data, Input *input, int * result)
{
Handler_message *hm = Mem_calloc(1, sizeof(*hm));
if (!input->handler) {
/* Error, send calling thread to tar pit... */
while (1) {
fprintf(stderr, "instance %s input %s does not have a handler!\n",
input->parent->label, input->type_label);
sleep(1);
}
}
Lock reply_lock;
Sem reply_sem;
hm->handler = input->handler;
hm->data = data;
if (result) {
Sem_init(&reply_sem);
Lock_init(&reply_lock);
Lock_acquire(&reply_lock);
hm->reply_sem = &reply_sem;
hm->result = result;
}
{
/* Wait for input lock, lock... */
Lock_acquire(&input->parent->inputs_lock);
{
/* Add node to list. */
if (input->parent->msg_first == 0L) {
/* input->parent->msg_last should also be 0L in this case... */
input->parent->msg_first = hm;
input->parent->msg_last = hm;
}
else {
/* At least one node. */
input->parent->msg_last->prev = hm;
input->parent->msg_last = hm;
}
}
if (input->parent->waiting) {
Event_signal(&input->parent->inputs_event);
}
input->parent->pending_messages += 1;
if (input->parent->pending_messages > 5) {
dpf("%s (%p): %d queued messages\n",
input->parent->label,
input,
input->parent->pending_messages);
}
Lock_release(&input->parent->inputs_lock);
}
while (input->parent->pending_messages > cfg.max_pending_messages) {
/* This one indicates a configuration problem or misestimation, so
make it an fprintf instead of a dpf. */
fprintf(stderr, "sleeping to drain %s message queue (%p) (%d)\n",
input->parent->label,
input,
input->parent->pending_messages);
sleep(1);
}
if (result) {
/* Wait for message recipient to signal back. */
Sem_wait(&reply_sem);
Sem_destroy(&reply_sem);
Lock_destroy(&reply_lock);
}
}
void PostData(void *data, Input *input)
{
if (!g_synchronous) {
PostDataGetResult(data, input, NULL);
}
else {
int throwaway;
PostDataGetResult(data, input, &throwaway);
}
}
Handler_message *GetData(Instance *pi, int wait_flag)
{
/* Return handler message if available. If no message available, return NULL if
wait_flag is 0, else wait until a message is available and return it. */
Handler_message *hm = 0L;
/* This is a quick hack "pause all the things" implementation. */
while (cfg.pause) {
nanosleep(&(struct timespec){.tv_sec = 0, .tv_nsec = (999999999+1)/50}, NULL);
}
/*
I thought about doing the if (...) check before the
Lock_acquire(), and changing the goto to a return, thus avoiding
the acquire/release cycle when there are no messages pending and
wait flag is zero. But I am unsure about ARM memory behavior, in
particular I worry that memory might not be synchronized properly
without the memory barrier events imposed by the locking, which
could make a thread return when there IS actually data available.
So I am leaving the lock acquire/release. Links,
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472c/CJAEGDEA.html
http://iphonedevsdk.com/forum/iphone-sdk-development/115122-multi-core-arm-and-sharing-global-variable-between-threads.html
*/
Lock_acquire(&pi->inputs_lock);
if (pi->msg_first == 0L && pi->msg_last == 0L && wait_flag == 0) {
goto out;
}
while (pi->msg_first == 0L && pi->msg_last == 0L) {
pi->waiting = 1;
Lock_release__event_wait__lock_acquire(&pi->inputs_lock, &pi->inputs_event);
}
pi->waiting = 0;
{
/* Take node, remove from list. */
if (pi->msg_first == pi->msg_last) {
/* Single node */
hm = pi->msg_first;
hm->prev = 0L;
pi->msg_first = 0L;
pi->msg_last = 0L;
}
else {
/* >1 nodes. */
hm = pi->msg_first;
pi->msg_first = hm->prev;
hm->prev = 0L;
}
}
pi->pending_messages -= 1;
out:
Lock_release(&pi->inputs_lock);
return hm;
}
void ReleaseMessage(Handler_message **msg, Instance *pi)
{
Handler_message *hm = *msg;
if (hm->reply_sem) {
/* Sender is waiting for a reply. */
if (hm->result) {
/* Sender supplied a result destination. */
/* FIXME: Could also provide a string result, or even a binary (ArrayU8) result. */
*(hm->result) = pi->result;
}
Sem_post(hm->reply_sem);
}
Mem_free(*msg);
*msg = 0L;
}
static IndexedSet(Template) templates = {};
void Template_register(Template *t)
{
IndexedSet_add(templates, t);
}
void Template_list(int verbose)
{
int i, j;
printf("Templates:\n");
for (i=0; i < templates.count; i++) {
printf("[%d] %s\n", i, templates.items[i]->label);
if (!verbose) {
continue;
}
printf(" Inputs:\n");
for (j=0; j < templates.items[i]->num_inputs; j++) {
if (templates.items[i]->inputs[j].type_label) {
printf(" [%d] %s\n", j, templates.items[i]->inputs[j].type_label);
}
else {
printf(" [%d] *** UNINITIALIZED\n", j);
}
}
printf(" Outputs:\n");
for (j=0; j < templates.items[i]->num_outputs; j++) {
if (templates.items[i]->outputs[j].type_label) {
printf(" [%d] %s\n", j, templates.items[i]->outputs[j].type_label);
}
else {
printf(" [%d] *** UNINITIALIZED\n", j);
}
}
}
}
static void Instance_print(Index_node *node)
{
Instance * pi = node->value;
printf(" %s (%s)\n", s(node->stringKey), pi->label);
}
static void Instance_print_verbose(Index_node *node)
{
int i;
Instance * pi = node->value;
printf(" %s (%s)\n", s(node->stringKey), pi->label);
for (i=0; i < pi->num_outputs; i++) {
if (pi->outputs[i].destination) {
printf(" %s.%s -> %s.%s\n"
, s(node->stringKey)
, pi->outputs[i].type_label
, s(pi->outputs[i].destination->parent->instance_label)
, pi->outputs[i].destination->type_label
);
}
}
}
void Instance_list(int verbose)
{
/* List instances in global instance group. */
if (gig->instances.index) {
if (verbose) {
Index_walk(gig->instances.index, Instance_print_verbose);
}
else {
Index_walk(gig->instances.index, Instance_print);
}
}
}
static void Instance_pending_messages(Index_node *node)
{
Instance * pi = node->value;
printf("%s (%s) has %d pending messsages\n",
s(node->stringKey), pi->label, pi->pending_messages);
}
void CTI_pending_messages(void)
{
if (gig->instances.index) {
Index_walk(gig->instances.index, Instance_pending_messages);
}
}
static Instance * _Instantiate_local(const char *label, String * instanceLabel, int run)
{
int i;
for (i=0; i < templates.count; i++) {
if (strcmp(templates.items[i]->label, label) == 0) {
Template *t = templates.items[i];
Instance *pi;
if (t->priv_size) {
/* New-style, where priv structure includes Instance member. */
pi = Mem_calloc(1, t->priv_size);
}
else {
pi = Mem_calloc(1, sizeof(*pi));
}
pi->label = t->label;
pi->instance_label = String_dup(instanceLabel);
pi->tick = t->tick;
pi->priv_size = t->priv_size;
/* Inputs and Ouputs are declared statically in each module
file. Here we make a copy and initialize instance-specific
structure fields. */
copy_list(t->inputs, t->num_inputs, &pi->inputs, &pi->num_inputs);
copy_list(t->outputs, t->num_outputs, &pi->outputs, &pi->num_outputs);
/* When a message is posted, the caller needs to find the Input's parent
and add to the notification queue. */
for (i=0; i < t->num_inputs; i++) {
pi->inputs[i].parent = pi;
}
Lock_init(&pi->inputs_lock);
Event_init(&pi->inputs_event);
/* Type-specific instance initialization. */
if (t->instance_init) {
t->instance_init(pi);
}
/* Run or start thread, only if tick function is defined. */
if (pi->tick) {
if (run) {
/* Run instance main loop, blocking the caller. */
Instance_thread_main(pi);
}
else {
/* Start a new thread. */
Instance_loop_thread(pi);
}
}
return pi;
}
}
fprintf(stderr, "No such template: %s\n", label);
return 0L;
}
void Instantiate_and_run(const char *label, String * instanceLabel)
{
_Instantiate_local(label, instanceLabel, 1);
}
Instance * Instantiate(const char *label, String * instanceLabel)
{
return _Instantiate_local(label, instanceLabel, 0);
}
Line_msg *Line_msg_new(const char *value)
{
Line_msg *lm = Mem_calloc(1, sizeof(*lm));
lm->value = String_new(value);
return lm;
}
void Line_msg_release(Line_msg **plm)
{
Line_msg *lm = *plm;
String_free(&lm->value);
Mem_free(lm);
*plm = 0L;
}
Config_buffer *Config_buffer_new(const char *label, const char *value)
{
Config_buffer *cb = Mem_calloc(1, sizeof(*cb));
cb->label = String_new(label);
if (value) {
cb->value = String_new(value);
}
return cb;
}
Config_buffer *Config_buffer_vrreq_new(const char *label, const char *value, Value *vreq, Range *rreq,
Event *event)
{
Config_buffer *cb = Config_buffer_new(label, value);
cb->vreq = vreq;
cb->rreq = rreq;
cb->wake = event;
return cb;
}
void Config_buffer_release(Config_buffer **cb)
{
if ((*cb)->label) {
String_free(&(*cb)->label);
}
if ((*cb)->value) {
String_free(&(*cb)->value);
}
Mem_free(*cb);
*cb = 0L;
}
void Generic_config_handler(Instance *pi, void *data, Config *config_table, int config_table_size)
{
/* Most modules can use this function to handle config messages. */
Config_buffer *cb_in = data;
int i;
/* Walk the config table. */
for (i=0; i < config_table_size; i++) {
if (streq(config_table[i].label, s(cb_in->label))) {
/* If value is passed in, call the set function. */
if (cb_in->value && config_table[i].vset) {
/* Generic setter. */
config_table[i].vset((uint8_t*)pi + config_table[i].value_offset,
s(cb_in->value));
}
else if (cb_in->value && config_table[i].set) {
/* Template-specific setter. */
// int rc = ... /* FIXME: What to do with this? */
config_table[i].set(pi, s(cb_in->value));
}
/* Check and fill range/value requests. */
if (cb_in->vreq && config_table[i].get_value) {
config_table[i].get_value(pi, cb_in->vreq);
}
if (cb_in->rreq && config_table[i].get_range) {
config_table[i].get_range(pi, cb_in->rreq);
}
/* Wake caller if they asked for it. */
if (cb_in->wake) {
Event_signal(cb_in->wake); // FIXME: Is this necessary?
}
break;
}
}
Config_buffer_release(&cb_in);
}
void cti_set_int(void *addr, const char *value)
{
*( (int *)addr) = atoi(value);
}
void cti_set_uint(void *addr, const char *value)
{
*( (unsigned int *)addr) = (unsigned int) strtoul(value, NULL, 0);
}
void cti_set_long(void *addr, const char *value)
{
*( (long *)addr) = atol(value);
}
void cti_set_ulong(void *addr, const char *value)
{
*( (int *)addr) = strtoull(value, NULL, 0);
}
void cti_set_string_local(void *addr, const char *value)
{
String_set_local((String *)addr, value);
}
void cti_set_string(void *addr, const char *value)
{
String_set((String **)addr, value);
}
void GetConfigValue(Input *pi, const char *label, Value *vreq)
{
Lock lock = {};
Event event = {};
Config_buffer *cb;
Lock_init(&lock);
Event_init(&event);
cb = Config_buffer_vrreq_new(label, 0L, vreq, 0L, &event);
Lock_acquire(&lock);
PostData(cb, pi);
Lock_release__event_wait__lock_acquire(&lock, &event);
Event_destroy(&event);
Lock_release(&lock);
}
void GetConfigRange(Input *pi, const char *label, Range *rreq)
{
Lock lock = {};
Event event = {};
Config_buffer *cb;
Lock_init(&lock);
Event_init(&event);
cb = Config_buffer_vrreq_new(label, 0L, 0L, rreq, &event);
Lock_acquire(&lock);
PostData(cb, pi);
Lock_release__event_wait__lock_acquire(&lock, &event);
Event_destroy(&event);
Lock_release(&lock);
}
void Connect(Instance *from, const char *label, Instance *to)
{
int i;
int from_index = -1;
int to_index = -1;
for (i=0; i < from->num_outputs ; i++) {
if (from->outputs[i].type_label && streq(from->outputs[i].type_label, label)) {
from_index = i;
if (from->outputs[i].destination == 0L) {
/* This is a bit subtle. The code will set the output to the first matching
and unset output, or override the last set output. */
break;
}
}
}
if (from_index == -1) {
fprintf(stderr, "Instance of %s does not have an output labelled '%s'\n", from->label, label);
fflush(stderr);
exit(1);
return;
}
for (i=0; i < to->num_inputs ; i++) {
if (to->inputs[i].type_label && streq(to->inputs[i].type_label, label)) {
to_index = i;
break;
}
}
if (to_index == -1) {
fprintf(stderr, "Instance of %s does not have an input labelled '%s'\n", to->label, label);
exit(1);
return;
}
from->outputs[from_index].destination = &to->inputs[to_index];
fprintf(stderr, "connected: %s.%d [%s] %s.%d\n", from->label, from_index, label, to->label, to_index);
}
void Connect2(Instance *from, const char *fromlabel, Instance *to, const char *tolabel)
{
int i;
int from_index = -1;
int to_index = -1;
for (i=0; i < from->num_outputs ; i++) {
if (from->outputs[i].type_label && streq(from->outputs[i].type_label, fromlabel)) {
from_index = i;
if (from->outputs[i].destination == 0L) {
/* This is a bit subtle. The code will set the output to the first matching
and unset output, or override the last set output. */
break;
}
}
}
if (from_index == -1) {
fprintf(stderr, "Instance of %s does not have an output labelled '%s'\n", from->label, fromlabel);
fflush(stderr);
exit(1);
return;
}
for (i=0; i < to->num_inputs ; i++) {
if (to->inputs[i].type_label && streq(to->inputs[i].type_label, tolabel)) {
to_index = i;
break;
}
}
if (to_index == -1) {
fprintf(stderr, "Instance of %s does not have an input labelled '%s'\n", to->label, tolabel);
exit(1);
return;
}
/* Verify that labels match up... */
from->outputs[from_index].destination = &to->inputs[to_index];
fprintf(stderr, "connected: %s:%s %s:%s\n", from->label, fromlabel, to->label, tolabel);
}
InstanceGroup *InstanceGroup_new(void)
{
InstanceGroup *g = Mem_calloc(1, sizeof(*g));
return g;
}
void InstanceGroup_add(InstanceGroup *g, const char *typeLabel, String *instanceLabel)
{
Instance *pi = 0L;
Instance *check;
check = 0L; /* FIXME: Make sure there isn't already an instance
with the same instanceLabel. */
if (check) {
return;
}
pi = Instantiate(typeLabel, instanceLabel);
if (!pi) {
return;
}
/* Add pi to g's set of instances. */
/* Pass "instanceLabel" as "key" parameter, for later lookup. */
int err;
IndexedSet_add_keyed(g->instances, instanceLabel, pi, &err);
}
Instance *InstanceGroup_find(InstanceGroup *g, String *ilabel)
{
if (g->instances.index) {
/* Look up using hash index. */
int found = 0;
Instance *pi = Index_find_string(g->instances.index, ilabel, &found);
return pi;
}
else {
/* Walk the list... */
}
return 0L;
}
void InstanceGroup_connect(InstanceGroup *g,
String * instanceLabel1,
const char *ioLabel,
String * instanceLabel2)
{
Instance *pi1 = InstanceGroup_find(g, instanceLabel1);
if (!pi1) { fprintf(stderr, "could not find instance '%s'\n", s(instanceLabel1)); return; }
Instance *pi2 = InstanceGroup_find(g, instanceLabel2);
if (!pi2) { fprintf(stderr, "could not find instance '%s'\n", s(instanceLabel2)); return; }
Connect(pi1, ioLabel, pi2);
}
void InstanceGroup_connect2(InstanceGroup *g,
String * instanceLabel1,
const char *oLabel,
String * instanceLabel2,
const char *iLabel
)
{
Instance *pi1 = InstanceGroup_find(g, instanceLabel1);
if (!pi1) { fprintf(stderr, "could not find instance '%s'\n", s(instanceLabel1)); return; }
Instance *pi2 = InstanceGroup_find(g, instanceLabel2);
if (!pi2) { fprintf(stderr, "could not find instance '%s'\n", s(instanceLabel2)); return; }
Connect2(pi1, oLabel, pi2, iLabel);
}
void InstanceGroup_wait(InstanceGroup *g)
{
while (1) {
/* FIXME: Wait for all instances to "finish", whatever that turns out to mean. */
nanosleep(&(struct timespec){.tv_sec = 1, .tv_nsec = 0}, NULL);
}
}
void InstanceGroup_free(InstanceGroup **g)
{
/* FIXME: Free Instances. Maybe check their status first? */
Mem_free(*g);
*g = 0L;
}
Value *Value_new(int type)
{
Value *v = Mem_calloc(1, sizeof(*v));
v->type = type;
return v;
}
void Value_free(Value *v)
{
if (v->type == RANGE_STRINGS) {
String_free(&v->u.string_value);
}
Mem_free(v);
}
/* Callback API */
Callback *Callback_new(void)
{
Callback *cb = Mem_calloc(1, sizeof(*cb));
Lock_init(&cb->lock);
Event_init(&cb->event);
return cb;
}
void Callback_wait(Callback *cb)
{
/* This just waits someone else to lock/unlock the lock. */
Lock_acquire(&cb->lock);
Lock_release__event_wait__lock_acquire(&cb->lock, &cb->event);
Lock_release(&cb->lock);
}
void Callback_fill(Callback *cb, int (*func)(void *), void *data)
{
Lock_acquire(&cb->lock);
printf("%s(%p)\n", __func__, func);
cb->func = func;
cb->data = data;
Event_signal(&cb->event); // Is this even necessary? It probably doesn't hurt.
Lock_release(&cb->lock);
}
/* Raw data buffer API. */
RawData_buffer *RawData_buffer_new(int size)
{
RawData_buffer *raw = Mem_malloc(sizeof(*raw));
raw->data_length = size;
raw->data = Mem_calloc(1, raw->data_length); /* Caller must fill in data! */
return raw;
}
void RawData_buffer_release(RawData_buffer *raw)
{
Mem_free(raw->data);
memset(raw, 0, sizeof(*raw));
Mem_free(raw);
}
Feedback_buffer *Feedback_buffer_new(void)
{
Feedback_buffer *fb = Mem_malloc(sizeof(*fb));
/* Caller must fill in fields. */
return fb;
}
void Feedback_buffer_release(Feedback_buffer *fb)
{
Mem_free(fb);
}
/* key=value command-line parameter support. */
typedef struct {
const char *key;
const char *value;
} CmdlineParam;
static IndexedSet(CmdlineParam) cmdline_params;
void CTI_cmdline_add(const char *key, const char *value)
{
CmdlineParam *p = Mem_malloc(sizeof(*p));
p->key = strdup(key);
p->value = strdup(value);
IndexedSet_add(cmdline_params, p);
// printf("%s: found %s=%s, count=%d\n", __func__, key, value, cmdline_params.count);
}
const char *CTI_cmdline_get(const char *key)
{
int i;
/* NOTE: No locking, it is assumed that all parameters are set
before instances start looking for them. */
// printf("%s: looking for key %s\n", __func__, key);
for (i=0; i < cmdline_params.count; i++) {
if (streq(cmdline_params.items[i]->key, key)) {
return cmdline_params.items[i]->value;
}
}
return NULL;
}