This file is indexed.

/usr/include/gecode/kernel/allocators.hpp is in libgecode-dev 4.4.0-3.

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Filip Konvicka <filip.konvicka@logis.cz>
 *
 *  Copyright:
 *     LOGIS, s.r.o., 2009
 *
 *  Bugfixes provided by:
 *     Gustavo Gutierrez
 *
 *  Last modified:
 *     $Date: 2013-03-07 17:39:13 +0100 (Thu, 07 Mar 2013) $ by $Author: schulte $
 *     $Revision: 13458 $
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <limits>

namespace Gecode {

  template<class T> struct space_allocator;

/**
 
\defgroup FuncMemAllocator Using allocators with Gecode
\ingroup FuncMem

%Gecode provides two allocator classes that can be used with
generic data structures such as those of the STL
(e.g. <tt>std::set</tt>). Memory can be allocated from the space heap
(see Space) or from a region (see Region).
 
\section FuncMemAllocatorA Using allocators with dynamic data structures

There are two possible scenarios for allocator usage. One is to
let the dynamic data structure allocate memory from the space or
region:

\code
struct MySpace : public Space {
  typedef std::set<int, std::less<int>, Gecode::space_allocator<int> > S;
  S safe_set;

  MySpace(void)
    : safe_set(S::key_compare(), S::allocator_type(*this))
  {}

  MySpace(bool share, MySpace& other)
    : Space(share, other), 
      safe_set(other.safe_set.begin(), other.safe_set.end(), 
               S::key_compare(), S::allocator_type(*this))
  {}

  ...
};
\endcode

In this example, \a S is a set that allocates its nodes from the
space heap. Note that we pass an instance of space_allocator
bound to this space to the constructor of the set. A similar
thing must be done in the copying constructor, where we must be
sure to pass an allocator that allocates memory from the
destination ("this") space, not "other".  Note that the set
itself is a member of \a MySpace, so it is destroyed within
<i>MySpace::~MySpace</i> as usual.  The set destructor destroys
all contained items and deallocates all nodes in its destructors.

\section FuncMemAllocatorB Preventing unnecessary destruction overhead

In the above example, we know that the value type in \a S is a
builtin type and does not have a destructor.  So what happens
during \a safe_set destruction is that it just deallocates all
nodes. However, we know that all nodes were allocated from the
space heap, which is going to disappear with the space anyway.
If we prevent calling \a safe_set destructor, we may save a
significant amount of time during space destruction.  A safe way
of doing this is to allocate the set object itself on the space
heap, and keep only a reference to it as a member of the
space. We can use the convenience helpers Space::construct for
the construction.

\code
struct MySpace : public Space {
  typedef std::set<int, std::less<int>, Gecode::space_allocator<int> > S;
  S& fast_set;

  MySpace(void)
    : fast_set(construct<S>(S::key_compare(), S::allocator_type(*this)))
  {}

  MySpace(bool share, MySpace& other)
    : Space(share, other), 
	fast_set(construct<S>(other.safe_set.begin(), other.safe_set.end(), 
	S::key_compare(), S::allocator_type(*this)))
  {}

  ...
};
\endcode

\section FuncMemAllocatorC Region example

The above examples were using a space_allocator. A region_allocator
works similarly, one just should keep in mind that regions never
really release any memory. Similar to Space, Region provides
helper functions Region::construct to make non-stack allocation
easy.

\code
Space& home = ...;
typedef std::set<int, std::less<int>, Gecode::region_allocator<int> > SR;
// Create a set with the region allocator. Note that the set destructor is still quite costly...
{
  Region r(home);
  SR r_safe_set(SR::key_compare(), (SR::allocator_type(r)));
  for(int i=0; i<10000; ++i)
    r_safe_set.insert(i*75321%10000);
}
// Create a set directly in the region (not on the stack). No destructors will be called.
{
  Region r(*this);
  SR& r_fast_set=r.construct<SR>(SR::key_compare(), SR::allocator_type(r));
  for(int i=0; i<10000; ++i)
    r_fast_set.insert(i*75321%10000);
}
\endcode

*/


  /**
   * \brief %Space allocator - specialization for \c void.
   *
   * The specialization is needed as the default instantiation fails 
   * for \c void.
   */
  template<> 
  struct space_allocator<void> {
    typedef void*       pointer;
    typedef const void* const_pointer;
    typedef void        value_type;
    /// Rebinding helper (returns the type of a similar allocator for type \a U)
    template<class U> struct rebind {
      typedef space_allocator<U> other;
    };
  };

