diff --git a/test/Jamfile b/test/Jamfile index 2132372..428f0ca 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -429,3 +429,6 @@ run sp_type_with_alignment_test.cpp ; run sp_ostream_test.cpp ; run ip_ostream_test.cpp ; run lsp_ostream_test.cpp ; + +run shared_ptr_alloc_test.cpp ; +run quick_allocator_test.cpp ; diff --git a/test/quick_allocator_test.cpp b/test/quick_allocator_test.cpp new file mode 100644 index 0000000..4050d09 --- /dev/null +++ b/test/quick_allocator_test.cpp @@ -0,0 +1,83 @@ +// Copyright 2025 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +// The interface of quick_allocator has never been documented, +// but in can be inferred from the source code to be +// +// template struct quick_allocator +// { +// // allocate memory for an object of type T +// static void* alloc(); +// +// // deallocate the memory returned from alloc() +// static void dealloc( void* p ); +// +// // if n == sizeof(T), returns alloc() +// // otherwise, allocates a memory block of size n +// static void* alloc( std::size_t n ); +// +// // deallocate the memory returned from alloc( n ) +// static void dealloc( void* p, std::size_t n ); +// }; + +struct X +{ + int data; +}; + +struct Y: public X +{ + int data2; +}; + +int main() +{ + using boost::detail::quick_allocator; + + void* p = quick_allocator::alloc(); + std::memset( p, 0xAA, sizeof(Y) ); + + { + void* p1 = quick_allocator::alloc(); + std::memset( p1, 0xCC, sizeof(X) ); + + void* p2 = quick_allocator::alloc(); + std::memset( p2, 0xDD, sizeof(X) ); + + BOOST_TEST_NE( p1, p2 ); + BOOST_TEST_EQ( *static_cast( p1 ), 0xCC ); + BOOST_TEST_EQ( *static_cast( p2 ), 0xDD ); + + quick_allocator::dealloc( p1 ); + BOOST_TEST_EQ( *static_cast( p2 ), 0xDD ); + + quick_allocator::dealloc( p2 ); + } + + { + void* p1 = quick_allocator::alloc( sizeof(X) ); + std::memset( p1, 0xCC, sizeof(X) ); + + void* p2 = quick_allocator::alloc( sizeof(Y) ); + std::memset( p2, 0xDD, sizeof(Y) ); + + BOOST_TEST_NE( p1, p2 ); + BOOST_TEST_EQ( *static_cast( p1 ), 0xCC ); + BOOST_TEST_EQ( *static_cast( p2 ), 0xDD ); + + quick_allocator::dealloc( p1, sizeof(X) ); + BOOST_TEST_EQ( *static_cast( p2 ), 0xDD ); + + quick_allocator::dealloc( p2, sizeof(Y) ); + } + + BOOST_TEST_EQ( *static_cast( p ), 0xAA ); + quick_allocator::dealloc( p ); + + return boost::report_errors(); +}