Developer documentation

system_linear_allocator.h
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): J.P. Pavone, GALAAD, INRIA
4  * $Id: linear_allocator.hpp,v 1.1 2005/07/11 10:03:56 jppavone Exp $
5  ********************************************************************/
6 #ifndef realroot_SOLVE_SBDSLV_MEMORY_LINEAR_ALLOCATOR_HPP
7 #define realroot_SOLVE_SBDSLV_MEMORY_LINEAR_ALLOCATOR_HPP
8 //--------------------------------------------------------------------
9 #include <algorithm>
10 #include <assert.h>
11 //--------------------------------------------------------------------
12 namespace mmx {
13 //--------------------------------------------------------------------
14 namespace memory
15 {
16  /* l'allocateur linéaire pourrait hérité de std::vector mais std::vector ne fournit pas de méthodes pour fixer sa
17  capacité (méthode adjust à faire) */
18  template<class T, bool automatic = false>
20  {
21  typedef T data_t;
22  typedef unsigned sz_t;
23  template<bool v> struct automatic_dispatch {};
24  data_t * m_chnk;
25  sz_t m_csz;
26  sz_t m_nalloc;
27 
28  void _chnkresize_ ( sz_t nsz )
29  {
30  if ( m_csz )
31  {
32  data_t * _chnk_ = new data_t[ m_csz = nsz ];
33  assert(_chnk_);
34  std::copy(m_chnk,m_chnk+m_nalloc,_chnk_);
35  std::swap(_chnk_,m_chnk);
36  delete[] _chnk_;
37  }
38  else
39  {
40  m_chnk = new T[ m_csz = nsz ];
41  assert(m_chnk);
42  };
43  };
44 
45  static const sz_t block = 1024;
46  public:
47  sz_t memuse() { return sizeof(T)*m_csz; };
48  void reserve( sz_t nsz )
49  {
50  if ( nsz < m_csz ) return;
51  this->_chunkresize_(nsz);
52  };
53 
54  inline sz_t alloc( sz_t sz )
55  {
56  assert(sz);
57  if ( m_nalloc + sz > m_csz )
58  {
59  sz_t ncsz = std::max(m_csz,(sz_t)1);
60  while ( ncsz < m_nalloc + sz ) ncsz *= 2;
61  _chnkresize_( ncsz );
62  };
63  m_nalloc = m_nalloc + sz;
64  return m_nalloc - sz;
65  };
66 
67  inline void free( sz_t sz ) {
68  assert(m_nalloc >= sz);
69  m_nalloc -= sz;
70  };
71 
72  inline void dec ( sz_t sz ) { free(sz); };
73 
74  void free() { m_nalloc = m_csz = 0; if ( m_chnk ) delete[] m_chnk; m_chnk = 0; };
75 
76  inline T * data( sz_t address, const automatic_dispatch<false>& ) const
77  {
78  if ( address >= m_nalloc ) std::cout << address << "/" << m_nalloc << std::endl;
79  assert(address<m_nalloc);
80  return m_chnk + address;
81  };
82 
83  inline T * data( sz_t address, const automatic_dispatch<true> & )
84  {
85  if ( address < m_nalloc ) return m_chnk+address;
86  alloc( address + 1 - m_nalloc );
87  return m_chnk + address;
88  };
89  inline T * data( sz_t address ) const { return data(address,automatic_dispatch<automatic>()); };
90  linear_allocator( unsigned n = 0 ): m_chnk(0), m_csz(0), m_nalloc(0)
91  {
92  _chnkresize_( (n?n:std::max((sz_t)(block/sizeof(T)),(sz_t)1)) );
93  };
95  };
96 
97 };
98 //--------------------------------------------------------------------
99 } //namespace mmx
100 /********************************************************************/
101 #endif //
unsigned sz_t
Definition: system_linear_allocator.h:22
linear_allocator(unsigned n=0)
Definition: system_linear_allocator.h:90
static const sz_t block
Definition: system_linear_allocator.h:45
sz_t m_nalloc
Definition: system_linear_allocator.h:26
T data_t
Definition: system_linear_allocator.h:21
MP swap(const MP &P, int var_i, int var_j)
Definition: sparse_monomials.hpp:988
T * data(sz_t address, const automatic_dispatch< true > &)
Definition: system_linear_allocator.h:83
void _chnkresize_(sz_t nsz)
Definition: system_linear_allocator.h:28
void free()
Definition: system_linear_allocator.h:74
Definition: system_linear_allocator.h:23
Definition: system_linear_allocator.h:19
TMPL void copy(Polynomial &r, const Polynomial &a)
Copy of a in r.
Definition: sparse_monomials.hpp:613
sz_t alloc(sz_t sz)
Definition: system_linear_allocator.h:54
T * data(sz_t address, const automatic_dispatch< false > &) const
Definition: system_linear_allocator.h:76
void free(sz_t sz)
Definition: system_linear_allocator.h:67
void dec(sz_t sz)
Definition: system_linear_allocator.h:72
sz_t memuse()
Definition: system_linear_allocator.h:47
void reserve(sz_t nsz)
Definition: system_linear_allocator.h:48
T * data(sz_t address) const
Definition: system_linear_allocator.h:89
sz_t m_csz
Definition: system_linear_allocator.h:25
~linear_allocator()
Definition: system_linear_allocator.h:94
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
Definition: array.hpp:12
data_t * m_chnk
Definition: system_linear_allocator.h:24
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home