aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/mem.hh
blob: 7ccbd6d72fd51d7ba8ae5b23962949d4d4184b01 (plain)
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
#ifndef MEM_HH
#define MEM_HH

#include <cstdlib>
#include <vector>

using namespace std;

// A chunk of memory, possibly shared between some clients
template<typename T>
class mem
{
  T * storage_;
  size_t nelems_;
  size_t vectorlength_;
  bool owns_storage_;
  
  vector<bool> clients_;
  size_t num_clients_;
  
public:
  mem (size_t vectorlength, size_t nelems, T * memptr = NULL);
  ~mem ();
  
  T * storage (size_t vectorindex) const
  {
    assert (vectorindex < vectorlength_);
    assert (clients_.AT(vectorindex));
    return & storage_ [vectorindex * nelems_];
  }
  
  void register_client (size_t vectorindex);
  void unregister_client (size_t vectorindex);
  bool has_clients () const;
};

#endif  // ifndef MEM_HH