| 1 | /* | 
|---|
| 2 | Simple DirectMedia Layer | 
|---|
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | 
|---|
| 4 |  | 
|---|
| 5 | This software is provided 'as-is', without any express or implied | 
|---|
| 6 | warranty.  In no event will the authors be held liable for any damages | 
|---|
| 7 | arising from the use of this software. | 
|---|
| 8 |  | 
|---|
| 9 | Permission is granted to anyone to use this software for any purpose, | 
|---|
| 10 | including commercial applications, and to alter it and redistribute it | 
|---|
| 11 | freely, subject to the following restrictions: | 
|---|
| 12 |  | 
|---|
| 13 | 1. The origin of this software must not be misrepresented; you must not | 
|---|
| 14 | claim that you wrote the original software. If you use this software | 
|---|
| 15 | in a product, an acknowledgment in the product documentation would be | 
|---|
| 16 | appreciated but is not required. | 
|---|
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | 
|---|
| 18 | misrepresented as being the original software. | 
|---|
| 19 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 20 | */ | 
|---|
| 21 | #include "SDL_internal.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "./SDL_list.h" | 
|---|
| 24 |  | 
|---|
| 25 | // Push | 
|---|
| 26 | bool SDL_ListAdd(SDL_ListNode **head, void *ent) | 
|---|
| 27 | { | 
|---|
| 28 | SDL_ListNode *node = (SDL_ListNode *)SDL_malloc(sizeof(*node)); | 
|---|
| 29 |  | 
|---|
| 30 | if (!node) { | 
|---|
| 31 | return false; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | node->entry = ent; | 
|---|
| 35 | node->next = *head; | 
|---|
| 36 | *head = node; | 
|---|
| 37 | return true; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | // Pop from end as a FIFO (if add with SDL_ListAdd) | 
|---|
| 41 | void SDL_ListPop(SDL_ListNode **head, void **ent) | 
|---|
| 42 | { | 
|---|
| 43 | SDL_ListNode **ptr = head; | 
|---|
| 44 |  | 
|---|
| 45 | // Invalid or empty | 
|---|
| 46 | if (!head || !*head) { | 
|---|
| 47 | return; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | while ((*ptr)->next) { | 
|---|
| 51 | ptr = &(*ptr)->next; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | if (ent) { | 
|---|
| 55 | *ent = (*ptr)->entry; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | SDL_free(*ptr); | 
|---|
| 59 | *ptr = NULL; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void SDL_ListRemove(SDL_ListNode **head, void *ent) | 
|---|
| 63 | { | 
|---|
| 64 | SDL_ListNode **ptr = head; | 
|---|
| 65 |  | 
|---|
| 66 | while (*ptr) { | 
|---|
| 67 | if ((*ptr)->entry == ent) { | 
|---|
| 68 | SDL_ListNode *tmp = *ptr; | 
|---|
| 69 | *ptr = (*ptr)->next; | 
|---|
| 70 | SDL_free(tmp); | 
|---|
| 71 | return; | 
|---|
| 72 | } | 
|---|
| 73 | ptr = &(*ptr)->next; | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void SDL_ListClear(SDL_ListNode **head) | 
|---|
| 78 | { | 
|---|
| 79 | SDL_ListNode *l = *head; | 
|---|
| 80 | *head = NULL; | 
|---|
| 81 | while (l) { | 
|---|
| 82 | SDL_ListNode *tmp = l; | 
|---|
| 83 | l = l->next; | 
|---|
| 84 | SDL_free(tmp); | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|