Pyrogenesis  trunk
headerless.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * (header-less) pool-based heap allocator
25  */
26 
27 #ifndef INCLUDED_ALLOCATORS_HEADERLESS
28 #define INCLUDED_ALLOCATORS_HEADERLESS
29 
30 /**
31  * (header-less) pool-based heap allocator
32  * provides Allocate and Deallocate without requiring in-band headers;
33  * this is useful when allocating page-aligned I/O buffers
34  * (headers would waste an entire page per buffer)
35  *
36  * policy:
37  * - allocation: first exhaust the freelist, then allocate more
38  * - freelist: address-ordered good fit, always split blocks
39  * - coalescing: immediate
40  * mechanism:
41  * - coalescing: boundary tags in freed memory with distinct bit patterns
42  * - freelist: segregated range lists of power-of-two size classes
43  *
44  * note: this module basically implements a (rather complex) freelist and
45  * could be made independent of the Pool allocation scheme. however, reading
46  * neighboring boundary tags may cause segmentation violations; knowing the
47  * bounds of valid committed memory (i.e. Pool extents) avoids this.
48  **/
50 {
51 public:
52  // allocators must 'naturally' align pointers, i.e. ensure they are
53  // multiples of the largest native type (currently __m128).
54  // since there are no headers, we can guarantee alignment by
55  // requiring sizes to be multiples of allocationAlignment.
56  static const size_t allocationAlignment = 16;
57 
58  // allocations must be large enough to hold our boundary tags
59  // when freed. (see rationale above BoundaryTagManager)
60  static const size_t minAllocationSize = 128;
61 
62  /**
63  * @param poolSize maximum amount of memory that can be allocated.
64  * this much virtual address space is reserved up-front (see Pool).
65  **/
66  HeaderlessAllocator(size_t poolSize);
67 
68  /**
69  * restore the original state (as if newly constructed).
70  * this includes reclaiming all extant allocations.
71  **/
72  void Reset();
73 
74  /**
75  * @param size [bytes] (= minAllocationSize + i*allocationAlignment).
76  * (this allocator is designed for requests on the order of several KiB)
77  * @return allocated memory or 0 if the pool is too fragmented or full.
78  **/
79  NOTHROW_DECLARE void* Allocate(size_t size);
80 
81  /**
82  * deallocate memory.
83  * @param p must be exactly as returned by Allocate (in particular,
84  * evenly divisible by allocationAlignment)
85  * @param size must be exactly as specified to Allocate.
86  **/
87  void Deallocate(void* p, size_t size);
88 
89  /**
90  * perform sanity checks; ensure allocator state is consistent.
91  **/
92  void Validate() const;
93 
94 private:
95  class Impl;
96  shared_ptr<Impl> impl;
97 };
98 
99 #endif // #ifndef INCLUDED_ALLOCATORS_HEADERLESS
shared_ptr< Impl > impl
Definition: headerless.h:95
static const size_t allocationAlignment
Definition: headerless.h:56
void Reset()
restore the original state (as if newly constructed).
Definition: headerless.cpp:755
NOTHROW_DECLARE void * Allocate(size_t size)
Definition: headerless.cpp:760
#define NOTHROW_DECLARE
indicate a function will not throw any synchronous exceptions, thus hopefully generating smaller and ...
Definition: code_annotation.h:85
static const size_t minAllocationSize
Definition: headerless.h:60
(header-less) pool-based heap allocator provides Allocate and Deallocate without requiring in-band he...
Definition: headerless.h:49
void Validate() const
perform sanity checks; ensure allocator state is consistent.
Definition: headerless.cpp:770
void Deallocate(void *p, size_t size)
deallocate memory.
Definition: headerless.cpp:765
Definition: headerless.cpp:607
HeaderlessAllocator(size_t poolSize)
Definition: headerless.cpp:750