Contiki-NG
stack.h
1 /*
2  * Copyright (c) 2017, George Oikonomou - http://www.spd.gr
3  * Copyright (c) 2017, James Pope
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*---------------------------------------------------------------------------*/
33 /**
34  * \addtogroup data
35  * @{
36  *
37  * \defgroup stack-data-structure Stack library
38  *
39  * This library provides functions for the creation and manipulation of
40  * stacks. The library is implemented as a wrapper around the list library.
41  *
42  * A stack is declared using the STACK macro. Stack elements must be
43  * allocated by the calling code and must be of a C struct datatype. In this
44  * struct, the first field must be a pointer called \e next. This field will
45  * be used by the library to maintain the stack. Application code must not
46  * modify this field directly.
47  * @{
48  */
49 /*---------------------------------------------------------------------------*/
50 #ifndef STACK_H_
51 #define STACK_H_
52 /*---------------------------------------------------------------------------*/
53 #include "contiki.h"
54 #include "lib/list.h"
55 
56 #include <stdbool.h>
57 /*---------------------------------------------------------------------------*/
58 /**
59  * \brief The stack data type
60  */
61 typedef list_t stack_t;
62 /*---------------------------------------------------------------------------*/
63 /**
64  * \brief Define a stack.
65  *
66  * This macro defines a stack.
67  *
68  * The datatype for elements must be a C struct. The struct's first member must
69  * be a pointer called \e next. This is used internally by the library to
70  * maintain data structure integrity and must not be modified directly by
71  * application code.
72  *
73  * \param name The name of the stack.
74  */
75 #define STACK(name) LIST(name)
76 /*---------------------------------------------------------------------------*/
77 struct stack {
78  struct stack *next;
79 };
80 /*---------------------------------------------------------------------------*/
81 /**
82  * \brief Initialise a stack
83  * \param stack The stack
84  */
85 static inline void
87 {
88  list_init(stack);
89 }
90 /*---------------------------------------------------------------------------*/
91 /**
92  * \brief Adds an element to the top of the stack
93  * \param stack The stack
94  * \param element A pointer to the element to be added
95  */
96 static inline void
97 stack_push(stack_t stack, void *element)
98 {
99  list_push(stack, element);
100 }
101 /*---------------------------------------------------------------------------*/
102 /**
103  * \brief Removes the top element from the stack
104  * \param stack The stack
105  * \return A pointer to the element popped
106  *
107  * If this function returns NULL if the stack was empty (stack underflow)
108  */
109 static inline void *
111 {
112  return list_pop(stack);
113 }
114 /*---------------------------------------------------------------------------*/
115 /**
116  * \brief Returns the top element of the stack, without popping it
117  * \param stack The stack
118  * \return A pointer to the element at the top of the stack
119  */
120 static inline void *
122 {
123  return list_head(stack);
124 }
125 /*---------------------------------------------------------------------------*/
126 /**
127  * \brief Check if a stack is empty
128  * \param stack The stack
129  * \retval true The stack is empty
130  * \retval false The stack has at least one element
131  */
132 static inline bool
134 {
135  return *stack == NULL ? true : false;
136 }
137 /*---------------------------------------------------------------------------*/
138 #endif /* STACK_H_ */
139 /*---------------------------------------------------------------------------*/
140 /**
141  * @}
142  * @}
143  */
void list_push(list_t list, void *item)
Add an item to the start of the list.
Definition: list.c:164
void ** list_t
The linked list type.
Definition: list.h:135
static void * stack_peek(stack_t stack)
Returns the top element of the stack, without popping it.
Definition: stack.h:121
static void stack_init(stack_t stack)
Initialise a stack.
Definition: stack.h:86
Linked list manipulation routines.
static void * stack_pop(stack_t stack)
Removes the top element from the stack.
Definition: stack.h:110
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
list_t stack_t
The stack data type.
Definition: stack.h:61
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
static bool stack_is_empty(stack_t stack)
Check if a stack is empty.
Definition: stack.h:133
void * list_pop(list_t list)
Remove the first object on a list.
Definition: list.c:215
static void stack_push(stack_t stack, void *element)
Adds an element to the top of the stack.
Definition: stack.h:97