  /**
   * \brief Allocator that allocates memory from a space heap
   *
   * Note that this allocator may be used to construct dynamic 
   * data structures that allocate memory from the space heap,
   * or even reside in the space heap as a whole.
   *
   * \ingroup FuncMemAllocator
   */
  template<class T> 
  struct space_allocator {
    /// Type of objects the allocator creates. This is identical to \a T.
    typedef T value_type;
    /// Type that can represent the size of the largest object.
    typedef size_t size_type;
    /// Type that can represent the difference between any two pointers.
    typedef ptrdiff_t difference_type;
    /// Type of pointers returned by the allocator.
    typedef T* pointer;
    /// Const version of pointer.
    typedef T const* const_pointer;
    /// Non-const reference to \a T.
    typedef T& reference;
    /// Const reference to \a T.
    typedef T const&  const_reference;
    /// Rebinding helper (returns the type of a similar allocator for type \a U).
    template<class U> struct rebind { 
      /// The allocator type for \a U
      typedef space_allocator<U> other;
    };

    /// The space that we allocate objects from
    Space& space;

    /**
     * \brief Construction
     * @param space The space whose heap to allocate objects from.
     */
    space_allocator(Space& space) throw() : space(space) {}
    /**
     * \brief Copy construction
     * @param al The allocator to copy.
     */
    space_allocator(space_allocator const& al) throw() : space(al.space) {}
    /**
     * \brief Assignment operator
     * @param al The allocator to assign.
     */
    space_allocator& operator =(space_allocator const& al) {
      (void) al;
      assert(&space == &al.space);
      return *this;
    }
    /**
     * \brief Copy from other instantiation
     * @param al The source allocator.
     */
    template<class U> 
    space_allocator(space_allocator<U> const& al) throw() : space(al.space) {}

    /// Convert a reference \a x to a pointer
    pointer address(reference x) const { return &x; }
    /// Convert a const reference \a x to a const pointer
    const_pointer address(const_reference x) const { return &x; }
    /// Returns the largest size for which a call to allocate might succeed.
    size_type max_size(void) const throw() {
      return std::numeric_limits<size_type>::max() / 
        (sizeof(T)>0 ? sizeof(T) : 1);
    }
    /**
     * \brief Allocates storage 
     *
     * Returns a pointer to the first element in a block of storage
     * <tt>count*sizeof(T)</tt> bytes in size. The block is aligned 
     * appropriately for objects of type \a T. Throws the exception 
     * \a bad_alloc if the storage is unavailable.
     */
    pointer allocate(size_type count) {
      return static_cast<pointer>(space.ralloc(sizeof(T)*count));
    }

    /**
     * \brief Allocates storage 
     *
     * Returns a pointer to the first element in a block of storage
     * <tt>count*sizeof(T)</tt> bytes in size. The block is aligned 
     * appropriately for objects of type \a T. Throws the exception 
     * \a bad_alloc if the storage is unavailable.
     * The (unused) parameter could be used as an allocation hint, 
     * but this allocator ignores it.
     */
    pointer allocate(size_type count, const void * const hint) {
      (void) hint;
      return allocate(count);
    }

    /// Deallocates the storage obtained by a call to allocate() with arguments \a count and \a p.
    void deallocate(pointer p, size_type count) {
      space.rfree(static_cast<void*>(p), count);
    }

    /*
     * \brief Constructs an object 
     *
     * Constructs an object of type \a T with the initial value of \a t 
     * at the location specified by \a element. This function calls 
     * the <i>placement new()</i> operator.
     */
    void construct(pointer element, const_reference t) {
      new (element) T(t);
    }

    /// Calls the destructor on the object pointed to by \a element.
    void destroy(pointer element) {
      element->~T();
    }
  };

  /**
   * \brief Tests two space allocators for equality
   *
   * Two allocators are equal when each can release storage allocated 
   * from the other.
   */
  template<class T1, class T2>
  bool operator==(space_allocator<T1> const& al1, 
                  space_allocator<T2> const& al2) throw() {
    return &al1.space == &al2.space;
  }

  /**
   * \brief Tests two space allocators for inequality
   *
   * Two allocators are equal when each can release storage allocated 
   * from the other.
   */
  template<class T1, class T2>
  bool operator!=(space_allocator<T1> const& al1, 
                  space_allocator<T2> const& al2) throw() {
    return &al1.space != &al2.space;
  }


  template<class T> struct region_allocator;

  /**
   * \brief %Region allocator - specialization for \c void.
   *
   * The specialization is needed as the default instantiation fails 
   * for \c void.
   */
  template<> 
  struct region_allocator<void> {
    typedef void* pointer;
    typedef const void* const_pointer;
    typedef void value_type;
    /// Rebinding helper (returns the type of a similar allocator for type \a U)
    template<class U> struct rebind {
      typedef region_allocator<U> other;
    };
  };

