CDI SDK
SDK for transporting chunks of data reliably and with low latency using a polled mode network driver.
Loading...
Searching...
No Matches
list_api.h
Go to the documentation of this file.
1// -------------------------------------------------------------------------------------------
2// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3// This file is part of the AWS CDI-SDK, licensed under the BSD 2-Clause "Simplified" License.
4// License details at: https://github.com/aws/aws-cdi-sdk/blob/mainline/LICENSE
5// -------------------------------------------------------------------------------------------
6
14#ifndef CDI_LIST_API_H__
15#define CDI_LIST_API_H__
16
17#include "utilities_api.h"
18
19#include <assert.h>
20#include <stddef.h>
21#include <stdbool.h>
22
23#include "cdi_os_api.h"
24
25//*********************************************************************************************************************
26//***************************************** START OF DEFINITIONS AND TYPES ********************************************
27//*********************************************************************************************************************
28
38
42typedef struct {
44 unsigned int count;
45} CdiList;
46
54
55//*********************************************************************************************************************
56//*********************************************** START OF VARIABLES **************************************************
57//*********************************************************************************************************************
58
59//*********************************************************************************************************************
60//******************************************* START OF STATIC FUNCTIONS ***********************************************
61//*********************************************************************************************************************
62
63//*********************************************************************************************************************
64//******************************************* START OF PUBLIC FUNCTIONS ***********************************************
65//*********************************************************************************************************************
66
75void CdiListInit(CdiList* list_ptr);
76
84static inline CdiListEntry* CdiListGetHead(CdiList* list_ptr)
85{
86 return &list_ptr->head_entry;
87}
88
96static inline bool CdiListIsEmpty(const CdiList* list_ptr)
97{
98 return (0 >= list_ptr->count);
99}
100
108static inline void CdiListAddAfter(CdiList* list_ptr, CdiListEntry* new_entry_ptr, CdiListEntry* prev_entry_ptr)
109{
110 CdiListEntry *next_entry_ptr = prev_entry_ptr->next_ptr;
111 // Update the new entry first, then insert it into the list. This allows multi-threaded access to read the list.
112 new_entry_ptr->next_ptr = next_entry_ptr;
113 new_entry_ptr->prev_ptr = prev_entry_ptr;
114
115 next_entry_ptr->prev_ptr = new_entry_ptr;
116 prev_entry_ptr->next_ptr = new_entry_ptr;
117 list_ptr->count++;
118}
119
128static inline void CdiListAddBefore(CdiList* list_ptr, CdiListEntry* new_entry_ptr, CdiListEntry* next_entry_ptr)
129{
130 CdiListEntry *prev_entry_ptr = next_entry_ptr->prev_ptr;
131 // Update the new entry first, then insert it into the list. This allows multi-threaded access to read the list.
132 new_entry_ptr->next_ptr = next_entry_ptr;
133 new_entry_ptr->prev_ptr = prev_entry_ptr;
134
135 next_entry_ptr->prev_ptr = new_entry_ptr;
136 prev_entry_ptr->next_ptr = new_entry_ptr;
137 list_ptr->count++;
138}
139
146static inline void CdiListAddHead(CdiList* list_ptr, CdiListEntry* new_entry_ptr)
147{
148 CdiListAddBefore(list_ptr, new_entry_ptr, list_ptr->head_entry.next_ptr);
149}
150
157static inline void CdiListAddTail(CdiList* list_ptr, CdiListEntry* new_entry_ptr)
158{
159 CdiListAddAfter(list_ptr, new_entry_ptr, list_ptr->head_entry.prev_ptr);
160}
161
169static inline CdiListEntry* CdiListPeek(const CdiList* list_ptr)
170{
171 if (CdiListIsEmpty(list_ptr)) {
172 return NULL;
173 }
174
175 return list_ptr->head_entry.next_ptr;
176}
177
185static inline CdiListEntry* CdiListPeekTail(const CdiList* list_ptr)
186{
187 if (CdiListIsEmpty(list_ptr)) {
188 return NULL;
189 }
190
191 return list_ptr->head_entry.prev_ptr;
192}
193
200static inline void CdiListRemove(CdiList* list_ptr, CdiListEntry* entry_ptr)
201{
202 // CdiListEntries should always point to other entries or point back to themselves. If the next_ptr is NULL then
203 // the CdiListEntry was never added to a list and so should not be removed from the list.
204 if (NULL != entry_ptr->next_ptr) {
205 entry_ptr->next_ptr->prev_ptr = entry_ptr->prev_ptr;
206 entry_ptr->prev_ptr->next_ptr = entry_ptr->next_ptr;
207 entry_ptr->next_ptr = entry_ptr;
208 entry_ptr->prev_ptr = entry_ptr;
209
210 assert(list_ptr->count);
211 list_ptr->count--;
212 }
213}
214
222static inline CdiListEntry* CdiListPop(CdiList* list_ptr)
223{
224 CdiListEntry* first_ptr;
225
226 if (CdiListIsEmpty(list_ptr)) {
227 return NULL;
228 }
229
230 first_ptr = list_ptr->head_entry.next_ptr;
231 CdiListRemove(list_ptr, first_ptr);
232
233 return first_ptr;
234}
235
243static inline int CdiListCount(const CdiList* list_ptr)
244{
245 return list_ptr->count;
246}
247
255static inline void CdiListIteratorInit(CdiList* list_ptr, CdiListIterator* ret_iterator_ptr)
256{
257 ret_iterator_ptr->head_ptr = CdiListGetHead(list_ptr);
258 ret_iterator_ptr->next_ptr = CdiListPeek(list_ptr);
259}
260
269{
270 CdiListEntry* ret_entry_ptr = iterator_ptr->next_ptr;
271
272 // Don't walk an empty list.
273 if (ret_entry_ptr) {
274 // If at head of the list, then no more entries, so use NULL.
275 if (ret_entry_ptr->next_ptr == iterator_ptr->head_ptr) {
276 iterator_ptr->next_ptr = NULL;
277 } else {
278 iterator_ptr->next_ptr = ret_entry_ptr->next_ptr;
279 }
280 }
281
282 return ret_entry_ptr;
283}
284
285#endif // CDI_LIST_API_H__
This file contains the declarations for OS functions for creating/managing/freeing threads,...
static bool CdiListIsEmpty(const CdiList *list_ptr)
Definition list_api.h:96
static void CdiListAddTail(CdiList *list_ptr, CdiListEntry *new_entry_ptr)
Definition list_api.h:157
void CdiListInit(CdiList *list_ptr)
Definition list.c:38
static CdiListEntry * CdiListPop(CdiList *list_ptr)
Definition list_api.h:222
static void CdiListIteratorInit(CdiList *list_ptr, CdiListIterator *ret_iterator_ptr)
Definition list_api.h:255
static void CdiListAddBefore(CdiList *list_ptr, CdiListEntry *new_entry_ptr, CdiListEntry *next_entry_ptr)
Definition list_api.h:128
static CdiListEntry * CdiListPeekTail(const CdiList *list_ptr)
Definition list_api.h:185
static void CdiListRemove(CdiList *list_ptr, CdiListEntry *entry_ptr)
Definition list_api.h:200
static void CdiListAddHead(CdiList *list_ptr, CdiListEntry *new_entry_ptr)
Definition list_api.h:146
static void CdiListAddAfter(CdiList *list_ptr, CdiListEntry *new_entry_ptr, CdiListEntry *prev_entry_ptr)
Definition list_api.h:108
static CdiListEntry * CdiListPeek(const CdiList *list_ptr)
Definition list_api.h:169
static int CdiListCount(const CdiList *list_ptr)
Definition list_api.h:243
static CdiListEntry * CdiListIteratorGetNext(CdiListIterator *iterator_ptr)
Definition list_api.h:268
static CdiListEntry * CdiListGetHead(CdiList *list_ptr)
Definition list_api.h:84
This structure represents a single list entry.
Definition list_api.h:34
CdiListEntry * prev_ptr
Pointer to previous item in list. If no items in list, will point to itself.
Definition list_api.h:36
CdiListEntry * next_ptr
Pointer to next item in list. If no items in list, will point to itself.
Definition list_api.h:35
This structure represents a list iterator.
Definition list_api.h:50
CdiListEntry * next_ptr
Pointer to next item in list. If no items in list, will point to head_entry_ptr.
Definition list_api.h:52
CdiListEntry * head_ptr
Pointer to head entry of list.
Definition list_api.h:51
This structure represents a list.
Definition list_api.h:42
unsigned int count
Number of entries in the list (used for convenience).
Definition list_api.h:44
CdiListEntry head_entry
Head entry of list item. Only valid if count >= 1.
Definition list_api.h:43
The declarations in this header file contain utility definitions and function prototypes that are use...