Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
object-pool.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_COMMON_OBJECT_POOL_H
17 #define IMPALA_COMMON_OBJECT_POOL_H
18 
19 #include <vector>
20 #include <boost/thread/locks.hpp>
21 #include <boost/thread/mutex.hpp>
22 
23 #include "util/spinlock.h"
24 
25 namespace impala {
26 
30 class ObjectPool {
31  public:
33 
35  for (ElementVector::iterator i = objects_.begin();
36  i != objects_.end(); ++i) {
37  delete *i;
38  }
39  }
40 
41  template <class T>
42  T* Add(T* t) {
46  boost::lock_guard<SpinLock> l(lock_);
47  objects_.push_back(obj);
48  return t;
49  }
50 
51  private:
52  struct GenericElement {
53  virtual ~GenericElement() {}
54  };
55 
56  template <class T>
58  SpecificElement(T* t): t(t) {}
60  delete t;
61  }
62 
63  T* t;
64  };
65 
66  typedef std::vector<GenericElement*> ElementVector;
69 };
70 
71 }
72 
73 #endif
Lightweight spinlock.
Definition: spinlock.h:24
ElementVector objects_
Definition: object-pool.h:67
std::vector< GenericElement * > ElementVector
Definition: object-pool.h:66