| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Clist.hpp | ||
| 3 | // \brief CFDP circular list header file | ||
| 4 | // | ||
| 5 | // This file is a port of CFDP circular list from the following files | ||
| 6 | // from the NASA Core Flight System (cFS) CFDP (CF) Application, version 3.0.0, | ||
| 7 | // adapted for use within the F-Prime (F') framework: | ||
| 8 | // - cf_clist.h (CFDP circular list data structure definitions) | ||
| 9 | // | ||
| 10 | // ====================================================================== | ||
| 11 | // | ||
| 12 | // NASA Docket No. GSC-18,447-1 | ||
| 13 | // | ||
| 14 | // Copyright (c) 2019 United States Government as represented by the | ||
| 15 | // Administrator of the National Aeronautics and Space Administration. | ||
| 16 | // All Rights Reserved. | ||
| 17 | // | ||
| 18 | // Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| 19 | // not use this file except in compliance with the License. You may obtain | ||
| 20 | // a copy of the License at | ||
| 21 | // | ||
| 22 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
| 23 | // | ||
| 24 | // Unless required by applicable law or agreed to in writing, software | ||
| 25 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
| 26 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 27 | // See the License for the specific language governing permissions and | ||
| 28 | // limitations under the License. | ||
| 29 | // | ||
| 30 | // ====================================================================== | ||
| 31 | |||
| 32 | #ifndef CFDP_CLIST_HPP | ||
| 33 | #define CFDP_CLIST_HPP | ||
| 34 | |||
| 35 | #include <Fw/Types/BasicTypes.hpp> | ||
| 36 | #include <cstddef> | ||
| 37 | |||
| 38 | namespace Svc { | ||
| 39 | namespace Ccsds { | ||
| 40 | namespace Cfdp { | ||
| 41 | |||
| 42 | /** | ||
| 43 | * @brief Traverse status for circular list operations | ||
| 44 | */ | ||
| 45 | enum CListTraverseStatus : U8 { | ||
| 46 | CLIST_TRAVERSE_CONTINUE = 0, /**< \brief Continue traversing the list */ | ||
| 47 | CLIST_TRAVERSE_EXIT = 1 /**< \brief Stop traversing the list */ | ||
| 48 | }; | ||
| 49 | |||
| 50 | /** \brief Constant indicating to continue traversal */ | ||
| 51 | constexpr U8 CFDP_CLIST_CONT = CLIST_TRAVERSE_CONTINUE; | ||
| 52 | |||
| 53 | /** \brief Constant indicating to stop traversal */ | ||
| 54 | constexpr U8 CFDP_CLIST_EXIT = CLIST_TRAVERSE_EXIT; | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Checks if the list traversal should continue | ||
| 58 | */ | ||
| 59 | ✗ | static inline bool CfdpCListTraverseStatusIsContinue(CListTraverseStatus stat) { | |
| 60 | ✗ | return (stat == CLIST_TRAVERSE_CONTINUE); | |
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * @brief Circular linked list node structure | ||
| 65 | */ | ||
| 66 | struct CListNode { | ||
| 67 | struct CListNode* next; /**< \brief Pointer to next node */ | ||
| 68 | struct CListNode* prev; /**< \brief Pointer to previous node */ | ||
| 69 | }; | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @brief Obtains a pointer to the parent structure | ||
| 73 | * | ||
| 74 | * Given a pointer to a CListNode object which is known to be a member of a | ||
| 75 | * larger container, this converts the pointer to that of the parent. | ||
| 76 | * This is the C++ equivalent of the Linux kernel's container_of macro. | ||
| 77 | */ | ||
| 78 | template <typename Container, typename Member> | ||
| 79 | ✗ | constexpr Container* container_of_cpp(Member* member_ptr, Member Container::* member) { | |
| 80 | // reinterpret_cast: Required for intrusive list node-to-parent pointer arithmetic (container_of idiom) | ||
| 81 | ✗ | return reinterpret_cast<Container*>(reinterpret_cast<U8*>(member_ptr) - | |
| 82 | ✗ | reinterpret_cast<std::ptrdiff_t>(&(reinterpret_cast<Container*>(0)->*member))); | |
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * @brief Callback function type for use with CfdpCListTraverse() | ||
| 87 | * | ||
| 88 | * @param node Current node being traversed | ||
| 89 | * @param context Opaque pointer passed through from initial call | ||
| 90 | * | ||
| 91 | * @returns integer status code indicating whether to continue traversal | ||
| 92 | * @retval #CFDP_CLIST_CONT Indicates to continue traversing the list | ||
| 93 | * @retval #CFDP_CLIST_EXIT Indicates to stop traversing the list | ||
| 94 | */ | ||
| 95 | using CListFunc = CListTraverseStatus (*)(CListNode*, void*); | ||
| 96 | |||
| 97 | /** | ||
| 98 | * @brief Callback type for list traversal | ||
| 99 | * | ||
| 100 | * Function pointer callback for list traversal operations. | ||
| 101 | * The callback receives the node and an opaque context pointer. | ||
| 102 | */ | ||
| 103 | using CListTraverseCallback = CListTraverseStatus (*)(CListNode*, void*); | ||
| 104 | |||
| 105 | /************************************************************************/ | ||
| 106 | /** @brief Initialize a clist node. | ||
| 107 | * | ||
| 108 | * @param node Pointer to node structure to be initialized | ||
| 109 | */ | ||
| 110 | void CfdpCListInitNode(CListNode* node); | ||
| 111 | |||
| 112 | /************************************************************************/ | ||
| 113 | /** @brief Insert the given node into the front of a list. | ||
| 114 | * | ||
| 115 | * @param head Pointer to head of list to insert into | ||
| 116 | * @param node Pointer to node to insert | ||
| 117 | */ | ||
| 118 | void CfdpCListInsertFront(CListNode** head, CListNode* node); | ||
| 119 | |||
| 120 | /************************************************************************/ | ||
| 121 | /** @brief Insert the given node into the back of a list. | ||
| 122 | * | ||
| 123 | * @param head Pointer to head of list to insert into | ||
| 124 | * @param node Pointer to node to insert | ||
| 125 | */ | ||
| 126 | void CfdpCListInsertBack(CListNode** head, CListNode* node); | ||
| 127 | |||
| 128 | /************************************************************************/ | ||
| 129 | /** @brief Remove the given node from the list. | ||
| 130 | * | ||
| 131 | * @param head Pointer to head of list to remove from | ||
| 132 | * @param node Pointer to node to remove | ||
| 133 | */ | ||
| 134 | void CfdpCListRemove(CListNode** head, CListNode* node); | ||
| 135 | |||
| 136 | /************************************************************************/ | ||
| 137 | /** @brief Remove the first node from a list and return it. | ||
| 138 | * | ||
| 139 | * @param head Pointer to head of list to remove from | ||
| 140 | * | ||
| 141 | * @returns The first node (now removed) in the list | ||
| 142 | * @retval nullptr if list was empty. | ||
| 143 | */ | ||
| 144 | CListNode* CfdpCListPop(CListNode** head); | ||
| 145 | |||
| 146 | /************************************************************************/ | ||
| 147 | /** @brief Insert the given node into the last after the given start node. | ||
| 148 | * | ||
| 149 | * @param head Pointer to head of list to remove from | ||
| 150 | * @param start Pointer to node to insert | ||
| 151 | * @param after Pointer to position to insert after | ||
| 152 | */ | ||
| 153 | void CfdpCListInsertAfter(CListNode** head, CListNode* start, CListNode* after); | ||
| 154 | |||
| 155 | /************************************************************************/ | ||
| 156 | /** @brief Traverse the entire list, calling the given function on all nodes. | ||
| 157 | * | ||
| 158 | * @note on traversal it's ok to delete the current node, but do not delete | ||
| 159 | * other nodes in the same list!! | ||
| 160 | * | ||
| 161 | * @param start List to traverse (first node) | ||
| 162 | * @param fn Callback function to invoke for each node | ||
| 163 | * @param context Opaque pointer to pass to callback | ||
| 164 | */ | ||
| 165 | void CfdpCListTraverse(CListNode* start, CListFunc fn, void* context); | ||
| 166 | |||
| 167 | /************************************************************************/ | ||
| 168 | /** @brief Reverse list traversal, starting from end, calling given function on all nodes. | ||
| 169 | * | ||
| 170 | * @note traverse_R will work backwards from the parameter's prev, and end on param | ||
| 171 | * | ||
| 172 | * @param end List to traverse (last node) | ||
| 173 | * @param fn Callback function to invoke for each node | ||
| 174 | * @param context Opaque pointer to pass to callback | ||
| 175 | */ | ||
| 176 | void CfdpCListTraverseR(CListNode* end, CListFunc fn, void* context); | ||
| 177 | |||
| 178 | } // namespace Cfdp | ||
| 179 | } // namespace Ccsds | ||
| 180 | } // namespace Svc | ||
| 181 | |||
| 182 | #endif /* !CFDP_CLIST_HPP */ | ||
| 183 |