1 /****************************************************************************
2  *
3  *  Copyright (c) 2023, Michael Becker (michael.f.becker@gmail.com)
4  *
5  *  This file is part of the FreeRTOS Add-ons project.
6  *
7  *  Source Code:
8  *  https://github.com/michaelbecker/freertos-addons
9  *
10  *  Project Page:
11  *  http://michaelbecker.github.io/freertos-addons/
12  *
13  *  On-line Documentation:
14  *  http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  *  MIT License
17  *
18  *  Permission is hereby granted, free of charge, to any person obtaining a
19  *  copy of this software and associated documentation files
20  *  (the "Software"), to deal in the Software without restriction, including
21  *  without limitation the rights to use, copy, modify, merge, publish,
22  *  distribute, sublicense, and/or sell copies of the Software, and to
23  *  permit persons to whom the Software is furnished to do so,subject to the
24  *  following conditions:
25  *
26  *  + The above copyright notice and this permission notice shall be included
27  *    in all copies or substantial portions of the Software.
28  *  + Credit is appreciated, but not required, if you find this project
29  *    useful enough to include in your application, product, device, etc.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35  *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
36  *  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
37  *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  *
39  ***************************************************************************/
40 /*
41  *  Copyright 2023-2024 NXP
42  *
43  *  SPDX-License-Identifier: BSD-3-Clause
44  *
45  */
46 
47 #ifndef STACK_H_
48 #define STACK_H_
49 
50 #include "slist.h"
51 
52 /**
53  *  The stack structure, we leverage low overhead singly linked lists here.
54  */
55 typedef struct Stack_t_
56 {
57     /**
58      *  How many items are in the stack.
59      */
60     int Count;
61 
62     /**
63      *  The head of the stack.
64      */
65     SlNode_t Head;
66 
67 } Stack_t;
68 
69 /**
70  *  Initialize a Stack structure you provide.
71  *
72  *  @param Stack Pointer to your stack structure.
73  */
74 void InitStack(Stack_t *Stack);
75 
76 /**
77  *  Push an item onto the stack.
78  *
79  *  Note that you have to have embedded an SListNode inside your data
80  *  structure.
81  *
82  *  @param Stack Pointer to your stack structure.
83  *  @param Node The SListNode you want on the stack.
84  */
85 void PushOnStack(Stack_t *Stack, SlNode_t *Node);
86 
87 /**
88  *  Pop an item off the stack.
89  *
90  *  Note that you have to have embedded an SListNode inside your data
91  *  structure.
92  *
93  *  @param Stack Pointer to your stack structure.
94  *  @return An SListNode from the stack, or NULL if it's empty.
95  */
96 SlNode_t *PopOffStack(Stack_t *Stack);
97 
98 /**
99  *  @return True if the stack is empty, false otherwise.
100  */
101 #define IsStackEmpty(_stack) ((_stack)->Count == 0)
102 
103 #endif
104