~ubuntu-branches/ubuntu/trusty/rheolef/trusty-proposed

« back to all changes in this revision

Viewing changes to nfem/lib/heap_allocator.h

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2010-06-12 09:08:59 UTC
  • Revision ID: james.westby@ubuntu.com-20100612090859-8gpm2gc7j3ab43et
Tags: upstream-5.89
ImportĀ upstreamĀ versionĀ 5.89

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _RHEO_HEAP_ALLOCATOR_H
 
2
#define _RHEO_HEAP_ALLOCATOR_H
 
3
///
 
4
/// This file is part of Rheolef.
 
5
///
 
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
7
///
 
8
/// Rheolef is free software; you can redistribute it and/or modify
 
9
/// it under the terms of the GNU General Public License as published by
 
10
/// the Free Software Foundation; either version 2 of the License, or
 
11
/// (at your option) any later version.
 
12
///
 
13
/// Rheolef is distributed in the hope that it will be useful,
 
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
/// GNU General Public License for more details.
 
17
///
 
18
/// You should have received a copy of the GNU General Public License
 
19
/// along with Rheolef; if not, write to the Free Software
 
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
///
 
22
/// =========================================================================
 
23
 
 
24
#include "rheolef/compiler.h"
 
25
 
 
26
template<class T>
 
27
class heap_allocator {
 
28
public:
 
29
    typedef size_t size_type;
 
30
    heap_allocator(size_type sizeof_bucket = sizeof(T));
 
31
    ~heap_allocator();
 
32
    T* new_bucket();
 
33
    void reinitialize (size_type sizeof_bucket = sizeof(T));
 
34
protected:
 
35
    void clear ();
 
36
    std::list<std::vector<char> > _heap;
 
37
    size_type _heap_block_size;
 
38
    size_type _heap_block_last_free;
 
39
    size_type _sizeof_bucket;
 
40
    size_type _counter;
 
41
    static const size_type _heap_block_size_init = 10;
 
42
};
 
43
template<class T>
 
44
inline
 
45
heap_allocator<T>::heap_allocator(size_type sz)
 
46
  : _heap(),
 
47
    _heap_block_size(_heap_block_size_init),
 
48
    _heap_block_last_free(0),
 
49
    _sizeof_bucket(sz),
 
50
    _counter(0)
 
51
{
 
52
    _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
 
53
}
 
54
template<class T>
 
55
inline
 
56
void
 
57
heap_allocator<T>::reinitialize (size_type sz)
 
58
{
 
59
    clear();
 
60
    _heap_block_size = heap_allocator<T>::_heap_block_size_init;
 
61
    _heap_block_last_free = 0;
 
62
    _sizeof_bucket = sz;
 
63
    _counter = 0;
 
64
    _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
 
65
}
 
66
template<class T>
 
67
inline
 
68
T*
 
69
heap_allocator<T>::new_bucket()
 
70
{
 
71
    if (_heap_block_last_free == _heap_block_size) {
 
72
      _heap_block_size *= 2;
 
73
      _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
 
74
      _heap_block_last_free = 0;
 
75
    }
 
76
    std::vector<char>& block = *(_heap.begin());
 
77
    char* p  = &block [_heap_block_last_free*_sizeof_bucket];
 
78
    _heap_block_last_free++;
 
79
    new ((void*) p) T(); // call default T constructor at p
 
80
    _counter++;
 
81
    return (T*)p;
 
82
}
 
83
template<class T>
 
84
inline
 
85
heap_allocator<T>::~heap_allocator()
 
86
{
 
87
    clear();
 
88
}
 
89
template<class T>
 
90
void
 
91
heap_allocator<T>::clear()
 
92
{
 
93
    size_type n = _heap_block_size_init;
 
94
    for (std::list<std::vector<char> >::reverse_iterator i = _heap.rbegin();
 
95
        _counter != 0 && i != _heap.rend(); i++, n *= 2) {
 
96
        std::vector<char>& block = *i;
 
97
        char* p = &(block[0]);
 
98
        for (size_type c = 0; _counter != 0 && c < n; c++, p += _sizeof_bucket) {
 
99
              ((T*)p)->~T();
 
100
              _counter--;
 
101
        }
 
102
    }
 
103
    _heap.erase(_heap.begin(), _heap.end());
 
104
}
 
105
#endif // _RHEO_HEAP_ALLOCATOR_H