1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
8 This file is derived from the FSB Allocator by Juha Nieminen. The
9 underlying method is unchanged, but the class has been refactored
10 to permit multiple separate allocators (e.g. one per thread)
11 rather than use a single global one (and to fit house style).
13 Copyright (c) 2008 Juha Nieminen
15 Permission is hereby granted, free of charge, to any person obtaining a copy
16 of this software and associated documentation files (the "Software"), to deal
17 in the Software without restriction, including without limitation the rights
18 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 copies of the Software, and to permit persons to whom the Software is
20 furnished to do so, subject to the following conditions:
22 The above copyright notice and this permission notice shall be included in
23 all copies or substantial portions of the Software.
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #ifndef _BLOCK_ALLOCATOR_H_
35 #define _BLOCK_ALLOCATOR_H_
40 * BlockAllocator is a simple allocator for fixed-size (usually small)
41 * chunks of memory. The size of an element is specified in the
42 * BlockAllocator constructor, and the functions allocate() and
43 * deallocate() are used to obtain and release a single element at a
46 * BlockAllocator may be an appropriate class to use in situations
47 * involving a very large number of allocations and deallocations of
48 * simple, identical objects across multiple threads (a hard situation
49 * for a generic system malloc implementation to handle well). Retain
50 * one BlockAllocator per thread (the class itself is not
51 * thread-safe), and ensure that each thread uses its own allocator
54 * BlockAllocator is based on Juha Nieminen's more general
60 typedef std::size_t data_t;
62 BlockAllocator(int elementSize) : m_sz(elementSize) { }
67 if (m_freelist.empty()) {
68 m_freelist.push_back(m_blocks.data.size());
69 m_blocks.data.push_back(Block(this));
72 const data_t index = m_freelist.back();
73 Block &block = m_blocks.data[index];
74 void *retval = block.allocate(index);
75 if (block.isFull()) m_freelist.pop_back();
85 data_t *unitPtr = (data_t *)ptr;
86 const data_t blockIndex = unitPtr[elementSizeInDataUnits()];
87 Block& block = m_blocks.data[blockIndex];
89 if (block.isFull()) m_freelist.push_back(blockIndex);
90 block.deallocate(unitPtr);
94 inline data_t elementsPerBlock() const {
97 inline data_t dataSize() const {
98 return sizeof(data_t);
100 inline data_t elementSizeInDataUnits() const {
101 return (m_sz + (dataSize() - 1)) / dataSize();
103 inline data_t unitSizeInDataUnits() const {
104 return elementSizeInDataUnits() + 1;
106 inline data_t blockSizeInDataUnits() const {
107 return elementsPerBlock() * unitSizeInDataUnits();
113 Block(BlockAllocator *a) :
116 m_firstFreeUnit(data_t(-1)),
125 bool isFull() const {
126 return m_allocated == m_a->elementsPerBlock();
132 m_firstFreeUnit = data_t(-1);
135 void *allocate(data_t index) {
137 if (m_firstFreeUnit == data_t(-1)) {
140 m_block = new data_t[m_a->blockSizeInDataUnits()];
144 data_t *retval = m_block + m_end;
145 m_end += m_a->unitSizeInDataUnits();
146 retval[m_a->elementSizeInDataUnits()] = index;
152 data_t *retval = m_block + m_firstFreeUnit;
153 m_firstFreeUnit = *retval;
159 void deallocate(data_t *ptr) {
161 *ptr = m_firstFreeUnit;
162 m_firstFreeUnit = ptr - m_block;
164 if (--m_allocated == 0) clear();
168 const BlockAllocator *m_a;
170 data_t m_firstFreeUnit;
177 std::vector<Block> data;
186 std::vector<data_t> m_freelist;