libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
coap_list.c
Go to the documentation of this file.
1 /* coap_list.c -- CoAP list structures
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "config.h"
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include "debug.h"
15 #include "mem.h"
16 #include "coap_list.h"
17 
18 int
20  int (*order)(void *, void *node) ) {
21  coap_list_t *p, *q;
22  if ( !queue || !node )
23  return 0;
24 
25  /* set queue head if empty */
26  if ( !*queue ) {
27  *queue = node;
28  return 1;
29  }
30 
31  /* replace queue head if new node has to be added before the existing queue head */
32  q = *queue;
33  if ( order( node->data, q->data ) < 0) {
34  node->next = q;
35  *queue = node;
36  return 1;
37  }
38 
39  /* search for right place to insert */
40  do {
41  p = q;
42  q = q->next;
43  } while ( q && order( node->data, q->data ) >= 0);
44 
45  /* insert new item */
46  node->next = q;
47  p->next = node;
48  return 1;
49 }
50 
51 int
53  if ( !node )
54  return 0;
55 
56  if ( node->delete_func )
57  node->delete_func( node->data );
58  coap_free( node->data );
59  coap_free( node );
60 
61  return 1;
62 }
63 
64 void
66  if ( !queue )
67  return;
68 
69  coap_delete_list( queue->next );
70  coap_delete( queue );
71 }
72 
74 coap_new_listnode(void *data, void (*delete_func)(void *) ) {
75  coap_list_t *node = coap_malloc( sizeof(coap_list_t) );
76  if ( ! node ) {
77 #ifndef NDEBUG
78  coap_log(LOG_CRIT, "coap_new_listnode: malloc\n");
79 #endif
80  return NULL;
81  }
82 
83  memset(node, 0, sizeof(coap_list_t));
84  node->data = data;
85  node->delete_func = delete_func;
86  return node;
87 }
88 
int coap_delete(coap_list_t *node)
Definition: coap_list.c:52
int coap_insert(coap_list_t **queue, coap_list_t *node, int(*order)(void *, void *node))
Definition: coap_list.c:19
void(* delete_func)(void *)
Callback function that is called from coap_delete to release additional memory allocated by data Set ...
Definition: coap_list.h:20
struct coap_linkedlistnode * next
Definition: coap_list.h:13
#define coap_malloc(size)
Definition: mem.h:15
void coap_delete_list(coap_list_t *queue)
Definition: coap_list.c:65
#define coap_free(size)
Definition: mem.h:16
coap_list_t * coap_new_listnode(void *data, void(*delete_func)(void *))
Creates a new list node and adds the given data object.
Definition: coap_list.c:74
#define coap_log(...)
Definition: debug.h:47