00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdlib.h>
00033
00034 typedef struct s_SF_EVENTQ_NODE
00035 {
00036 void *event;
00037
00038 struct s_SF_EVENTQ_NODE *prev;
00039 struct s_SF_EVENTQ_NODE *next;
00040
00041 } SF_EVENTQ_NODE;
00042
00043 typedef struct s_SF_EVENTQ
00044 {
00045
00046
00047
00048
00049 SF_EVENTQ_NODE *head;
00050 SF_EVENTQ_NODE *last;
00051
00052 SF_EVENTQ_NODE *node_mem;
00053 char *event_mem;
00054
00055
00056
00057
00058
00059
00060
00061 char *reserve_event;
00062
00063
00064
00065
00066 int max_nodes;
00067 int log_nodes;
00068 int event_size;
00069
00070
00071
00072
00073
00074 int (*sort)(void *event1, void *event2);
00075
00076
00077
00078
00079
00080 int cur_nodes;
00081 int cur_events;
00082
00083 } SF_EVENTQ;
00084
00085 static SF_EVENTQ s_eventq;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 int sfeventq_init(int max_nodes, int log_nodes, int event_size,
00103 int (*sort)(void *, void *))
00104 {
00105 if(max_nodes <= 0 || log_nodes <= 0 || event_size <= 0 || !sort)
00106 return -1;
00107
00108
00109
00110
00111 s_eventq.node_mem =
00112 (SF_EVENTQ_NODE *)malloc(sizeof(SF_EVENTQ_NODE)*max_nodes);
00113 if(!s_eventq.node_mem)
00114 return -1;
00115
00116 s_eventq.event_mem = (char *)malloc(event_size*(max_nodes+1));
00117 if(!s_eventq.event_mem)
00118 return -1;
00119
00120 s_eventq.max_nodes = max_nodes;
00121 s_eventq.log_nodes = log_nodes;
00122 s_eventq.event_size = event_size;
00123 s_eventq.sort = sort;
00124 s_eventq.cur_nodes = 0;
00125 s_eventq.cur_events = 0;
00126 s_eventq.reserve_event =
00127 (void *)(&s_eventq.event_mem[max_nodes*s_eventq.event_size]);
00128
00129 return 0;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 void *sfeventq_event_alloc(void)
00149 {
00150 void *event;
00151
00152 if(s_eventq.cur_events >= s_eventq.max_nodes)
00153 {
00154 if(!s_eventq.reserve_event)
00155 return NULL;
00156
00157 event = (void *)s_eventq.reserve_event;
00158 s_eventq.reserve_event = NULL;
00159
00160 return event;
00161 }
00162
00163 event =
00164 (void *)(&s_eventq.event_mem[s_eventq.cur_events*s_eventq.event_size]);
00165
00166 s_eventq.cur_events++;
00167
00168
00169 return event;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void sfeventq_reset(void)
00183 {
00184 s_eventq.head = NULL;
00185 s_eventq.cur_nodes = 0;
00186 s_eventq.cur_events = 0;
00187 s_eventq.reserve_event =
00188 (void *)(&s_eventq.event_mem[s_eventq.max_nodes*s_eventq.event_size]);
00189
00190 return;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 static SF_EVENTQ_NODE *get_eventq_node(void *event)
00214 {
00215 SF_EVENTQ_NODE *node;
00216
00217 if(s_eventq.cur_nodes >= s_eventq.max_nodes)
00218 {
00219
00220
00221
00222
00223 if(!s_eventq.sort(event, s_eventq.last->event))
00224 {
00225 s_eventq.reserve_event = event;
00226 return NULL;
00227 }
00228
00229 node = s_eventq.last;
00230
00231
00232
00233
00234 s_eventq.reserve_event = node->event;
00235 node->event = event;
00236
00237 if(s_eventq.last->prev)
00238 {
00239 s_eventq.last = s_eventq.last->prev;
00240 s_eventq.last->next = NULL;
00241 }
00242
00243
00244
00245
00246 return node;
00247 }
00248
00249
00250
00251
00252 return &s_eventq.node_mem[s_eventq.cur_nodes++];
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 int sfeventq_add(void *event)
00271 {
00272 SF_EVENTQ_NODE *node;
00273 SF_EVENTQ_NODE *tmp;
00274
00275 if(!event)
00276 return -1;
00277
00278
00279
00280
00281
00282
00283
00284 node = get_eventq_node(event);
00285 if(!node)
00286 return 0;
00287
00288 node->event = event;
00289 node->next = NULL;
00290 node->prev = NULL;
00291
00292
00293
00294
00295 if(s_eventq.cur_nodes == 1)
00296 {
00297 s_eventq.head = s_eventq.last = node;
00298 return 0;
00299 }
00300
00301
00302
00303
00304 for(tmp = s_eventq.head; tmp; tmp = tmp->next)
00305 {
00306 if(s_eventq.sort(event, tmp->event))
00307 {
00308
00309
00310
00311 if(tmp->prev)
00312 tmp->prev->next = node;
00313 else
00314 s_eventq.head = node;
00315
00316 node->prev = tmp->prev;
00317 node->next = tmp;
00318
00319 tmp->prev = node;
00320
00321 return 0;
00322 }
00323 }
00324
00325
00326
00327
00328 node->prev = s_eventq.last;
00329
00330 s_eventq.last->next = node;
00331 s_eventq.last = node;
00332
00333 return 0;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 int sfeventq_action(int (*action_func)(void *, void *), void *user)
00351 {
00352 SF_EVENTQ_NODE *node;
00353 int logged = 0;
00354
00355 if(!action_func)
00356 return -1;
00357
00358 if(!(s_eventq.head))
00359 return 0;
00360
00361 for(node = s_eventq.head; node; node = node->next)
00362 {
00363 if(logged >= s_eventq.log_nodes)
00364 return 1;
00365
00366 if(action_func(node->event, user))
00367 return -1;
00368
00369 logged++;
00370 }
00371
00372 return 1;
00373 }
00374
00375
00376 #ifdef I_WANT_MY_MAIN
00377
00378 #include <stdio.h>
00379 #include <time.h>
00380
00381 int mysort(void *event1, void *event2)
00382 {
00383 int *e1;
00384 int *e2;
00385
00386 if(!event1 || !event2)
00387 return 0;
00388
00389 e1 = (int *)event1;
00390 e2 = (int *)event2;
00391
00392 if(*e1 < *e2)
00393 return 1;
00394
00395 return 0;
00396 }
00397
00398 int myaction(void *event, void *user)
00399 {
00400 int *e;
00401
00402 if(!event)
00403 return 1;
00404
00405 e = (int *)event;
00406
00407 printf("-- EVENT: %d\n", *e);
00408
00409 return 0;
00410 }
00411
00412 int main(int argc, char **argv)
00413 {
00414 int max_events;
00415 int log_events;
00416 int add_events;
00417 int *event;
00418 int iCtr;
00419
00420 if(argc < 4)
00421 {
00422 printf("-- Not enough args\n");
00423 return 1;
00424 }
00425
00426 max_events = atoi(argv[1]);
00427 if(max_events <= 0)
00428 {
00429 printf("-- max_events invalid.\n");
00430 return 1;
00431 }
00432
00433 log_events = atoi(argv[2]);
00434 if(log_events <= 0)
00435 {
00436 printf("-- log_events invalid.\n");
00437 return 1;
00438 }
00439
00440 add_events = atoi(argv[3]);
00441 if(add_events <= 0)
00442 {
00443 printf("-- add_events invalid.\n");
00444 return 1;
00445 }
00446
00447 if(max_events < log_events)
00448 {
00449 printf("-- log_events greater than max_events\n");
00450 return 1;
00451 }
00452
00453 srandom(time(NULL));
00454
00455 sfeventq_init(max_events, log_events, sizeof(int), mysort);
00456
00457 do
00458 {
00459 printf("-- Event Queue Test --\n\n");
00460
00461 for(iCtr = 0; iCtr < add_events; iCtr++)
00462 {
00463 event = (int *)sfeventq_event_alloc();
00464 if(!event)
00465 {
00466 printf("-- event allocation failed\n");
00467 return 1;
00468 }
00469
00470 *event = (int)(random()%3);
00471
00472 sfeventq_add(event);
00473 printf("-- added %d\n", *event);
00474 }
00475
00476 printf("\n-- Logging\n\n");
00477
00478 if(sfeventq_action(myaction, NULL))
00479 {
00480 printf("-- There was a problem.\n");
00481 return 1;
00482 }
00483
00484 sfeventq_reset();
00485
00486 } while(getc(stdin) < 14);
00487
00488 return 0;
00489 }
00490 #endif