LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mempool.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #pragma once
18 #ifndef MEMPOOL_HPP
19 #define MEMPOOL_HPP
20 #include "defs.hpp"
21 #include <list>
22 #include <stdint.h>
23 #include <assert.h>
24 class mempool;
25 namespace __HIDER__{
26  template <typename... Args>
27  struct Impl;
28 
29  template <typename Th, typename... Tl>
30  struct Impl<Th, Tl...>
31  {
32  static std::list<size_t> _create()
33  {
34  auto tmp = Impl<Tl...>::_create();
35  tmp.push_front(sizeof(Th));
36  return tmp;
37  }
38  };
39 
40  template <>
41  struct Impl<>
42  {
43  static std::list<size_t> _create()
44  {
45  return std::list<size_t>();
46  }
47  };
48 
49  struct ptr_make
50  {
51 
52  };
53  template <typename... Args>
54  struct lmake;
55 
56  template <typename Th, typename... Tl>
57  struct lmake<Th, Tl...>
58  {
59  static void _create(std::list<void**>& ival, Th* restr * hval, Tl* restr *... lvals)
60  {
61  ival.push_back((void**)hval);
62  lmake<Tl...>::_create(ival, lvals...);
63  }
64  };
65 
66  template <>
67  struct lmake<>
68  {
69  static void _create(std::list<void**>& ival){}
70  };
71 
72 }
74 
80 class mempool{
81  static void _create(size_t al, char* v, char* en,
82  std::list<size_t> sizes, std::list<size_t> vsizes, std::list<char* >& ptrl);
86  char* izard;//charizard lol.
87  size_t total_bytes;
88  std::list<size_t> csizes, vsizes;
89  std::list<void**> cptrs;
90  template<class ...Tl>
91  void make_this(size_t mal, size_t dim, Tl* restr *... args);
92  template<class ...Tl>
93  void make_this(size_t dim, Tl* restr *... args){
94  make_this(32, dim, args...);
95  }
96 
97  public:
99 
104  template<class ...Tl>
105  void create(size_t dim, Tl* restr *... args){
106  clear();
107  make_this(32, dim, args...);
108  }
110 
116  template<class ...Tl>
117  void create(size_t mal, size_t dim, Tl* restr *... args){
118  clear();
119  make_this(mal, dim, args...);
120  }
124  template<class ...Tl>
125  void add(size_t mal, size_t dim, Tl* restr * ... args);
126  template<class ...Tl>
127  void add(size_t dim, Tl* restr * ... args){
128  add(32, dim, args...);
129  }
131 
137  void set_dim(size_t num, size_t mal=32);
139  void clear();
140  mempool();
141  ~mempool();
142 };
143 
152 template<class ...Tl>
153 void mempool::make_this(size_t mal, size_t dim, Tl* restr *... args){
154  assert(mal >= 16);
155  csizes.clear();
156  cptrs.clear();
157  auto val = __HIDER__::Impl<Tl...>::_create();
158  //for now, just allocate (n+1)*n_align + size_total
159  total_bytes = (val.size() +1)*mal;
160  std::list<char*> held_ptrs;
161  for(auto s : val){
162  csizes.push_back(dim);
163  vsizes.push_back(s);
164  total_bytes += dim * s;
165  }
166  bool is_done=false;
167  while(!is_done){
168  is_done=true;
169  izard= new char[total_bytes];
170  try{
172  csizes, vsizes, held_ptrs);
173  }
174  catch(std::exception& e){
175  std::cout << "Needs more memory" << std::endl;
176  is_done=false;
177  delete [] izard;
178  total_bytes *= 2;
179  }
180  }
182  auto h = held_ptrs.begin();
183  auto c = cptrs.begin();
184  for(; h != held_ptrs.end() && c != cptrs.end();){
185  *(*c) = *h;
186  h++;
187  c++;
188  }
189 }
190 
202 template<class ...Tl>
203 void mempool::add(size_t mal, size_t dim, Tl* restr * ... args){
204  assert(mal >= 16);
205  auto val = __HIDER__::Impl<Tl...>::_create();
206  //for now, just allocate (n+1)*n_align + size_total
207  size_t new_bytes = (val.size() +1)*mal;
208  std::list<char*> held_ptrs;
209  for(auto s : val){
210  csizes.push_back(dim);
211  vsizes.push_back(s);
212  new_bytes += dim * s;
213  }
214  total_bytes+=new_bytes;
215  bool is_done=false;
216  if(izard){
217  delete[] izard;
218  }
219  while(!is_done){
220  //try setting aligned pointers in currently allocated memory
221  is_done=true;
222  izard= new char[total_bytes];
223  try{
225  csizes, vsizes, held_ptrs);
226  }
227  catch(std::exception& e){
228  is_done=false;
229  delete [] izard;
230  total_bytes *= 2;
231  }
232  }
234  auto h = held_ptrs.begin();
235  auto c = cptrs.begin();
236  for(; h != held_ptrs.end() && c != cptrs.end();){
237  *(*c) = *h;
238  h++;
239  c++;
240  }
241 }
242 #endif