summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/tests/latency/allocator.hpp
blob: 56a7af3ae9bb121eca38bce901935921596836a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// allocator.hpp
// ~~~~~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP

#include <boost/aligned_storage.hpp>

// Represents a single connection from a client.
class allocator
{
public:
  allocator()
    : in_use_(false)
  {
  }

  void* allocate(std::size_t n)
  {
    if (in_use_ || n >= 1024)
      return ::operator new(n);
    in_use_ = true;
    return static_cast<void*>(&space_);
  }

  void deallocate(void* p)
  {
    if (p != static_cast<void*>(&space_))
      ::operator delete(p);
    else
      in_use_ = false;
  }

private:
  allocator(const allocator&);
  allocator& operator=(const allocator&);

  // Whether the reusable memory space is currently in use.
  bool in_use_;

  // The reusable memory space made available by the allocator.
  boost::aligned_storage<1024>::type space_;
};

#endif // ALLOCATOR_HPP