Lucene++ - a full-featured, c++ search engine
API Documentation


 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
LuceneAllocator.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef ALLOCATOR_H
8 #define ALLOCATOR_H
9 
10 #include "Config.h"
11 
12 namespace Lucene
13 {
15  LPPAPI void* AllocMemory(size_t size);
16 
18  LPPAPI void* ReallocMemory(void* memory, size_t size);
19 
21  LPPAPI void FreeMemory(void* memory);
22 
25  LPPAPI void ReleaseThreadCache();
26 
27  #ifdef LPP_USE_ALLOCATOR
28 
34  template <typename TYPE>
35  class LuceneAllocator
36  {
37  public:
38  typedef size_t size_type;
39  typedef ptrdiff_t difference_type;
40  typedef TYPE* pointer;
41  typedef const TYPE* const_pointer;
42  typedef TYPE& reference;
43  typedef const TYPE& const_reference;
44  typedef TYPE value_type;
45 
47  {
48  }
49 
51  {
52  }
53 
54  pointer allocate(size_type n, const void* = 0)
55  {
56  return (TYPE*)AllocMemory((size_t)(n * sizeof(TYPE)));
57  }
58 
59  void deallocate(void* p, size_type)
60  {
61  if (p != NULL)
62  FreeMemory(p);
63  }
64 
65  pointer address(reference x) const
66  {
67  return &x;
68  }
69 
70  const_pointer address(const_reference x) const
71  {
72  return &x;
73  }
74 
75  LuceneAllocator<TYPE>& operator= (const LuceneAllocator&)
76  {
77  return *this;
78  }
79 
80  void construct(pointer p, const TYPE& val)
81  {
82  new ((TYPE*)p) TYPE(val);
83  }
84 
85  void destroy(pointer p)
86  {
87  p->~TYPE();
88  }
89 
90  size_type max_size() const
91  {
92  return size_t(-1);
93  }
94 
95  template <class U>
96  struct rebind
97  {
98  typedef LuceneAllocator<U> other;
99  };
100 
101  template <class U>
102  LuceneAllocator(const LuceneAllocator<U>&)
103  {
104  }
105  };
106 
107  template <typename TYPE>
108  inline bool operator== (const LuceneAllocator<TYPE>&, const LuceneAllocator<TYPE>&)
109  {
110  return true;
111  }
112 
113  template <typename TYPE>
114  inline bool operator!= (const LuceneAllocator<TYPE>&, const LuceneAllocator<TYPE>&)
115  {
116  return false;
117  }
118 
119  template <>
120  class LuceneAllocator<void>
121  {
122  public:
123  typedef void* pointer;
124  typedef const void* const_pointer;
125  typedef void value_type;
126 
128  {
129  }
130 
132  {
133  }
134 
135  template <class U>
136  struct rebind
137  {
138  typedef LuceneAllocator<U> other;
139  };
140 
141  template <class U>
142  LuceneAllocator(const LuceneAllocator<U>&)
143  {
144  }
145  };
146 
147  #endif
148 }
149 
150 #ifndef LPP_USE_ALLOCATOR
151 #define LuceneAllocator std::allocator
152 #endif
153 
154 #endif

clucene.sourceforge.net