Go to the documentation of this file.
15 #include <iostream>
16 #include "matop.hxx"
18 #include <iomanip>
19 #include <stdlib.h>
20 #endif
23 namespace CH_Matrix_Classes {
46  class Memarray{
47  private:
50  class Entry{
51  public:
53  char *addr;
54  long size;
55  int index;
58  Entry(){next=0; addr=0; size=0;}
60  ~Entry(){delete[] addr;}
61  };
63  long max_entries;
64  long max_sizes;
66  unsigned long addr_mask;
67  unsigned long in_use;
68  unsigned long memarray_users;
76  int size_index(long size);
78  long index_size(int index);
80  int addr_index(const char *addr);
82  int get_more_entries();
84  public:
86  Memarray(long number_of_entries,int number_of_sizes,int address_bits);
88  ~Memarray();
90  unsigned long get_in_use() const {return in_use;}
92  unsigned long get_memarray_users() const {return memarray_users;}
94  unsigned long increment_memarray_users() {return ++memarray_users;}
96  unsigned long decrement_memarray_users() {return --memarray_users;}
99  long get(long size,char *& addr);
101  long get(long size,double *& addr)
102  {return get(size*long(sizeof(double)),(char*&)addr)/long(sizeof(double));}
104  long get(long size,float *& addr)
105  {return get(size*long(sizeof(float)),(char*&)addr)/long(sizeof(float));}
107  long get(long size,long *& addr)
108  {return get(size*long(sizeof(long)),(char*&)addr)/long(sizeof(long));}
110  long get(long size,int *& addr)
111  {return get(size*long(sizeof(int)),(char*&)addr)/long(sizeof(int));}
113  int free(void *addr);
114  };
118  {
119  protected:
122  public:
125  {
126  if (memarray==0) {
127  memarray=new Memarray(1L,60,10);
128  }
129  memarray->increment_memarray_users();
130  }
133  virtual ~Memarrayuser()
134  {
136  if (memarray==0) {
137  MEmessage(MatrixError(ME_unspec,"*** Error: Memarrayuser::~Memarrayuser(): memory management killed prematurely",MTglobalfun));
138  }
139 #endif
140  if (memarray->decrement_memarray_users()==0) {
141  delete memarray;
142  memarray=0;
143  }
144  }
145  };
148  template<class T>
149  inline int mem_provide(
150  Memarray& memarray,
151  long provide,
152  long in_use,
153  long& avail,
154  T*& store
155  )
156  {
157  if (provide<avail) return 0;
158  T* tmpstore;
159  long tmpavail=memarray.get(((provide>2*avail)?provide:2*avail)*long(sizeof(T)),(char *&)tmpstore)/long(sizeof(T));
160  if ((tmpstore==0)&&(provide<2*avail)){
161  tmpavail=memarray.get(provide*long(sizeof(T)),(char *&)tmpstore)/long(sizeof(T));
162  }
163  if (tmpstore==0) return 1;
164  long i;
165  for(i=0;i<in_use;i++){
166  tmpstore[i]=store[i];
167  }
168 *)store);
169  store=tmpstore;
170  avail=tmpavail;
171  return 0;
172  }
175  template<class T>
176  inline int mem_provide_init0(
177  Memarray& memarray,
178  long provide,
179  long& avail,
180  T*& store
181  )
182  {
183  if (provide<avail) return 0;
184  T* tmpstore;
185  long tmpavail=memarray.get(((provide>2*avail)?provide:2*avail)*long(sizeof(T)),(char *&)tmpstore)/long(sizeof(T));
186  if ((tmpstore==0)&&(provide<2*avail)){
187  tmpavail=memarray.get(provide*long(sizeof(T)),(char *&)tmpstore)/long(sizeof(T));
188  }
189  if (tmpstore==0) return 1;
190  long i;
191  for(i=0;i<avail;i++){
192  tmpstore[i]=store[i];
193  }
194  for(;i<tmpavail;i++){
195  tmpstore[i]=0;
196  }
197 *)store);
198  store=tmpstore;
199  avail=tmpavail;
200  return 0;
201  }
205 }
207 #endif
holds the information of one allocated block and serves as an item in the singly linked lists ...
Definition: memarray.hxx:50
error arises in a global function
Definition: matop.hxx:1586
long index_size(int index)
compute size of blocks in the free list with this index
int size_index(long size)
compute index for free list to a request of size
int mem_provide(Memarray &memarray, long provide, long in_use, long &avail, T *&store)
provide sufficient memory for an existing array, reallocating and copying the old information upon ne...
Definition: memarray.hxx:149
A simple memory manager for frequent allocation and deallocation of arrays of roughly the same size...
Definition: memarray.hxx:46
Entry first_empty
its next pointer points to the first free CH_Matrix_Classes::Memarray::Entry item, that does not yet hold an allocated block
Definition: memarray.hxx:70
int get_more_entries()
double number of available CH_Matrix_Classes::Memarray::Entry items
unsigned long increment_memarray_users()
announce a new user
Definition: memarray.hxx:94
long size
gives the size of the allocated block
Definition: memarray.hxx:54
static Memarray * memarray
pointer to common memory manager for all Memarrayusers, instantiated in memarray.cxx ...
Definition: memarray.hxx:121
unsigned long memarray_users
number of objects announced as "living" users of this memory manager
Definition: memarray.hxx:68
Entry * entry_store
points to the allocated array of CH_Matrix_Classes::Memarray::Entry items
Definition: memarray.hxx:71
Such an object is generated and passed to MEmessage(), whenever an error occurs. It holds some output...
Definition: matop.hxx:1609
Entry * next
points to the next entry in the list
Definition: memarray.hxx:52
int MEmessage(const MatrixError &)
displays an error message and terminates via abort() or returns 1 in case of warnings.
int free(void *addr)
free the array pointed to by addr (addr must be an address returned by get)
Entry * first_free
points to an array of size max_sizes, its Entry i start the free list for size 2^i ...
Definition: memarray.hxx:72
char * addr
points to the allocated block
Definition: memarray.hxx:53
long max_sizes
current number of lists holding free blocks
Definition: memarray.hxx:64
unsigned long in_use
number of CH_Matrix_Classes::Memarray::Entry items in use (pointing to an allocated block) ...
Definition: memarray.hxx:67
unsigned long get_in_use() const
returns the number of CH_Matrix_Classes::Memarray::Entry items in use
Definition: memarray.hxx:90
Basic types, constants and templates for simple linear algebra routines like BLAS level 1...
Matrix Classes and Linear Algebra. See Matrix Classes (namespace CH_Matrix_Classes) for a quick intro...
Definition: PSCOracle.hxx:20
int addr_index(const char *addr)
compute index into address class to retrieve entry of "addr"
int index
gives the index of the size group of this block
Definition: memarray.hxx:55
long get(long size, char *&addr)
request a character array of at least this size, the address is then stored in addr and the actual si...
unsigned long decrement_memarray_users()
decrement number of announced users
Definition: memarray.hxx:96
unspecified error type, not in the list below
Definition: matop.hxx:1600
Memarray(long number_of_entries, int number_of_sizes, int address_bits)
specify inital number of Entry items, the number of 2^i classes, the number of last bits used in find...
All derived classes share a common Memarray memory manager, which is generated with the first user an...
Definition: memarray.hxx:117
long max_entries
current number of CH_Matrix_Classes::Memarray::Entry items available
Definition: memarray.hxx:63
if memarray is NULL, then a new Memarray is generated. In any case the number of users of the Memarra...
Definition: memarray.hxx:124
virtual ~Memarrayuser()
the number of users is decremented and the Memarray memory manager is destructed, if the number is ze...
Definition: memarray.hxx:133
Entry * first_used
points to an array of size max_addr_entr, its Entry i start the list of entries whose memory is in us...
Definition: memarray.hxx:73
unsigned long get_memarray_users() const
returns the number of announced "living" users of this Memory manager
Definition: memarray.hxx:92
long max_addr_entr
current number of lists hodling occupied blocks
Definition: memarray.hxx:65
int mem_provide_init0(Memarray &memarray, long provide, long &avail, T *&store)
provide sufficient memory for an existing array, reallocating and copying the old information and ini...
Definition: memarray.hxx:176
unsigned long addr_mask
mask to extract last bits of an address as index for freeing
Definition: memarray.hxx:66