Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
InlineAllocator.h
Go to the documentation of this file.
1 #ifndef QPID_INLINEALLOCATOR_H
2 #define QPID_INLINEALLOCATOR_H
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include <memory>
26 #include <assert.h>
27 #include <boost/type_traits/type_with_alignment.hpp>
28 #include <boost/type_traits/alignment_of.hpp>
29 
30 namespace qpid {
31 
32 template <typename RequestedType, typename InlineType, typename BaseAllocator, size_t Max>
33 struct InlineRebind;
34 
35 
40 template <class BaseAllocator, size_t Max>
41 class InlineAllocator : public BaseAllocator {
42  public:
43  typedef typename BaseAllocator::pointer pointer;
44  typedef typename BaseAllocator::size_type size_type;
45  typedef typename BaseAllocator::value_type value_type;
46 
47  InlineAllocator() : allocated(false) {}
48  InlineAllocator(const InlineAllocator& x) : BaseAllocator(x), allocated(false) {}
49 
51  if (n <= Max && !allocated) {
52  allocated=true;
53  return reinterpret_cast<value_type*>(address());
54  }
55  else
56  return BaseAllocator::allocate(n, 0);
57  }
58 
60  if (p == address()) {
61  assert(allocated);
62  allocated=false;
63  }
64  else
66  }
67 
68  template<typename T1>
69  struct rebind {
71  };
72 
73  private:
74  // POD object with alignment and size to hold Max value_types.
75  static const size_t ALIGNMENT=boost::alignment_of<value_type>::value;
76  typedef typename boost::type_with_alignment<ALIGNMENT>::type Aligner;
77  union Store {
78  Aligner aligner_;
79  char sizer_[sizeof(value_type)*Max];
80  } store;
81  value_type* address() { return reinterpret_cast<value_type*>(&store); }
82  bool allocated;
83 };
84 
85 
86 // Rebind: if RequestedType == InlineType, use the InlineAllocator,
87 // otherwise, use the BaseAllocator without any inlining.
88 
89 template <typename RequestedType, typename InlineType, typename BaseAllocator, size_t Max>
90 struct InlineRebind {
91  typedef typename BaseAllocator::template rebind<RequestedType>::other other;
92 };
93 
94 template <typename T, typename BaseAllocator, size_t Max>
95 struct InlineRebind<T, T, BaseAllocator, Max> {
97 };
98 
99 } // namespace qpid
100 
101 #endif

Qpid C++ API Reference
Generated on Thu Nov 15 2012 for Qpid C++ Client API by doxygen 1.8.1.2