00001 #ifndef QPID_INLINEALLOCATOR_H
00002 #define QPID_INLINEALLOCATOR_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <memory>
00026 #include <assert.h>
00027
00028 namespace qpid {
00029
00034 template <class BaseAllocator, size_t Max>
00035 class InlineAllocator : public BaseAllocator {
00036 public:
00037 typedef typename BaseAllocator::pointer pointer;
00038 typedef typename BaseAllocator::size_type size_type;
00039 typedef typename BaseAllocator::value_type value_type;
00040
00041 InlineAllocator() : allocated(false) {}
00042
00043 pointer allocate(size_type n) {
00044 if (n <= Max && !allocated) {
00045 allocated=true;
00046 return store;
00047 }
00048 else
00049 return BaseAllocator::allocate(n, 0);
00050 }
00051
00052 void deallocate(pointer p, size_type n) {
00053 if (p == store) {
00054 assert(allocated);
00055 allocated=false;
00056 }
00057 else BaseAllocator::deallocate(p, n);
00058 }
00059
00060 template<typename T1>
00061 struct rebind {
00062 typedef typename BaseAllocator::template rebind<T1>::other BaseOther;
00063 typedef InlineAllocator<BaseOther, Max> other;
00064 };
00065
00066 private:
00067 value_type store[Max];
00068 bool allocated;
00069 };
00070
00071 }
00072
00073 #endif