Index: libc/src/__support/CPP/blockstore.h =================================================================== --- libc/src/__support/CPP/blockstore.h +++ libc/src/__support/CPP/blockstore.h @@ -113,6 +113,29 @@ *ptr = value; } + T &back() { + static_assert(REVERSE_ORDER); + return *begin(); + } + + void pop_back() { + static_assert(REVERSE_ORDER); + // TOOD should be calling "back().~T()" here but we don't run destructors + // in destrory either. + fill_count--; + if (fill_count || current == &first) + return; + Block *old = current; + current = current->next; + if (old != &first) + ::free(old); + fill_count = BLOCK_SIZE; + } + + bool empty() const { + return current == &first && !fill_count; + } + iterator begin() { if (REVERSE_ORDER) return iterator(current, fill_count); Index: libc/test/src/__support/CPP/blockstore_test.cpp =================================================================== --- libc/test/src/__support/CPP/blockstore_test.cpp +++ libc/test/src/__support/CPP/blockstore_test.cpp @@ -66,3 +66,24 @@ TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse10) { populate_and_iterate<4, 10, true>(); } + +TEST_F(LlvmLibcBlockStoreTest, Back) { + using __llvm_libc::cpp::BlockStore; + BlockStore block_store; + for (int i = 0; i < 20; i++) + block_store.push_back(i); + for (int i = 19; i >= 0; i--, block_store.pop_back()) + ASSERT_EQ(block_store.back(), i); + block_store.destroy(&block_store); +} + +TEST_F(LlvmLibcBlockStoreTest, Empty) { + using __llvm_libc::cpp::BlockStore; + BlockStore block_store; + + ASSERT_TRUE(block_store.empty()); + block_store.push_back(1); + for (int i = 0; i < 10; i++, block_store.push_back(1)) + ASSERT_FALSE(block_store.empty()); + block_store.destroy(&block_store); +}