  /**
   * \brief Allocator that allocates memory from a region
   *
   * Note that this allocator may be used to construct dynamic data structures
   * that allocate memory from the region, or even reside in the region as a whole.
   *
   * \ingroup FuncMemAllocator
   */
  template<class T> 
  struct region_allocator {
    /// Type of objects the allocator creates. This is identical to \a T.
    typedef T value_type;
    /// Type that can represent the size of the largest object.
    typedef size_t size_type;
    /// Type that can represent the difference between any two pointers.
    typedef ptrdiff_t difference_type;
    /// Type of pointers returned by the allocator.
    typedef T* pointer;
    /// Const version of pointer.
    typedef T const*  const_pointer;
    /// Non-const reference to \a T.
    typedef T&  reference;
    /// Const reference to \a T.
    typedef T const& const_reference;

    /// Rebinding helper (returns the type of a similar allocator for type \a U).
    template<class U> struct rebind { 
      /// The allocator type for \a U
     typedef region_allocator<U> other;
    };

    /// The region that we allocate objects from
    Region& region;

    /**
     * \brief Construction
     * @param region The region to allocate objects from.
     */
    region_allocator(Region& region) throw() 
      : region(region) {}
    /**
     * \brief Copy construction
     * @param al The allocator to copy.
     */
    region_allocator(region_allocator const& al) throw() 
      : region(al.region) {}
    /**
     * \brief Copy from other instantiation.
     * @param al The source allocator.
     */
    template<class U> 
    region_allocator(region_allocator<U> const& al) throw() 
      : region(al.region) {}

    /// Convert a reference \a x to a pointer
    pointer address(reference x) const { return &x; }
    /// Convert a const reference \a x to a const pointer
    const_pointer address(const_reference x) const { return &x; }
    /// Returns the largest size for which a call to allocate might succeed.
    size_type max_size(void) const throw() {
      return std::numeric_limits<size_type>::max() 
        / (sizeof(T)>0 ? sizeof(T) : 1);
    }

     /**
      * \brief Allocates storage 
      *
      * Returns a pointer to the first element in a block of storage
      * <tt>count*sizeof(T)</tt> bytes in size. The block is aligned 
      * appropriately for objects of type \a T. Throws the exception 
      * \a bad_alloc if the storage is unavailable.
      */
    pointer allocate(size_type count) {
      return static_cast<pointer>(region.ralloc(sizeof(T)*count));
    }

     /**
      * \brief Allocates storage 
      *
      * Returns a pointer to the first element in a block of storage
      * <tt>count*sizeof(T)</tt> bytes in size. The block is aligned 
      * appropriately for objects of type \a T. Throws the exception 
      * \a bad_alloc if the storage is unavailable.
      *
      * The (unused) parameter could be used as an allocation hint, 
      * but this allocator ignores it.
      */
    pointer allocate(size_type count, const void * const hint) {
      (void) hint;
      return allocate(count);
    }
    
    /**
     * \brief Deallocates storage
     *
     * Deallocates storage obtained by a call to allocate() with 
     * arguments \a count and \a p. Note that region allocator never 
     * actually deallocates memory (so this function does nothing);
     * the memory is released when the region is destroyed.
     */
    void deallocate(pointer* p, size_type count) {
      region.rfree(static_cast<void*>(p), count);
    }

    /**
     * \brief Constructs an object
     *
     * Constructs an object of type \a T with the initial value 
     * of \a t at the location specified by \a element. This function 
     * calls the <i>placement new()</i> operator.
     */
     void construct(pointer element, const_reference t) {
      new (element) T(t);
    }

    /// Calls the destructor on the object pointed to by \a element.
    void destroy(pointer element) {
      element->~T();
    }
  };

  /*
   * \brief Tests two region allocators for equality
   *
   * Two allocators are equal when each can release storage allocated 
   * from the other.
   */
  template<class T1, class T2>
  bool operator==(region_allocator<T1> const& al1, 
                  region_allocator<T2> const& al2) throw() {
    return &al1.region == &al2.region;
  }

  /*
   * \brief Tests two region allocators for inequality
   *
   * Two allocators are equal when each can release storage allocated
   * from the other.
   */
  template<class T1, class T2>
  bool operator!=(region_allocator<T1> const& al1, 
                  region_allocator<T2> const& al2) throw() {
    return &al1.region != &al2.region;
  }

}

// STATISTICS: kernel-memory