From 259dbfa7419dfd6e523c652c84154d681e7fcb03 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 25 Aug 2017 14:27:20 +0100 Subject: [PATCH] Added type size test and error to ipool::allocate --- src/pool.h | 20 +++++++++++++++++++- test/test_pool.cpp | 10 ++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/pool.h b/src/pool.h index c6f2545b..f51f067d 100644 --- a/src/pool.h +++ b/src/pool.h @@ -90,7 +90,20 @@ namespace etl public: pool_object_not_in_pool(string_type file_name, numeric_type line_number) - : pool_exception(ETL_ERROR_TEXT("pool:notinpool", ETL_FILE"B"), file_name, line_number) + : pool_exception(ETL_ERROR_TEXT("pool:not in pool", ETL_FILE"B"), file_name, line_number) + {} + }; + + //*************************************************************************** + /// The exception thrown when an the type requested is larger than the element size. + ///\ingroup pool + //*************************************************************************** + class pool_element_size : public pool_exception + { + public: + + pool_element_size(string_type file_name, numeric_type line_number) + : pool_exception(ETL_ERROR_TEXT("pool:element size", ETL_FILE"C"), file_name, line_number) {} }; @@ -112,6 +125,11 @@ namespace etl template T* allocate() { + if (sizeof(T) > ITEM_SIZE) + { + ETL_ASSERT(false, ETL_ERROR(etl::pool_element_size)); + } + return reinterpret_cast(allocate_item()); } diff --git a/test/test_pool.cpp b/test/test_pool.cpp index 5aabf967..cc3748e6 100644 --- a/test/test_pool.cpp +++ b/test/test_pool.cpp @@ -264,6 +264,16 @@ namespace CHECK_EQUAL(4U, pool.available()); } + + //************************************************************************* + TEST(test_type_error) + { + etl::pool pool; + + etl::ipool& ip = pool; + + CHECK_THROW(ip.allocate(), etl::pool_element_size); + } }; }