quick_allocator.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
  3. // Copyright 2003 David Abrahams
  4. // Copyright 2003, 2025 Peter Dimov
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // https://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/config/header_deprecated.hpp>
  8. #include <memory>
  9. #include <cstddef>
  10. BOOST_HEADER_DEPRECATED("std::allocator or std::pmr::synchronized_pool_resource")
  11. namespace boost
  12. {
  13. namespace detail
  14. {
  15. template<class T> struct quick_allocator
  16. {
  17. static void* alloc()
  18. {
  19. return std::allocator<T>().allocate( 1 );
  20. }
  21. static void* alloc( std::size_t n )
  22. {
  23. if( n != sizeof(T) ) // class-specific delete called for a derived object
  24. {
  25. return ::operator new( n );
  26. }
  27. else
  28. {
  29. return alloc();
  30. }
  31. }
  32. static void dealloc( void* p )
  33. {
  34. if( p != 0 ) // 18.4.1.1/13
  35. {
  36. std::allocator<T>().deallocate( static_cast<T*>( p ), 1 );
  37. }
  38. }
  39. static void dealloc( void* p, std::size_t n )
  40. {
  41. if( n != sizeof(T) ) // class-specific delete called for a derived object
  42. {
  43. ::operator delete( p );
  44. }
  45. else
  46. {
  47. dealloc( p );
  48. }
  49. }
  50. };
  51. } // namespace detail
  52. } // namespace boost
  53. #endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED