Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
small_object_allocator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__SMALL_OBJECT_ALLOCATOR__
34#define __H__SMALL_OBJECT_ALLOCATOR__
35
36#include <cassert>
37#include <vector>
38
43{
44 public:
45 FixedAllocator(std::size_t blockSize, unsigned char numBlocksPerChunk);
46 void* allocate();
47 void deallocate(void* p);
48
49 private:
55 struct Chunk
56 {
58 void init(std::size_t blockSize, unsigned char numBlocks);
59
61 void free();
62
64 void* allocate(std::size_t blockSize);
66
68 void deallocate(void* p, std::size_t blockSize);
69
70 unsigned char* m_pData;
71 unsigned char m_firstAvailableBlock;
72 unsigned char m_numAvailableBlocks;
73 };
74
75 private:
76 inline bool pointer_is_in_chunk(void* p, Chunk* chunk)
77 {
78 return (p >= chunk->m_pData)
79 && (p < chunk->m_pData + m_blockSize * m_numBlocksPerChunk);
80 }
81
82 private:
83 typedef std::vector<Chunk> Chunks;
84
85 private:
86 std::size_t m_blockSize;
87 unsigned char m_numBlocksPerChunk;
92 std::size_t m_numFreeBlocks;
93};
94
96template <std::size_t maxObjSize = 64, std::size_t maxChunkSize = 4096>
98{
99 public:
101 static SmallObjectAllocator& inst();
102
104 void* allocate(std::size_t numBytes);
105
107 void deallocate(void* p, std::size_t size);
108
109 private:
111
112 private:
113 std::vector<FixedAllocator> m_allocators;
114};
115
116
122template <std::size_t maxObjSize = 64, std::size_t maxChunkSize = 4096>
124{
125 public:
126 static void* operator new(std::size_t size);
127 static void operator delete(void* p, std::size_t size);
128 virtual ~SmallObject() {}
129};
130
132// include implementation
134
135#endif
parameterString p
Definition small_object_allocator.h:43
std::size_t m_numFreeBlocks
Definition small_object_allocator.h:92
std::vector< Chunk > Chunks
Definition small_object_allocator.h:83
unsigned char m_numBlocksPerChunk
Definition small_object_allocator.h:87
std::size_t m_blockSize
Definition small_object_allocator.h:86
int m_deallocChunkIndex
Definition small_object_allocator.h:91
int m_emptyChunkIndex
Definition small_object_allocator.h:90
void * allocate()
Definition small_object_allocator.cpp:48
bool pointer_is_in_chunk(void *p, Chunk *chunk)
Definition small_object_allocator.h:76
Chunks m_chunks
Definition small_object_allocator.h:88
Chunk * m_allocChunk
Definition small_object_allocator.h:89
void deallocate(void *p)
Definition small_object_allocator.cpp:78
Definition small_object_allocator.h:98
void deallocate(void *p, std::size_t size)
make sure that size exactly specifies the number of bytes of the object to which p points.
Definition small_object_allocator_impl.h:57
void * allocate(std::size_t numBytes)
if numBytes > maxObjSize, allocate will directly call new.
Definition small_object_allocator_impl.h:47
static SmallObjectAllocator & inst()
returns an instance to this singleton
Definition small_object_allocator_impl.h:39
SmallObjectAllocator()
Definition small_object_allocator_impl.h:68
std::vector< FixedAllocator > m_allocators
Definition small_object_allocator.h:113
Definition small_object_allocator.h:124
virtual ~SmallObject()
Definition small_object_allocator.h:128
virtual void init()
Definition small_object_allocator.h:56
void deallocate(void *p, std::size_t blockSize)
deallocates the given pointer.
Definition small_object_allocator.cpp:178
unsigned char m_firstAvailableBlock
Definition small_object_allocator.h:71
unsigned char m_numAvailableBlocks
Definition small_object_allocator.h:72
unsigned char * m_pData
Definition small_object_allocator.h:70
void free()
call this method instead of a destructor
Definition small_object_allocator.cpp:159