COMBINATORIAL_BLAS  1.6
MemoryPool.cpp
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 6/15/2017 ---------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc --------------------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2017, The Regents of the University of California
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 
30 #include "CombBLAS/MemoryPool.h"
31 
32 using namespace std;
33 
34 namespace combblas {
35 
36 MemoryPool::MemoryPool(void * m_beg, size_t m_size):initbeg((char*)m_beg), initend(((char*)m_beg)+m_size)
37 {
38  Memory m((char*) m_beg, m_size);
39  freelist.push_back(m);
40 }
41 
42 void * MemoryPool::alloc(size_t size)
43 {
44  for(list<Memory>::iterator iter = freelist.begin(); iter != freelist.end(); ++iter)
45  {
46  if ((*iter).size > size) // return the first 'big enough' chunk of memory
47  {
48  char * free = (*iter).begin;
49  (*iter).begin += size; // modify the beginning of the remaining chunk
50  (*iter).size -= size; // modify the size of the remaning chunk
51 
52  return (void *) free; // return the memory
53  }
54  }
55  cout << "No pinned memory available" << endl;
56  return NULL;
57 }
58 
59 
60 void MemoryPool::dealloc(void * base, size_t size)
61 {
62  if( ((char*) base) >= initbeg && (((char*)base) + size) < initend)
63  {
64  list<Memory>::iterator titr = freelist.begin(); // trailing iterator
65  list<Memory>::iterator litr = freelist.begin(); // leading iterator
66  ++litr;
67 
68  if( (char*)base < titr->begaddr()) // if we're inserting to the front of the list
69  {
70  if(titr->begaddr() == ((char*)base) + size)
71  {
72  titr->begin = (char*)base;
73  titr->size += size;
74  }
75  else
76  {
77  Memory m((char*) base, size);
78  freelist.insert(titr, m);
79  }
80  }
81  else if( litr == freelist.end() ) // if we're inserting to the end of a 'single element list'
82  {
83  if(titr->endaddr() == (char*)base)
84  {
85  titr->size += size;
86  }
87  else
88  {
89  Memory m((char*) base, size);
90  freelist.insert(litr, m);
91  }
92  }
93  else // not a single element list, nor we're inserting to the front
94  {
95  // loop until you find the right spot
96  while( litr != freelist.end() && litr->begaddr() < (char*)base)
97  { ++litr; ++titr; }
98 
102  if(titr->endaddr() == (char*)base)
103  {
105  if( litr == freelist.end() || litr->begaddr() != ((char*)base) + size)
106  {
107  titr->size += size;
108  }
109  else
110  {
111  titr->size += (size + litr->size); // modify the chunk pointed by trailing iterator
112  freelist.erase(litr); // delete the chunk pointed by the leading iterator
113  }
114  }
115  else
116  {
117  if( litr == freelist.end() || litr->begaddr() != ((char*)base) + size)
118  {
119  Memory m((char*) base, size);
120 
122  freelist.insert(litr, m);
123  }
124  else
125  {
126  litr->begin = (char*)base;
127  litr->size += size;
128  }
129  }
130  }
131  }
132  else
133  {
134  cerr << "Memory starting at " << base << " and ending at "
135  << (void*) ((char*) base + size) << " is out of pool bounds, cannot dealloc()" << endl;
136  }
137 }
138 
140 ofstream& operator<< (ofstream& outfile, const MemoryPool & mpool)
141 {
142  int i = 0;
143  for(list<Memory>::const_iterator iter = mpool.freelist.begin(); iter != mpool.freelist.end(); ++iter, ++i)
144  {
145  outfile << "Chunk " << i << " of size: " << (*iter).size << " starts:" << (void*)(*iter).begin
146  << " and ends: " << (void*) ((*iter).begin + (*iter).size) << endl ;
147  }
148  return outfile;
149 }
150 
151 }
int size
void dealloc(void *base, size_t size)
Definition: MemoryPool.cpp:60
void * alloc(size_t size)
Definition: MemoryPool.cpp:42
friend std::ofstream & operator<<(std::ofstream &outfile, const MemoryPool &mpool)
Definition: CCGrid.h:4