Developer documentation

system_nstack.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: nstack.hpp,v 1.1 2005/07/11 10:03:56 jppavone Exp $
5  ********************************************************************/
6 #ifndef realroot_SOLVE_MEMORY_NSTACK_HPP
7 #define realroot_SOLVE_MEMORY_NSTACK_HPP
8 //--------------------------------------------------------------------
9 #include <iostream>
10 #include <stdlib.h>
11 //--------------------------------------------------------------------
12 namespace mmx {
13 //--------------------------------------------------------------------
14 namespace memory
15 {
16  /* constructeur T() necessaire */
17  template< typename T >
18  struct nstack
19  {
20  typedef unsigned int sz_t;
21  sz_t m_n, m_sz;
22  T * m_frees, * m_top;
23  inline void * _alloc_( sz_t sz ) { return malloc( sz ); };
24  inline T * alloc() {
25  if ( m_frees ) { T * tmp = m_frees; m_frees = prev(m_frees); prev(tmp) = 0; return tmp; };
26  T* tmp = (T*)_alloc_( sizeof(T)*m_sz );
27  for ( sz_t i = 0; i < m_n; i ++ ) new((void*)(tmp+i)) T; /* initialisation */
28  prev(tmp) = 0;
29  return tmp;
30  };
31  inline T*& prev( T * chnk ) { return *(T**)(chnk+m_n); };
32  public:
33  inline void pop() { T * tmp = prev(m_top); prev(m_top) = m_frees; m_frees = m_top; m_top = tmp; };
34  inline T * top() { return m_top; };
35  inline void push() { T * tmp = alloc(); prev(tmp) = m_top; m_top = tmp; };
36 
37  nstack( unsigned n = 1 )
38  {
39  if ( sizeof(T) > sizeof(T*) ) m_sz = n+1; else m_sz = n+2;
40  m_n = n;
41  m_frees = 0;
42  m_top = 0;
43  };
44 
45  inline unsigned deep()
46  {
47  unsigned rslt = 0;
48  T * tmp = top();
49  if ( ! tmp ) return 0;
50  while ( prev(tmp) ) { rslt ++; tmp = prev(tmp); };
51  return rslt;
52  };
53 
54  inline void popall() { while ( m_top ) pop(); };
55 
57  {
58  popall();
59  while ( m_frees ) {
60  T * tmp = m_frees; m_frees = prev(m_frees);
61  for ( sz_t i = 0; i < m_n; i ++ ) (tmp+i)->~T();
62  free( tmp );
63  };
64  };
65  };
66 };
67 //--------------------------------------------------------------------
68 } //namespace mmx
69 /********************************************************************/
70 #endif //
void popall()
Definition: system_nstack.h:54
void push()
Definition: system_nstack.h:35
T * m_top
Definition: system_nstack.h:22
T * alloc()
Definition: system_nstack.h:24
T * top()
Definition: system_nstack.h:34
T *& prev(T *chnk)
Definition: system_nstack.h:31
Definition: system_nstack.h:18
nstack(unsigned n=1)
Definition: system_nstack.h:37
~nstack()
Definition: system_nstack.h:56
unsigned int sz_t
Definition: system_nstack.h:20
sz_t m_sz
Definition: system_nstack.h:21
void pop()
Definition: system_nstack.h:33
unsigned deep()
Definition: system_nstack.h:45
T * m_frees
Definition: system_nstack.h:22
sz_t m_n
Definition: system_nstack.h:21
void * _alloc_(sz_t sz)
Definition: system_nstack.h:23
Definition: array.hpp:12
Home