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
|
/* Minified version of code from tinydtls 0.9 */
/* See https://projects.eclipse.org/projects/iot.tinydtls */
#include "utlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int clock_time_t;
typedef struct netq_t {
struct netq_t *next;
clock_time_t t;
} netq_t;
void dump_queue(struct netq_t *queue)
{
struct netq_t *p;
int i = 0;
if (!queue) {
printf("(null)\n");
} else {
LL_FOREACH(queue, p) {
printf("node #%d, timeout: %d\n", i++, p->t);
}
}
}
int netq_insert_node(netq_t **queue, netq_t *node)
{
netq_t *p = *queue;
while (p && p->t <= node->t) {
p = p->next;
}
/* *INDENT-OFF* */
if (p)
LL_PREPEND_ELEM(*queue, p, node);
else
LL_APPEND(*queue, node);
/* *INDENT-ON* */
return 1;
}
int main()
{
struct netq_t *nq = NULL;
size_t i;
clock_time_t timestamps[] = { 300, 100, 200, 400, 500 };
for (i = 0; i < sizeof(timestamps)/sizeof(clock_time_t); i++) {
struct netq_t *node = (struct netq_t *)malloc(sizeof *node);
memset(node, '\0', sizeof *node);
node->t = timestamps[i];
if (netq_insert_node(&nq, node) != 1) {
puts("ERROR");
}
}
dump_queue(nq);
return 0;
}
|