allocator.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // allocator.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ALLOCATOR_HPP
  11. #define ALLOCATOR_HPP
  12. #include <boost/aligned_storage.hpp>
  13. // Represents a single connection from a client.
  14. class allocator
  15. {
  16. public:
  17. allocator()
  18. : in_use_(false)
  19. {
  20. }
  21. void* allocate(std::size_t n)
  22. {
  23. if (in_use_ || n >= 1024)
  24. return ::operator new(n);
  25. in_use_ = true;
  26. return static_cast<void*>(&space_);
  27. }
  28. void deallocate(void* p)
  29. {
  30. if (p != static_cast<void*>(&space_))
  31. ::operator delete(p);
  32. else
  33. in_use_ = false;
  34. }
  35. private:
  36. allocator(const allocator&);
  37. allocator& operator=(const allocator&);
  38. // Whether the reusable memory space is currently in use.
  39. bool in_use_;
  40. // The reusable memory space made available by the allocator.
  41. boost::aligned_storage<1024>::type space_;
  42. };
  43. #endif // ALLOCATOR_HPP