From d1dac603c8fc4e31a0505526c3650819b071ab19 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 3 Mar 2013 14:23:00 +0000 Subject: [PATCH] Add RtList appendAt and insertAt methods --- source/libs/rtmempool/list.h | 2 +- source/tests/RtList.cpp | 20 +++++++++++++++++--- source/utils/RtList.hpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/source/libs/rtmempool/list.h b/source/libs/rtmempool/list.h index 48cb45a83..9f9ffbf09 100644 --- a/source/libs/rtmempool/list.h +++ b/source/libs/rtmempool/list.h @@ -279,7 +279,7 @@ static inline void list_splice_init(struct list_head *list, struct list_head *he } /** - * list_splice_init - join two lists and reinitialise the emptied list. + * list_splice_tail_init - join two lists and reinitialise the emptied list. * @list: the new list to add. * @head: the place to add it in the first list. * diff --git a/source/tests/RtList.cpp b/source/tests/RtList.cpp index cc56d32ab..e3ab65244 100644 --- a/source/tests/RtList.cpp +++ b/source/tests/RtList.cpp @@ -83,7 +83,7 @@ struct PostRtEvents { } postRtEvents; -void run4Tests() +void run5Tests() { unsigned short k = 0; MyData allMyData[MAX_RT_EVENTS]; @@ -101,7 +101,7 @@ void run4Tests() postRtEvents.mutex.unlock(); printf("Post-Rt Event Count: %i\n", k); - assert(k == 4); + assert(k == 5); // data should be empty now assert(postRtEvents.data.count() == 0); @@ -124,6 +124,7 @@ int main() MyData m2(2); MyData m3(3); MyData m4(4); + MyData m5(5); // start assert(postRtEvents.data.count() == 0); @@ -141,6 +142,8 @@ int main() postRtEvents.appendRT(m2); postRtEvents.appendRT(m4); postRtEvents.appendRT(m3); + assert(postRtEvents.data.count() == 1); + assert(postRtEvents.dataPendingRT.count() == 3); postRtEvents.trySplice(); assert(postRtEvents.data.count() == 4); assert(postRtEvents.dataPendingRT.count() == 0); @@ -150,9 +153,20 @@ int main() MyData& my = *it; printf("FOR DATA!!!: %i %s\n", my.idStr, (const char*)my.str); + + if (my.idStr == 1) + { + // +1 append at + postRtEvents.dataPendingRT.insertAt(m5, it); + assert(postRtEvents.data.count() == 4); + assert(postRtEvents.dataPendingRT.count() == 1); + postRtEvents.trySplice(); + assert(postRtEvents.data.count() == 5); + assert(postRtEvents.dataPendingRT.count() == 0); + } } - run4Tests(); + run5Tests(); // reset postRtEvents.clear(); diff --git a/source/utils/RtList.hpp b/source/utils/RtList.hpp index e81171912..d303d4e9e 100644 --- a/source/utils/RtList.hpp +++ b/source/utils/RtList.hpp @@ -80,6 +80,8 @@ protected: k_list_head* const kQueue; k_list_head* fEntry; Data* fData; + + friend class List; }; List() @@ -139,6 +141,19 @@ public: return false; } + bool appendAt(const T& value, const Itenerator& it) + { + if (Data* const data = _allocate()) + { + std::memcpy(&data->value, &value, sizeof(T)); + list_add_tail(&data->siblings, it.fEntry->next); + fCount++; + return true; + } + + return false; + } + bool insert(const T& value) { if (Data* const data = _allocate()) @@ -152,6 +167,19 @@ public: return false; } + bool insertAt(const T& value, const Itenerator& it) + { + if (Data* const data = _allocate()) + { + std::memcpy(&data->value, &value, sizeof(T)); + list_add(&data->siblings, it.fEntry->prev); + fCount++; + return true; + } + + return false; + } + T& getAt(const size_t index, const bool remove = false) { if (fCount == 0 || index >= fCount)