diff options
Diffstat (limited to 'unittests/KDAlloc/allocate.cpp')
-rw-r--r-- | unittests/KDAlloc/allocate.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/unittests/KDAlloc/allocate.cpp b/unittests/KDAlloc/allocate.cpp new file mode 100644 index 00000000..895f8215 --- /dev/null +++ b/unittests/KDAlloc/allocate.cpp @@ -0,0 +1,66 @@ +//===-- allocate.cpp ------------------------------------------------------===// +// +// The KLEE Symbolic Virtual Machine +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "klee/KDAlloc/kdalloc.h" + +#if defined(USE_GTEST_INSTEAD_OF_MAIN) +#include "gtest/gtest.h" +#endif + +#include <cassert> +#include <deque> +#include <iomanip> +#include <iostream> + +#if defined(USE_GTEST_INSTEAD_OF_MAIN) +int allocate_sample_test() { +#else +int main() { +#endif + // initialize a factory and an associated allocator (using the location "0" + // gives an OS-assigned location) + klee::kdalloc::AllocatorFactory factory(static_cast<std::size_t>(1) << 20, 0); + klee::kdalloc::Allocator allocator = factory.makeAllocator(); + + std::deque<void *> allocations; + for (std::size_t i = 0; i < 10; ++i) { + auto p = allocator.allocate(4); + allocations.emplace_back(p); + std::cout << "Allocated " << std::right << std::setw(64 / 4) << std::hex + << (static_cast<char *>(p) - + static_cast<char *>(factory.getMapping().getBaseAddress())) + << "\n"; + } + + { + auto p = allocations[2]; + allocations.erase(allocations.begin() + 2); + std::cout << "Freeing " << std::right << std::setw(64 / 4) << std::hex + << (static_cast<char *>(p) - + static_cast<char *>(factory.getMapping().getBaseAddress())) + << "\n"; + allocator.free(p, 4); + } + + for (auto p : allocations) { + std::cout << "Freeing " << std::right << std::setw(64 / 4) << std::hex + << (static_cast<char *>(p) - + static_cast<char *>(factory.getMapping().getBaseAddress())) + << "\n"; + allocator.free(p, 4); + } + + exit(0); +} + +#if defined(USE_GTEST_INSTEAD_OF_MAIN) +TEST(KDAllocDeathTest, AllocateSample) { + ASSERT_EXIT(allocate_sample_test(), ::testing::ExitedWithCode(0), ""); +} +#endif |