diff --git a/nx/include/switch/runtime/util/list.h b/nx/include/switch/runtime/util/list.h index eb08a0ee..7e11904f 100644 --- a/nx/include/switch/runtime/util/list.h +++ b/nx/include/switch/runtime/util/list.h @@ -36,7 +36,7 @@ void listFree(List* l); /** * @brief Inserts something in a position - * @param l List object. + * @param l List object * @param item A pointer to the thing you want to insert * @param pos The position to insert (0 is the first position) */ @@ -44,7 +44,7 @@ void listInsert(List* l, void* item, u32 pos); /** * @brief Inserts something in the first position - * @param l List object. + * @param l List object * @param item A pointer to the thing you want to insert */ static inline void listInsertFirst(List* l, void* item) { @@ -53,21 +53,21 @@ static inline void listInsertFirst(List* l, void* item) { /** * @brief Inserts something at the end of the list - * @param l List object. + * @param l List object * @param item A pointer to the thing you want to insert */ void listInsertLast(List* l, void* item); /** * @brief Deletes the node of the list which has the item specified (makes a pointer comparison to ckeck that) - * @param l List object. + * @param l List object * @param item A pointer to the thing you want to delete */ void listDelete(List* l, void* item); /** * @brief Checks if the item is inserted in the list (makes a pointer comparison to ckeck that) - * @param l List object. + * @param l List object * @param item A pointer to the thing you want to check * @return true if the item is in the list, false otherwise */ @@ -75,15 +75,61 @@ bool listIsInserted(List* l, void* item); /** * @brief Returns the number of items inserted in the list - * @param l List object. + * @param l List object * @return The number of nodes (the number of inserted things) in the list */ u32 listGetNumNodes(List* l); /** * @brief Returns the item inserted in an specified position - * @param l List object. + * @param l List object * @param pos The position of the item * @return A pointer to that item, NULL if it isn't found */ -void* listGetItem(List* l, u32 pos); \ No newline at end of file +void* listGetItem(List* l, u32 pos); + +/** + * @brief Inserts the item in the first position + * @param l List object + * @param item A pointer to something to store + */ +static inline void listPushFront(List* l, void* item) { + listInsert(l, item, 0); +} + +/** + * @brief Returns the item inserted in the first position and deletes it + * @param l List object + * @return A pointer to that item, NULL if the list is empty + */ +void* listPopFront(List* l); + +/** + * @brief Returns the item inserted in the first position + * @param l List object. + * @return A pointer to that item, NULL if the list is empty + */ +void* listPeekFront(List* l); + +/** + * @brief Inserts the item in the last position + * @param l List object. + * @param item A pointer to something you want to insert + */ +static inline void listPushBack(List* l, void* item) { + listInsertLast(List* l, void* item); +} + +/** + * @brief Returns the item inserted in the last position and deletes it + * @param l List object. + * @return A pointer to that item, NULL if the list is empty + */ +void* listPopBack(List* l); + +/** + * @brief Returns the item inserted in the last position + * @param l List object. + * @return A pointer to that item, NULL if the list is empty + */ +void* listPeekBack(List* l); \ No newline at end of file diff --git a/nx/source/runtime/util/list.c b/nx/source/runtime/util/list.c index 77b87320..49b1f25f 100644 --- a/nx/source/runtime/util/list.c +++ b/nx/source/runtime/util/list.c @@ -81,6 +81,22 @@ void listDelete(List* l, void* item) { rwlockWriteUnlock(&l->mutex); } +void listDeleteAtPos(List* l, u32 pos) { + rwlockReadLock(&l->mutex); + if(pos >= l->num_nodes || pos < 0) { + return; + } + Node* aux = l->header; + for(int i = pos; i > 0; i++) { + aux = aux->next; + } + Node* delete = aux->next; + aux->next = delete->next; + free(delete); + l->num_nodes--; + rwlockReadUnlock(&l->mutex); +} + bool listIsInserted(List* l, void* item) { rwlockReadLock(&l->mutex); Node* aux = l->header; @@ -111,4 +127,46 @@ void* listGetItem(List* l, u32 pos) { void* result = aux->item; rwlockReadUnlock(&l->mutex); return result; +} + +void* listPopFront(List* l) { + rwlockReadLock(&l->mutex); + if(l->num_nodes == 0) { + return NULL; + } + void* result = listGetItem(l, 0); + listDeleteAtPos(l, 0); + rwlockReadUnlock(&l->mutex); + return result; +} + +void* listPeekFront(List* l) { + rwlockReadLock(&l->mutex); + if(l->num_nodes == 0) { + return NULL; + } + void* result = listGetItem(l, 0); + rwlockReadUnlock(&l->mutex); + return result; +} + +void* listPopBack(List* l) { + rwlockReadLock(&l->mutex); + if(l->num_nodes == 0) { + return NULL; + } + void* result = listGetItem(l, l->num_nodes-1); + listDeleteAtPos(l, l->num_nodes-1); + rwlockReadUnlock(&l->mutex); + return result; +} + +void* listPeekBack(List* l) { + rwlockReadLock(&l->mutex); + if(l->num_nodes == 0) { + return NULL; + } + void* result = listGetItem(l, l->num_nodes-1); + rwlockReadUnlock(&l->mutex); + return result; } \ No newline at end of file