summaryrefslogtreecommitdiff
path: root/src/shared/dlt_queue.h
blob: 40eda03a7d4e9551613e5df81441f69e7f270727 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * @licence app begin@
 * Copyright (C) 2012  BMW AG
 *
 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
 *
 * Contributions are licensed to the GENIVI Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
 * \author Lassi Marttala <lassi.lm.marttala@partner.bmw.de> BMW 2012
 *
 * \file dlt_common_cfg.h
 * For further information see http://www.genivi.org/.
 * @licence end@
 */


/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt_queue.h                                                   **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Lassi Marttala <lassi.lm.marttala@partner.bmw.de>             **
**                                                                            **
**  PURPOSE   : Linked list based dynamic queue                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: no                                           **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/

/*******************************************************************************
**                      Author Identity                                       **
********************************************************************************
**                                                                            **
** Initials     Name                       Company                            **
** --------     -------------------------  ---------------------------------- **
**  lm          Lassi Marttala             Eureka GmbH                        **
*******************************************************************************/
#ifndef __DLT_QUEUE_H_
#define __DLT_QUEUE_H_
#include <stdint.h>

/**
 * \brief One item in the linked list
 */
typedef struct {
	void 					*item;
	uint16_t 				item_size;
	struct dlt_queue_item	*next_item;
}dlt_queue_item;

/**
 * \brief Linked list of items
 */
typedef struct {
	dlt_queue_item	*head;
	dlt_queue_item	*tail;
} dlt_queue;

/**
 * \brief Create new empty queue item
 * Allocates a new item and returns a pointer to it
 * @return Pointer to the new item
 */
dlt_queue_item *dlt_queue_create_item();

/**
 * \brief Free an queue item
 * Frees the memory of the queue item
 * @param item Pointer to the item to be freed
 */
void dlt_queue_free_item(dlt_queue_item *item);

/**
 * \brief Create a new empty queue
 * Queue is created with no items in it.
 * User is responsible for calling dlt_queue_free after
 * the queue is not needed anymore.
 * @return New allocated queue
 */
dlt_queue *dlt_queue_create();

/**
 * \brief Delete a queue
 * Free the memory of the queue structure.
 * Queued items are NOT freed. User must first pop all the items
 * to remove the from queue and fere their memory themself.
 * @param queue the queue to be freed
 */
void dlt_queue_free(dlt_queue *queue);

/**
 * \brief Push an item to the end of the queue
 * Add a new item to the queue. It is now the new tail item.
 * Queue does not take ownership of the item. User still must
 * handle the freeing of the memory.
 * @param item New item to add to the queue
 * @param queue Queue to add the item to.
 */
void dlt_queue_push(dlt_queue_item *item, dlt_queue *queue);

/**
 * \brief Pop an item from the head of the queue
 * Remove an item from the head of the queue and return it.
 * @param queue Queue to pop from
 * @return Pointer to the popped item
 */
dlt_queue_item *dlt_queue_pop(dlt_queue *queue);

/**
 * \brief Checks if the queue is empty
 * Checks if the head and and tail are null pointers
 * and returns the result.
 * @param queue Queue to check
 * @return 1 if queue is empty, 0 if not.
 */
int dlt_queue_is_empty(dlt_queue *queue);

/**
 * \brief Counts the items in the queue
 * Walks the item chain and reports the number of items in
 * the queue. If you only need to know if there are items
 * or not, please use dlt_queue_is_empty, which is cheaper
 * than counting the items.
 * @param queue Queue to check
 * @return number of items.
 */
int dlt_queue_item_count(dlt_queue *queue);
#endif