Sridhar Parasuram | bf39132 | 2015-01-23 09:29:07 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 2 | |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions are |
| 5 | met: |
| 6 | * Redistributions of source code must retain the above copyright |
| 7 | notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above |
| 9 | copyright notice, this list of conditions and the following |
| 10 | disclaimer in the documentation and/or other materials provided |
| 11 | with the distribution. |
| 12 | * Neither the name of The Linux Foundation nor the names of its |
| 13 | contributors may be used to endorse or promote products derived |
| 14 | from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /*=========================================================================== |
| 30 | |
| 31 | S H A R E D M E M O R Y |
| 32 | |
| 33 | ===========================================================================*/ |
| 34 | |
| 35 | |
| 36 | |
| 37 | /*=========================================================================== |
| 38 | |
| 39 | INCLUDE FILES |
| 40 | |
| 41 | ===========================================================================*/ |
| 42 | #include "smem_list.h" |
| 43 | #include <sys/types.h> |
| 44 | |
| 45 | /*============================================================================= |
| 46 | |
| 47 | GLOBAL DEFINITIONS AND DECLARATIONS FOR MODULE |
| 48 | |
| 49 | =============================================================================*/ |
| 50 | |
| 51 | |
| 52 | /*=========================================================================== |
| 53 | |
| 54 | PRIVATE FUNCTION DECLARATIONS |
| 55 | |
| 56 | ===========================================================================*/ |
| 57 | |
| 58 | /*=========================================================================== |
| 59 | FUNCTION smem_list_init |
| 60 | ===========================================================================*/ |
| 61 | /** |
| 62 | Creates a generic list. |
| 63 | |
| 64 | @param[in/out] list Pointer to the user list. |
| 65 | |
| 66 | @return |
| 67 | N/A. |
| 68 | |
| 69 | @dependencies |
| 70 | List must be protected by critical sections for multi-threaded use. |
| 71 | |
| 72 | @sideeffects |
| 73 | None. |
| 74 | */ |
| 75 | /*=========================================================================*/ |
| 76 | void smem_list_init( smem_list_type *list ) |
| 77 | { |
| 78 | list->next = NULL; |
| 79 | list->count = 0; |
| 80 | } |
| 81 | |
| 82 | /*=========================================================================== |
| 83 | FUNCTION smem_list_append |
| 84 | ===========================================================================*/ |
| 85 | /** |
| 86 | Appends an element to the end of a generic list. |
| 87 | |
| 88 | The element must contain a smem_list_link_type at its base address. |
| 89 | The element should not be already present in any list. |
| 90 | |
| 91 | @param[in/out] list Pointer to the user list. |
| 92 | @param[in] item Element to be inserted. |
| 93 | |
| 94 | @return |
| 95 | N/A. |
| 96 | |
| 97 | @dependencies |
| 98 | The element must contain a smem_list_link_type at its base address. |
| 99 | List must be protected by critical sections for multi-threaded use. |
| 100 | |
| 101 | @sideeffects |
| 102 | None. |
| 103 | */ |
| 104 | /*=========================================================================*/ |
| 105 | void smem_list_append( smem_list_type *list, void *item ) |
| 106 | { |
| 107 | smem_list_link_type *link = ( smem_list_link_type * )list; |
| 108 | |
| 109 | /* get next item until link->next points to null */ |
| 110 | while( link->next ) |
| 111 | { |
| 112 | link = link->next; |
| 113 | } |
| 114 | |
| 115 | /* link->next points to null here, but we can point it to the new item */ |
| 116 | link->next = item; |
| 117 | |
| 118 | /* point link to the head of the new item */ |
| 119 | link = item; |
| 120 | |
| 121 | /* terminate the list */ |
| 122 | link->next = NULL; |
| 123 | |
| 124 | list->count++; |
| 125 | } |
| 126 | |
| 127 | /*=========================================================================== |
| 128 | FUNCTION smem_list_delete |
| 129 | ===========================================================================*/ |
| 130 | /** |
| 131 | Removes the specified element from a generic list. |
| 132 | |
| 133 | The element may be present at any point in the list. |
| 134 | |
| 135 | @param[in] list Pointer to the user list. |
| 136 | @param[in] item Element to be removed. |
| 137 | |
| 138 | @return |
| 139 | 0 if the element is deleted, or a negative error code if the element |
| 140 | is not found. |
| 141 | |
| 142 | @dependencies |
| 143 | List must be protected by critical sections for multi-threaded use. |
| 144 | |
| 145 | @sideeffects |
| 146 | None. |
| 147 | */ |
| 148 | /*=========================================================================*/ |
| 149 | long smem_list_delete( smem_list_type *list, void *item ) |
| 150 | { |
| 151 | smem_list_link_type *link = ( smem_list_link_type * )list; |
| 152 | |
| 153 | /* get next item until link points to item in question */ |
| 154 | while( link->next != item ) |
| 155 | { |
| 156 | if( NULL == link->next ) |
| 157 | { |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | link = link->next; |
| 162 | } |
| 163 | |
| 164 | link->next = ( ( smem_list_link_type * )item )->next; |
| 165 | |
| 166 | list->count--; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /*=========================================================================== |
| 172 | FUNCTION smem_list_first |
| 173 | ===========================================================================*/ |
| 174 | /** |
| 175 | Retrieves the first element in the list without removing it. |
| 176 | |
| 177 | @param[in] list Pointer to the user list. |
| 178 | |
| 179 | @return |
| 180 | Pointer to the first element in the list. |
| 181 | |
| 182 | @dependencies |
| 183 | List must be protected by critical sections for multi-threaded use. |
| 184 | |
| 185 | @sideeffects |
| 186 | None. |
| 187 | */ |
| 188 | /*=========================================================================*/ |
| 189 | void *smem_list_first( smem_list_type *list ) |
| 190 | { |
| 191 | /* return the first item in the list */ |
| 192 | return list->next; |
| 193 | } |
| 194 | |
| 195 | /*=========================================================================== |
| 196 | FUNCTION smem_list_next |
| 197 | ===========================================================================*/ |
| 198 | /** |
| 199 | Retrieves the next element in the list. |
| 200 | |
| 201 | @param[in] list Pointer to the user list. |
| 202 | |
| 203 | @return |
| 204 | Pointer to the next element in the list. |
| 205 | |
| 206 | @dependencies |
| 207 | List must be protected by critical sections for multi-threaded use. |
| 208 | |
| 209 | @sideeffects |
| 210 | None. |
| 211 | */ |
| 212 | /*=========================================================================*/ |
| 213 | void *smem_list_next( void * item ) |
| 214 | { |
| 215 | /* just return the item that this item points to |
| 216 | * (base address contains a pointer) */ |
| 217 | return ( ( smem_list_link_type * )item )->next; |
| 218 | } |
| 219 | |
| 220 | /*=========================================================================== |
| 221 | FUNCTION smem_list_count |
| 222 | ===========================================================================*/ |
| 223 | /** |
| 224 | Gets the number of elements in the list. |
| 225 | |
| 226 | @param[in] list Pointer to the user list. |
| 227 | |
| 228 | @return |
| 229 | Count of elements in the list. |
| 230 | |
| 231 | @dependencies |
| 232 | List must be protected by critical sections for multi-threaded use. |
| 233 | |
| 234 | @sideeffects |
| 235 | None. |
| 236 | */ |
| 237 | /*=========================================================================*/ |
| 238 | long smem_list_count( smem_list_type *list ) |
| 239 | { |
| 240 | return list->count; |
| 241 | } |
| 242 | |