diff --git a/containers_cpp/include/embb/containers/internal/fixed_size_list-inl.h b/containers_cpp/include/embb/containers/internal/fixed_size_list-inl.h new file mode 100644 index 0000000..9f2b4eb --- /dev/null +++ b/containers_cpp/include/embb/containers/internal/fixed_size_list-inl.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_INL_H_ +#define EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_INL_H_ + +namespace embb { +namespace containers { +namespace internal { +template< typename ElementT > +FixedSizeList::FixedSizeList(size_t max_size) : + max_size(max_size), + size(0) { + elementsArray = static_cast( + embb::base::Allocation::Allocate(sizeof(ElementT) * + max_size)); +} + +template< typename ElementT > +inline size_t FixedSizeList::GetSize() const { + return size; +} + +template< typename ElementT > +inline size_t FixedSizeList::GetMaxSize() const { + return max_size; +} + +template< typename ElementT > +inline void FixedSizeList::clear() { + size = 0; +} + +template< typename ElementT > +typename FixedSizeList::iterator +FixedSizeList::begin() const { + return &elementsArray[0]; +} + +template< typename ElementT > +typename FixedSizeList::iterator +FixedSizeList::end() const { + return &elementsArray[size]; +} + +template< typename ElementT > +FixedSizeList< ElementT > & +FixedSizeList::operator= (const FixedSizeList & other) { + size = 0; + + if (max_size < other.size) { + EMBB_THROW(embb::base::ErrorException, "Copy target to small"); + } + + for (const_iterator it = other.begin(); it != other.end(); ++it) { + PushBack(*it); + } + return *this; +} + +template< typename ElementT > +bool FixedSizeList::PushBack(ElementT const el) { + if (size + 1 > max_size) { + return false; + } + elementsArray[size] = el; + size++; + return true; +} + +template< typename ElementT > +FixedSizeList::~FixedSizeList() { + embb::base::Allocation::Free(elementsArray); +} + +} // namespace internal +} // namespace containers +} // namespace embb + +#endif // EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_INL_H_ diff --git a/containers_cpp/include/embb/containers/internal/fixed_size_list.h b/containers_cpp/include/embb/containers/internal/fixed_size_list.h new file mode 100644 index 0000000..0f5893d --- /dev/null +++ b/containers_cpp/include/embb/containers/internal/fixed_size_list.h @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_H_ +#define EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_H_ + +namespace embb { +namespace containers { +namespace internal { +/** + * A list with fixed size, implemented as an array. Replaces std::vector that + * was used in previous hazard pointer implementation. + * + * Provides iterators, so we can apply algorithms from the STL. + * + * \tparam ElementT Type of the elements contained in the list. + */ +template< typename ElementT > +class FixedSizeList { + private: + /** + * Capacity of the list + */ + size_t max_size; + + /** + * Size of the list + */ + size_t size; + + /** + * Pointer to the array containing the list + */ + ElementT* elementsArray; + + /** + * Copy constructor not implemented. Would require dynamic memory allocation. + */ + FixedSizeList( + const FixedSizeList & + /**< [IN] Other list */); + + public: + /** + * Definition of an iterator + */ + typedef ElementT * iterator; + + /** + * Definition of a const iterator + */ + typedef const ElementT * const_iterator; + + /** + * Constructor, initializes list with given capacity + */ + FixedSizeList( + size_t max_size + /**< [IN] Capacity of the list */); + + /** + * Gets the current size of the list + * + * \return Size of the list + */ + inline size_t GetSize() const; + + /** + * Gets the capacity of the list + * + * \return The capacity of the list + */ + inline size_t GetMaxSize() const; + + /** + * Removes all elements from the list without changing the capacity + */ + inline void clear(); + + /** + * Iterator pointing to the first element + * + * \return Begin iterator + */ + iterator begin() const; + + /** + * Iterator pointing beyond the last element + * + * \return End iterator + */ + iterator end() const; + + /** + * Copies the elements of another list to this list. The capacity of + * this list has to be greater than or equal to the size of the other list. + */ + FixedSizeList & operator=( + const FixedSizeList & other + /**< [IN] Other list */); + + /** + * Appends an element to the end of the list + * + * \return \c false if the operation was not successful because the list is + * full, otherwise \c true. + */ + bool PushBack( + ElementT const el + /**< [IN] Element to append to the list */); + + /** + * Destructs the list. + */ + ~FixedSizeList(); +}; + +} // namespace internal +} // namespace containers +} // namespace embb + +#include "./fixed_size_list-inl.h" + +#endif // EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_H_ diff --git a/containers_cpp/include/embb/containers/internal/hazard_pointer-inl.h b/containers_cpp/include/embb/containers/internal/hazard_pointer-inl.h index 3873dba..8cb5cfe 100644 --- a/containers_cpp/include/embb/containers/internal/hazard_pointer-inl.h +++ b/containers_cpp/include/embb/containers/internal/hazard_pointer-inl.h @@ -30,71 +30,6 @@ namespace embb { namespace containers { namespace internal { -template< typename ElementT > -FixedSizeList::FixedSizeList(size_t max_size) : - max_size(max_size), - size(0) { - elementsArray = static_cast( - embb::base::Allocation::Allocate(sizeof(ElementT) * - max_size)); -} - -template< typename ElementT > -inline size_t FixedSizeList::GetSize() const { - return size; -} - -template< typename ElementT > -inline size_t FixedSizeList::GetMaxSize() const { - return max_size; -} - -template< typename ElementT > -inline void FixedSizeList::clear() { - size = 0; -} - -template< typename ElementT > -typename FixedSizeList::iterator -FixedSizeList::begin() const { - return &elementsArray[0]; -} - -template< typename ElementT > -typename FixedSizeList::iterator -FixedSizeList::end() const { - return &elementsArray[size]; -} - -template< typename ElementT > -FixedSizeList< ElementT > & -FixedSizeList::operator= (const FixedSizeList & other) { - size = 0; - - if (max_size < other.size) { - EMBB_THROW(embb::base::ErrorException, "Copy target to small"); - } - - for (const_iterator it = other.begin(); it != other.end(); ++it) { - PushBack(*it); - } - return *this; -} - -template< typename ElementT > -bool FixedSizeList::PushBack(ElementT const el) { - if (size + 1 > max_size) { - return false; - } - elementsArray[size] = el; - size++; - return true; -} - -template< typename ElementT > -FixedSizeList::~FixedSizeList() { - embb::base::Allocation::Free(elementsArray); -} template< typename GuardType > bool HazardPointerThreadEntry::IsActive() { diff --git a/containers_cpp/include/embb/containers/internal/hazard_pointer.h b/containers_cpp/include/embb/containers/internal/hazard_pointer.h index e1e5f49..1fc74db 100644 --- a/containers_cpp/include/embb/containers/internal/hazard_pointer.h +++ b/containers_cpp/include/embb/containers/internal/hazard_pointer.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -43,113 +44,6 @@ namespace embb { namespace containers { namespace internal { -/** - * A list with fixed size, implemented as an array. Replaces std::vector that - * was used in previous hazard pointer implementation. - * - * Provides iterators, so we can apply algorithms from the STL. - * - * \tparam ElementT Type of the elements contained in the list. - */ -template< typename ElementT > -class FixedSizeList { - private: - /** - * Capacity of the list - */ - size_t max_size; - - /** - * Size of the list - */ - size_t size; - - /** - * Pointer to the array containing the list - */ - ElementT* elementsArray; - - /** - * Copy constructor not implemented. Would require dynamic memory allocation. - */ - FixedSizeList( - const FixedSizeList & - /**< [IN] Other list */); - - public: - /** - * Definition of an iterator - */ - typedef ElementT * iterator; - - /** - * Definition of a const iterator - */ - typedef const ElementT * const_iterator; - - /** - * Constructor, initializes list with given capacity - */ - FixedSizeList( - size_t max_size - /**< [IN] Capacity of the list */); - - /** - * Gets the current size of the list - * - * \return Size of the list - */ - inline size_t GetSize() const; - - /** - * Gets the capacity of the list - * - * \return The capacity of the list - */ - inline size_t GetMaxSize() const; - - /** - * Removes all elements from the list without changing the capacity - */ - inline void clear(); - - /** - * Iterator pointing to the first element - * - * \return Begin iterator - */ - iterator begin() const; - - /** - * Iterator pointing beyond the last element - * - * \return End iterator - */ - iterator end() const; - - /** - * Copies the elements of another list to this list. The capacity of - * this list has to be greater than or equal to the size of the other list. - */ - FixedSizeList & operator=( - const FixedSizeList & other - /**< [IN] Other list */); - - /** - * Appends an element to the end of the list - * - * \return \c false if the operation was not successful because the list is - * full, otherwise \c true. - */ - bool PushBack( - ElementT const el - /**< [IN] Element to append to the list */); - - /** - * Destructs the list. - */ - ~FixedSizeList(); -}; /** * Hazard pointer entry for a single thread. Holds the actual guards that