/usr/include/qm-dsp/thread/BlockAllocator.h is in libqm-dsp-dev 1.7.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
QM DSP Library
Centre for Digital Music, Queen Mary, University of London.
This file is derived from the FSB Allocator by Juha Nieminen. The
underlying method is unchanged, but the class has been refactored
to permit multiple separate allocators (e.g. one per thread)
rather than use a single global one (and to fit house style).
Copyright (c) 2008 Juha Nieminen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _BLOCK_ALLOCATOR_H_
#define _BLOCK_ALLOCATOR_H_
#include <cstdlib>
/**
* BlockAllocator is a simple allocator for fixed-size (usually small)
* chunks of memory. The size of an element is specified in the
* BlockAllocator constructor, and the functions allocate() and
* deallocate() are used to obtain and release a single element at a
* time.
*
* BlockAllocator may be an appropriate class to use in situations
* involving a very large number of allocations and deallocations of
* simple, identical objects across multiple threads (a hard situation
* for a generic system malloc implementation to handle well). Retain
* one BlockAllocator per thread (the class itself is not
* thread-safe), and ensure that each thread uses its own allocator
* exclusively.
*
* BlockAllocator is based on Juha Nieminen's more general
* FSBAllocator.
*/
class BlockAllocator
{
public:
typedef std::size_t data_t;
BlockAllocator(int elementSize) : m_sz(elementSize) { }
void *
allocate()
{
if (m_freelist.empty()) {
m_freelist.push_back(m_blocks.data.size());
m_blocks.data.push_back(Block(this));
}
const data_t index = m_freelist.back();
Block &block = m_blocks.data[index];
void *retval = block.allocate(index);
if (block.isFull()) m_freelist.pop_back();
return retval;
}
void
deallocate(void *ptr)
{
if (!ptr) return;
data_t *unitPtr = (data_t *)ptr;
const data_t blockIndex = unitPtr[elementSizeInDataUnits()];
Block& block = m_blocks.data[blockIndex];
if (block.isFull()) m_freelist.push_back(blockIndex);
block.deallocate(unitPtr);
}
private:
inline data_t elementsPerBlock() const {
return 512;
}
inline data_t dataSize() const {
return sizeof(data_t);
}
inline data_t elementSizeInDataUnits() const {
return (m_sz + (dataSize() - 1)) / dataSize();
}
inline data_t unitSizeInDataUnits() const {
return elementSizeInDataUnits() + 1;
}
inline data_t blockSizeInDataUnits() const {
return elementsPerBlock() * unitSizeInDataUnits();
}
class Block
{
public:
Block(BlockAllocator *a) :
m_a(a),
m_block(0),
m_firstFreeUnit(data_t(-1)),
m_allocated(0),
m_end(0)
{}
~Block() {
delete[] m_block;
}
bool isFull() const {
return m_allocated == m_a->elementsPerBlock();
}
void clear() {
delete[] m_block;
m_block = 0;
m_firstFreeUnit = data_t(-1);
}
void *allocate(data_t index) {
if (m_firstFreeUnit == data_t(-1)) {
if (!m_block) {
m_block = new data_t[m_a->blockSizeInDataUnits()];
m_end = 0;
}
data_t *retval = m_block + m_end;
m_end += m_a->unitSizeInDataUnits();
retval[m_a->elementSizeInDataUnits()] = index;
++m_allocated;
return retval;
} else {
data_t *retval = m_block + m_firstFreeUnit;
m_firstFreeUnit = *retval;
++m_allocated;
return retval;
}
}
void deallocate(data_t *ptr) {
*ptr = m_firstFreeUnit;
m_firstFreeUnit = ptr - m_block;
if (--m_allocated == 0) clear();
}
private:
const BlockAllocator *m_a;
data_t *m_block;
data_t m_firstFreeUnit;
data_t m_allocated;
data_t m_end;
};
struct Blocks
{
std::vector<Block> data;
Blocks() {
data.reserve(1024);
}
};
const int m_sz;
Blocks m_blocks;
std::vector<data_t> m_freelist;
};
#endif
|