COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MemoryPool.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Sequential and Parallel Sparse Matrix Multiplication Library /
3 / version 2.3 --------------------------------------------------/
4 / date: 01/18/2009 ---------------------------------------------/
5 / description: Simple memory pool implementation ---------------/
6 / author: Aydin Buluc (aydin@cs.ucsb.edu) ----------------------/
7 / This can be improved by keeping a second index that keeps ----/
8 / track of order in terms of size, so that finding an available /
9 / sized chunk is optimal and fast ------------------------------/
10 \****************************************************************/
11 
12 #ifndef _MEMORY_POOL_H
13 #define _MEMORY_POOL_H
14 
15 #include <iostream>
16 #include <list>
17 #include <new> // For "placement new" (classes using this memory pool may need it)
18 #include <fstream>
19 
20 using namespace std;
21 
24 class Memory
25 {
26  public:
27  Memory(char * m_beg, size_t m_size): begin(m_beg),size(m_size)
28  {};
29 
30  char * begaddr() { return begin; }
31  char * endaddr() { return begin + size; }
32 
33  bool operator < (const Memory & rhs) const
34  { return (begin < rhs.begin); }
35  bool operator == (const Memory & rhs) const
36  { return (begin == rhs.begin); }
37 
38  char * begin;
39  size_t size;
40 };
41 
42 
49 {
50 public:
51  MemoryPool(void * m_beg, size_t m_size);
52 
53  void * alloc(size_t size);
54  void dealloc (void * base, size_t size);
55 
56  friend ofstream& operator<< (ofstream& outfile, const MemoryPool & mpool);
57 
58 private:
59  // std::list is a doubly linked list (i.e., a Sequence that supports both forward and backward traversal)
60  list<Memory> freelist;
61  char * initbeg;
62  char * initend;
63 };
64 
65 
66 #endif