File: freerange

package info (click to toggle)
vtk6 6.1.0+dfsg2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 165,164 kB
  • ctags: 226,428
  • sloc: cpp: 1,354,490; ansic: 730,748; python: 227,134; tcl: 48,285; xml: 8,290; yacc: 4,832; java: 3,827; perl: 3,108; lex: 1,809; sh: 1,437; asm: 471; makefile: 229
file content (350 lines) | stat: -rw-r--r-- 10,192 bytes parent folder | download | duplicates (12)
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    freerange

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*----------------------------------------------------------------------------
 Copyright (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
// .NAME freerange - allocates and deallocates contiguous memory efficiently
//
// .SECTION Description
//
// .SECTION Thanks
// Thanks to David Thompson for implementing this class.

#ifndef freerange_H
#define freerange_H

#include <assert.h>

#include <vtksys/stl/algorithm>
#include <vtksys/stl/deque>
#include <vtksys/stl/functional>
#include <vtksys/stl/vector>
#include <vtksys/stl/stdexcept>
#include <vtksys/ios/iostream>
#include <stdlib.h>
#include <string.h>

#if defined(__BORLANDC__)
  // Disable Borland compiler warning messages that often occur in valid code.
#  pragma warn -8027 /* functions w/ do/for/while not expanded inline */
#endif

/**\brief A free list template class in the spirit of the STL.
 *
 * A free list is a vector of items stored by value. Since items
 * are stored by value, it avoids the overhead of memory allocation
 * and deallocation as items are inserted and removed. When an item
 * is removed, it is not destroyed; a mark is made in a vector of
 * "available" positions. The next time an entry is inserted, the
 * most recently removed item's position in the vector of all items
 * is used to store the newly inserted member.
 */
template< class T, class idx_t, T empty_entry=-1 >
class freerange
{
  public:
    typedef vtksys_stl::deque<idx_t>                dead_list_entry_t;
    typedef vtksys_stl::vector< dead_list_entry_t > dead_list_t;
  protected:
    T* array ;
    idx_t array_len ; // max id + 1
    idx_t array_top ; // id of next available entry
    dead_list_t dead ;
    int dead_size;
    idx_t used;
  public:
    freerange()
    {
      array_len = 16 ;
      array_top = 0 ;
      array = new T [ array_len ];
      for ( idx_t i=array_top; i<array_len; ++i )
        array[i] = empty_entry;

      dead_size = 27;
      dead.resize( dead_size+1 ); // dead[0] is unused.

      used = 0;
    }
    ~freerange()
    {
      delete [] array ;
    }

    idx_t size() const { return used; }
    idx_t max_id() const { return array_top - 1; }
    idx_t capacity() const { return array_len; }

    // Access to the array itself.
    T* pointer(idx_t pos) { return array + pos; }

    int max_chunked_grab_size() const { return dead_size; }
    void set_max_chunked_grab_size( int c )
    {
      if ( c == dead_size )
        return;
      if ( c < 1 )
        return;
      dead_size = c;
      // FIXME: the following resize will leak any chunks larger than c!
      dead.resize( dead_size + 1 );
    }

    bool grab_internal( idx_t& v, int num ) 
    {
      if ( (num <= dead_size) && (! dead[num].empty()) ) {
        v = dead[num].front() ;
        dead[num].pop_front() ;
        used += num;
        return (v != 0);
      }
      if ( array_top + num > array_len ) {
        // OK, time to resize the array
        do {
          array_len <<= 1 ;
        } while ( array_top + num > array_len );

        T* array_tmp = new T [ array_len ];
        if ( ! array_tmp )
          throw vtksys_stl::runtime_error( "freerange memory allocation failed" );
        for ( idx_t i=0; i<array_top; i++ )
          array_tmp[i] = array[i];
        delete [] array;
        array = array_tmp;
      }
      v = array_top;
      array_top += num;
      used += num;
      return (v != 0) ;
    }

    idx_t grab( int num=1 )
    {
      if ( num <= 0 )
        return -1;
      idx_t v;
      if ( grab_internal( v, num ) )
        for (idx_t i=0; i<num; ++i )
          array[ v + i ] = empty_entry;
      return v;
    }

    idx_t grabAndAssign( const T& src )
    { idx_t entry; grab_internal(entry,1) ; array[entry] = src ; return entry ; }

    void mark_as_freed( idx_t i, idx_t num )
    {
      for ( idx_t stop=i+num; i < stop; i++ )
        array[ i ] = empty_entry;
    }

    void free( idx_t i, idx_t num=1 )
    {
      if ( num <= 0 )
        return;

      if ( i > array_top )
        { // trying to fee past end of storage. continue quietly
        return;
        }
      else if ( i + num > array_top )
        { // trying to fee past end of storage. truncate and continue quietly
        array_top = i;
        used -= array_top - i;
        return;
        }
      else if ( i == array_top - num )
        {
        array_top -= num ;
        used -= num;
        return ;
        }
      mark_as_freed( i, num );
      // break up large freed blocks into random sizes that all fit in our dead pool
      while ( num >= dead_size ) {
        // Not all OS's have lrand48(), try rand() instead.
        //register int deadpool = lrand48() % dead_size + 1;
        register int deadpool = rand() % dead_size + 1;
        dead[deadpool].push_front( i );
        i += deadpool;
        num -= deadpool;
        used -= deadpool;
      }
      if ( num ) {
        dead[num].push_front( i );
        used -= num;
      }
    }

    T& operator[] ( idx_t i )
    {
      return array[i] ;
    }

    const T& operator[] ( idx_t i ) const
    {
      return array[i] ;
    }

    void resize( idx_t new_size )
    {
      idx_t i = 0;

      // note that resizing doesn't guarantee the number of contiguous free
      // or even free items in the list... it only guarantees storage for
      // some total number of items assuming that all the holes get filled.
      if ( new_size == array_len ) {
        return;
      } else if ( new_size < array_len ) {
        // time to remove elements AND possibly dead entries
        // until we're trimmed down to size
        array_len = new_size;
        T* array_tmp = new T [ array_len ];
        if ( ! array_tmp )
          throw vtksys_stl::runtime_error( "freerange memory allocation failed" );
        if ( array_top > array_len ) {
          used -= array_top - array_len;
          array_top = array_len;
        }
        for ( i=0; i<array_top; i++ )
          array_tmp[i] = array[i];
        delete [] array;
        for ( i=array_top; i<new_size; ++i )
          array_tmp[i] = empty_entry;
        array = array_tmp;
        for ( i=1; i<dead_size; i++ ) { // dead[0] is unused
          typename dead_list_entry_t::iterator it = vtksys_stl::remove_if( dead[i].begin(), dead[i].end(), vtksys_stl::bind2nd( vtksys_stl::greater_equal<idx_t>(), new_size ) );
          dead[i].erase( it, dead[i].end() );
        }
      } else { // new_size > array_len
        // allocate more memory
        array_len = new_size;
        T* array_tmp = new T [ array_len ];
        if ( ! array_tmp )
          throw vtksys_stl::runtime_error( "freerange memory allocation failed" );
        for ( i=0; i<array_top; i++ )
          array_tmp[i] = array[i];
        for ( i=array_top; i<new_size; ++i )
          array_tmp[i] = empty_entry;
        delete [] array;
        array = array_tmp;
      }
    }

    // dead[0] is unused!
    typename dead_list_t::iterator dead_begin() { return dead.begin()+1; }
    typename dead_list_t::iterator dead_end() { return dead.end(); }

    // dead[0] is unused!
    typename dead_list_t::const_iterator dead_begin() const { return dead.begin()+1; }
    typename dead_list_t::const_iterator dead_end() const { return dead.end(); }

    void clear()
    {
      array_top = 0;
      used = 0;
      for ( typename dead_list_t::iterator it = dead_begin(); it != dead_end(); ++it )
        it->clear();
    }

    T empty_entry_value() { return empty_entry; }

    class iterator ;
    friend class iterator ;

    iterator begin() ;
    iterator end() ;

    class iterator {
      protected:
        idx_t posn ;
        freerange* fl ;

        void check( idx_t p )
        {
          if ( ! fl ) {
            posn = 0 ;
            return ;
          }

          while ( ((*fl)[p] == empty_entry) && (p < fl->array_top) ) 
            p++ ;

          if ( p >= fl->array_top ) {
            fl = 0 ;
            posn = 0 ;
          } else {
            posn = p ;
          }
        }

      public:
        typedef vtksys_stl::bidirectional_iterator_tag iterator_category;

        iterator()
        { fl = 0 ; posn = 0 ; }

        iterator( freerange<T,idx_t,empty_entry>* list, idx_t p )
        { fl = list ; check(p) ; }

        iterator( const iterator& i )
        { fl = i.fl ; posn = i.posn ; }

        ~iterator() { } ;

        iterator& operator = ( idx_t p )
        { check( p ) ; }

        idx_t position() const { return posn ; }

        bool operator == ( const iterator& i ) const
        { return ((i.fl == fl) && (i.posn == posn)) ; }

        bool operator != ( const iterator& i ) const
        { return ( (fl != i.fl) || (posn != i.posn) ) ; }

        iterator& operator ++ ()
        { check( ++posn ) ; return *this ; }

        const T& operator * () const
        { assert(fl) ; return fl->array[posn] ; }

        //const T* operator -> () const
        //{ assert(fl) ; return &fl->array[posn] ; }

        T& operator * ()
        { assert(fl) ; return fl->array[posn] ; }

        //T* operator -> ()
        //{ assert(fl) ; return &fl->array[posn] ; }
    } ;

} ;


template< class T, class idx_t, T empty_entry >
typename freerange<T,idx_t,empty_entry>::iterator freerange<T,idx_t,empty_entry>::begin()
{
  return iterator( this, 0 ) ;
}

template< class T, class idx_t, T empty_entry >
typename freerange<T,idx_t,empty_entry>::iterator freerange<T,idx_t,empty_entry>::end() 
{
  return iterator( 0, 0 ) ;
}

#endif // freerange_H