This file is indexed.

/usr/include/cxxtools/pool.h is in libcxxtools-dev 2.0-4ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2003 Tommi Maekitalo
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * As a special exception, you may use this file as part of a free
 * software library without restriction. Specifically, if other files
 * instantiate templates or use macros or inline functions from this
 * file, or you compile this file and link it with other files to
 * produce an executable, this file does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License. This exception does not however invalidate any other
 * reasons why the executable file might be covered by the GNU Library
 * General Public License.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef CXXTOOLS_POOL_H
#define CXXTOOLS_POOL_H

#include <cxxtools/condition.h>
#include <stack>

namespace cxxtools
{

/**
 This class is a factory for objects wich are default constructable.
 */
template <class T>
class DefaultCreator
{
  public:
    T* operator() ()
    { return new T(); }
};

/**
 A Pool is a container for reusable objects.

 It maintains a list of objects, which are not in use. If a program
 needs a object, he can request one with the get-method. The Pool
 returns a object, which is convertable to the needed object. As long
 as the program holds this special object, the object is in use.

 Objects are created when needed and only deleted on request.
 A upper limit of objects can be passed to the constructor. This makes
 only sense in a multithreaded environment, because the get-method
 blocks, when the limit is reached and waits for someone to release a
 object.
 */
template <typename T, typename CreatorType = DefaultCreator<T> >
class Pool
{
  public:
    /**
     The PoolObject holds a reference to the object.
     The held object is released for reuse, when the last reference
     is destroyed.
     */
    class PoolObject
    {
      public:
        typedef Pool<T, CreatorType> pool_type;
        typedef PoolObject* ptr_type;
        typedef const PoolObject* const_ptr_type;

      private:
        T* ptr;
        pool_type* mypool;

        mutable const_ptr_type prev;
        mutable const_ptr_type next;

        void unlink(bool reuse = true)
        {
          if (mypool)
          {
            {
              MutexLock lock(mypool->mutex);
              if (next == this)
              {
                if (reuse && (mypool->maxCount == 0 || mypool->currentCount <= mypool->maxCount))
                  mypool->freePool.push(ptr);
                else
                {
                  delete ptr;
                  --mypool->currentCount;
                }
                mypool->objectAvailable.signal();
              }
              else
              {
                next->prev = prev;
                prev->next = next;
              }
            }
            next = prev = this;
          }
        }

        void link(const PoolObject& s)
        {
          mypool = s.mypool;
          ptr = s.ptr;
          if (mypool)
          {
            prev = &s;
            next = s.next;
            MutexLock lock(mypool->mutex);
            prev->next = this;
            next->prev = this;
          }
        }

      public:
        PoolObject()
          : ptr(0),
            mypool(0),
            prev(this),
            next(this)
        { }

        PoolObject(T* o, pool_type& p)
          : ptr(o),
            mypool(&p),
            prev(this),
            next(this)
        { }

        PoolObject(const PoolObject& s)
          : ptr(s.ptr),
            mypool(s.mypool),
            prev(this),
            next(this)
        {
          if (mypool)
            link(s);
        }

        ~PoolObject()
        {
          unlink();
        }

        PoolObject& operator= (const PoolObject& s)
        {
          if (ptr != s.ptr)
          {
            unlink();
            link(s);
          }
          return *this;
        }

        /**
         The reference can be dropped before destruction.
         */
        void release(bool reuse = true)
        {
          unlink(reuse);
          mypool = 0;
        }

        /// The object can be dereferenced like the held object
        T* operator->() const              { return ptr; }
        /// The object can be dereferenced like the held object
        T& operator*() const               { return *ptr; }
        /// The object is convertable to a pointer to the held object
        operator T* () const               { return ptr; }
        /// The object is convertable to a reference to the held object
        operator T& () const               { return *ptr; }
        bool operator== (const T* p) const { return ptr == p; }
    };

    friend class PoolObject;

    typedef PoolObject objectptr_type;

  private:
    typedef std::stack<T*> objectcontainer_type;
    unsigned currentCount;
    unsigned maxCount;
    objectcontainer_type freePool;
    mutable Mutex mutex;
    mutable Condition objectAvailable;
    CreatorType creator;

    // make non-copyable
    Pool(const Pool&); // no implementation
    Pool& operator= (const Pool&); // no implementation

  public:
    /// Create a Pool with a maximum count.
    /// If count is 0, no limit is used
    explicit Pool(unsigned _maxCount = 0, CreatorType _creator = CreatorType())
      : currentCount(0),
        maxCount(_maxCount),
        creator(_creator)
    { }

    /// create a Pool without limit and a special creator
    explicit Pool(CreatorType _creator)
      : currentCount(0),
        maxCount(0),
        creator(_creator)
    { }

    /// destructing the Pool destroys all free objects too
    ~Pool()
    {
      while (!freePool.empty())
      {
        delete freePool.top();
        freePool.pop();
      }
    }

    /**
     Returns a wrapper-class, which helds the object in use.

     This method does the actual work of the Pool. It creates new
     objects, when needed and the upper limit is not reached.
     */
    objectptr_type get()
    {
      MutexLock lock(mutex);

      if (maxCount > 0)
        while (freePool.empty() && currentCount >= maxCount)
          objectAvailable.wait(lock);

      T* obj;

      if (freePool.empty())
      {
        obj = creator();
        ++currentCount;
      }
      else
      {
        obj = freePool.top();
        freePool.pop();
      }

      lock.unlock();

      objectptr_type po(obj, *this);
      return po;
    }

    /**
     Release all free objects.

     A maximum number of objects kept can be passed.
     Objects in use are not counted at all.
     */
    void drop(unsigned keep = 0)
    {
      MutexLock lock(mutex);
      while (freePool.size() > keep)
      {
        delete freePool.top();
        --currentCount;
        freePool.pop();
      }
      objectAvailable.signal();
    }

    unsigned getCurrentSize() const
    {
      MutexLock lock(mutex);
      return currentCount;
    }

    unsigned getMaximumSize() const
    { return maxCount; }

    void setMaximumSize(unsigned s)
    {
      MutexLock lock(mutex);
      maxCount = s;
      while (!freePool.empty() && currentCount > maxCount)
      {
        delete freePool.top();
        --currentCount;
        freePool.pop();
      }

    }

    CreatorType& getCreator()               { return creator; }
    const CreatorType& getCreator() const   { return creator; }
};

}

#endif // CXXTOOLS_POOL_H