Wolframe, 0.0.3

allocators.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 #ifndef _UTILS_ALLOCATORS_HPP_INCLUDED
36 #define _UTILS_ALLOCATORS_HPP_INCLUDED
37 #include <cstddef>
38 #include <stdexcept>
39 #include <new>
40 
41 namespace _Wolframe {
42 namespace utils {
43 
47 {
48 public:
51  void* alloc( std::size_t nofBytes);
52 
53 private:
54  class MemChunk;
55  MemChunk* m_chunk;
56 };
57 
58 
62 {
63 public:
66 
67  std::size_t alloc( std::size_t nofBytes);
68  const void* base() const {return m_ar;}
69  void* base() {return m_ar;}
70  std::size_t size() const {return m_pos;}
71 
72 private:
73  enum {InitBlockSize=(1<<14)};
74  char* m_ar;
75  std::size_t m_size;
76  std::size_t m_pos;
77 };
78 
79 
82 template <typename Type>
84 {
85  const Type* base() const
86  {
87  return (const Type*)ArrayDoublingAllocator::base();
88  }
89 
90  Type* base()
91  {
92  return (Type*)ArrayDoublingAllocator::base();
93  }
94 
95  const Type& operator[]( std::size_t idx) const
96  {
97  if (idx > size()/sizeof(Type))
98  {
99  throw std::logic_error( "Array bounds access");
100  }
101  return ((const Type*)ArrayDoublingAllocator::base())[ idx];
102  }
103 
104  Type& operator[]( std::size_t idx)
105  {
106  if (idx > size()/sizeof(Type))
107  {
108  throw std::logic_error( "Array bounds access");
109  }
110  return ((Type*)ArrayDoublingAllocator::base())[ idx];
111  }
112 
113  std::size_t alloc( unsigned int nof)
114  {
115  std::size_t mm = nof * sizeof(Type);
116  if (mm < nof) throw std::bad_alloc();
117  std::size_t idx = ArrayDoublingAllocator::alloc( mm);
118  return idx / sizeof(Type);
119  }
120 };
121 
122 }}//namespace
123 #endif
124 
Type & operator[](std::size_t idx)
Definition: allocators.hpp:104
const void * base() const
Definition: allocators.hpp:68
Interface for an allocator implemented with the strategy of array doubling.
Definition: allocators.hpp:61
std::size_t m_pos
Definition: allocators.hpp:76
MemChunk * m_chunk
Definition: allocators.hpp:54
Interface for an allocator for small chunks without a free. Memory is freed in the destructor of the ...
Definition: allocators.hpp:46
Interface for an array doubling allocator that allocates only one fixed size type of element...
Definition: allocators.hpp:83
void * base()
Definition: allocators.hpp:69
std::size_t size() const
Definition: allocators.hpp:70
std::size_t m_size
Definition: allocators.hpp:75
void * alloc(std::size_t nofBytes)
const Type & operator[](std::size_t idx) const
Definition: allocators.hpp:95
std::size_t alloc(unsigned int nof)
Definition: allocators.hpp:113
char * m_ar
Definition: allocators.hpp:74
const Type * base() const
Definition: allocators.hpp:85
std::size_t alloc(std::size_t nofBytes)
Type * base()
Definition: allocators.hpp:90