• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // allocator.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 
11 #ifndef ALLOCATOR_HPP
12 #define ALLOCATOR_HPP
13 
14 #include <boost/aligned_storage.hpp>
15 
16 // Represents a single connection from a client.
17 class allocator
18 {
19 public:
allocator()20   allocator()
21     : in_use_(false)
22   {
23   }
24 
allocate(std::size_t n)25   void* allocate(std::size_t n)
26   {
27     if (in_use_ || n >= 1024)
28       return ::operator new(n);
29     in_use_ = true;
30     return static_cast<void*>(&space_);
31   }
32 
deallocate(void * p)33   void deallocate(void* p)
34   {
35     if (p != static_cast<void*>(&space_))
36       ::operator delete(p);
37     else
38       in_use_ = false;
39   }
40 
41 private:
42   allocator(const allocator&);
43   allocator& operator=(const allocator&);
44 
45   // Whether the reusable memory space is currently in use.
46   bool in_use_;
47 
48   // The reusable memory space made available by the allocator.
49   boost::aligned_storage<1024>::type space_;
50 };
51 
52 #endif // ALLOCATOR_HPP
53