PageAllocator.h
1 /*
2  * Copyright (C) 2009-2010 by Bendri Batti
3  * Copyright (C) 2009-2013 by Marc Boris Duerner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef PT_PAGE_ALLOCATOR_H
31 #define PT_PAGE_ALLOCATOR_H
32 
33 #include <Pt/Api.h>
34 #include <Pt/Allocator.h>
35 #include <Pt/NonCopyable.h>
36 #include <cstddef>
37 
38 namespace Pt {
39 
75 class PT_API PageAllocator : public Pt::Allocator
76  , protected NonCopyable
77 {
78  public:
80  class Page
81  {
82  public:
83  Page(Page* nextChunk, std::size_t chunkSize);
84 
85  ~Page();
86 
87  void clear();
88 
89  void* allocate(std::size_t reqSize);
90 
91  Page *nextVariableChunk()
92  { return _nextChunk; }
93 
94  std::size_t spaceAvailable() const
95  { return _chunkSize - _bytesAlreadyAllocated; }
96 
97  std::size_t capacity() const
98  { return _chunkSize; }
99 
100  private:
101  Page* _nextChunk;
102  char* _mem;
103  std::size_t _chunkSize;
104  std::size_t _bytesAlreadyAllocated;
105  };
106 
107  enum { MinChunkSize = 4096 };
108 
109  public:
111  PageAllocator(std::size_t pageSize = MinChunkSize);
112 
114  ~PageAllocator();
115 
117  void clear();
118 
119  // inherit docs
120  void* allocate( std::size_t size );
121 
122  // inherit docs
123  void deallocate( void* p, std::size_t size );
124 
125  private:
127  void expandStorage( std::size_t size );
128 
129  private:
130  Page* _listOfVariableChunk;
131 };
132 
133 } // namespace Pt
134 
135 #endif
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Allocator interface.
Definition: Allocator.h:81
Page based allocator.
Definition: PageAllocator.h:75