OpENer - Open Source EtherNet/IP(TM) I/O Target Stack  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
doublylinkedlist.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2017, Rockwell Automation, Inc.
3  * All rights reserved.
4  *
5  ******************************************************************************/
6 
7 #include "doublylinkedlist.h"
8 
9 #include "opener_user_conf.h"
10 #include <stdio.h> // Needed to define NULL
11 #include <assert.h>
12 
14  NodeMemoryAllocator allocator,
15  NodeMemoryDeallocator deallocator) {
16  list->first = NULL;
17  list->last = NULL;
18  list->allocator = allocator;
19  list->deallocator = deallocator;
20 }
21 
23  DoublyLinkedListNode *iterator = list->first;
24  while(NULL != iterator) {
25  DoublyLinkedListNode *to_delete = iterator;
26  iterator = iterator->next;
27  DoublyLinkedListNodeDestroy(list, &to_delete);
28  }
29 
30  list->first = NULL;
31  list->last = NULL;
32  list->allocator = NULL;
33  list->deallocator = NULL;
34 }
35 
37  const void *const data,
39  allocator) {
40  DoublyLinkedListNode *new_node = (DoublyLinkedListNode *)allocator();
41  new_node->data = (void *)data;
42  return new_node;
43 }
44 
46  DoublyLinkedListNode **node) {
47  OPENER_ASSERT(list->deallocator != NULL)
48  list->deallocator(node);
49 }
50 
52  void *data) {
53  OPENER_ASSERT(list->allocator != NULL)
55  list->allocator);
56  if(NULL == list->first) {
57  list->first = new_node;
58  list->last = new_node;
59  } else {
60  new_node->next = list->first;
61  list->first->previous = new_node;
62  list->first = new_node;
63  }
64 }
65 
67  const void *const data) {
68  OPENER_ASSERT(list->allocator != NULL)
70  list->allocator);
71  if(NULL == list->last) {
72  list->first = new_node;
73  list->last = new_node;
74  } else {
75  new_node->previous = list->last;
76  list->last->next = new_node;
77  list->last = new_node;
78  }
79 }
80 
83  void *data) {
84  OPENER_ASSERT(list->allocator != NULL)
85  if(list->first == node) {
86  DoublyLinkedListInsertAtHead(list, data);
87  } else {
89  list->allocator);
90  new_node->previous = node->previous;
91  new_node->next = node;
92  node->previous = new_node;
93  new_node->previous->next = new_node;
94  }
95 }
96 
99  void *data) {
100  OPENER_ASSERT(list->allocator != NULL)
101  if(list->last == node) {
102  DoublyLinkedListInsertAtTail(list, data);
103  } else {
105  list->allocator);
106  new_node->previous = node;
107  new_node->next = node->next;
108  node->next->previous = new_node;
109  node->next = new_node;
110  }
111 }
112 
114  DoublyLinkedListNode **pointer_to_node_pointer)
115 {
116  DoublyLinkedListNode *node = *pointer_to_node_pointer;
117  DoublyLinkedListNode *previous = node->previous;
118  DoublyLinkedListNode *next = node->next;
119 
120  if (node == list->first && node == list->last) {
121  list->first = NULL;
122  list->last = NULL;
123  } else {
124  if(node == list->first) {
125  list->first = next;
126  }
127  if(node == list->last) {
128  list->last = previous;
129  }
130  if(NULL != previous) {
131  previous->next = next;
132  }
133  if(NULL != next) {
134  next->previous = previous;
135  }
136 
137  }
138 
139 
140  DoublyLinkedListNodeDestroy(list, pointer_to_node_pointer);
141 }
void DoublyLinkedListInsertAtHead(DoublyLinkedList *const list, void *data)
DoublyLinkedListNode * first
#define OPENER_ASSERT(assertion)
DoublyLinkedListNode * previous
NodeMemoryAllocator allocator
void DoublyLinkedListRemoveNode(DoublyLinkedList *const list, DoublyLinkedListNode **pointer_to_node_pointer)
DoublyLinkedListNode * next
void DoublyLinkedListInsertAfterNode(DoublyLinkedList *const list, DoublyLinkedListNode *node, void *data)
void DoublyLinkedListNodeDestroy(const DoublyLinkedList *const list, DoublyLinkedListNode **node)
void DoublyLinkedListDestroy(DoublyLinkedList *list)
DoublyLinkedListNode * DoublyLinkedListNodeCreate(const void *const data, NodeMemoryAllocator const allocator)
void DoublyLinkedListInitialize(DoublyLinkedList *list, NodeMemoryAllocator allocator, NodeMemoryDeallocator deallocator)
void(* NodeMemoryDeallocator)(DoublyLinkedListNode **node)
void DoublyLinkedListInsertAtTail(DoublyLinkedList *const list, const void *const data)
NodeMemoryDeallocator deallocator
void DoublyLinkedListInsertBeforeNode(DoublyLinkedList *const list, DoublyLinkedListNode *node, void *data)
DoublyLinkedListNode *(* NodeMemoryAllocator)()
DoublyLinkedListNode * last