/usr/include/dune/common/poolallocator.hh is in libdune-common-dev 2.2.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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=8 sw=2 sts=2:
// $Id: poolallocator.hh 6785 2012-05-31 22:07:47Z sander $
#ifndef DUNE_COMMON_POOLALLOCATOR_HH
#define DUNE_COMMON_POOLALLOCATOR_HH
/** \file
* \brief An stl-compliant pool allocator
*/
#include"alignment.hh"
#include"static_assert.hh"
#include"lcm.hh"
#include<typeinfo>
#include<iostream>
#include<cassert>
#include<new>
//forward declarations.
// we need to know the test function to declare it friend
template<std::size_t size, typename T>
struct testPoolMain;
namespace Dune
{
template<typename T, std::size_t s>
class Pool;
template<typename T, std::size_t s>
class PoolAllocator;
}
namespace std
{
/*
template<class T, std::size_t S>
inline ostream& operator<<(ostream& os, Dune::Pool<T,S>& pool)
{
os<<"pool="<<&pool<<" allocated_="<<pool.allocated_;
return os;
}
template<class T, std::size_t S>
inline ostream& operator<<(ostream& os, Dune::PoolAllocator<T,S>& pool)
{
os<<pool.memoryPool_<<std::endl;
return os;
}
*/
}
namespace Dune
{
/**
* @file
* This file implements the classes Pool and PoolAllocator providing
* memory allocation for objects in chunks.
* @author Markus Blatt
*/
/**
* @addtogroup Common
*
* @{
*/
/**
* @brief A memory pool of objects.
*
* The memory for the objects is organized in chunks.
* Each chunks is capable of holding a specified number of
* objects. The allocated objects will be properly aligned
* for fast access.
* Deallocated objects are cached for reuse to prevent memory
* fragmentation.
* @warning If the size of the objects allocated is less than the
* size of a pointer memory is wasted.
* @warning Due to aligned issues at the number of bytes of the
* alignment prerequisite (< 4 bytes) are wasted. This effect
* becomes negligible for big sizes of chunkSize.
*
* \tparam T The type that is allocated by us.
* \tparam s The size of a memory chunk in bytes.
*/
template<class T, std::size_t s>
class Pool
{
// make the test function friend
friend struct ::testPoolMain<s,T>;
//friend std::ostream& std::operator<<<>(std::ostream&,Pool<T,s>&);
template< class, std::size_t > friend class PoolAllocator;
private:
/** @brief Reference to next free element. */
struct Reference
{
Reference *next_;
};
public:
/** @brief The type of object we allocate memory for. */
typedef T MemberType;
enum
{
/**
* @brief The size of a union of Reference and MemberType.
*/
unionSize = ((sizeof(MemberType) < sizeof(Reference)) ?
sizeof(Reference) : sizeof(MemberType)),
/**
* @brief Size requirement. At least one object has to
* stored.
*/
size = ((sizeof(MemberType) <= s && sizeof(Reference) <= s)?
s : unionSize),
/**
* @brief The alignment that suits both the MemberType and
* the Reference (i.e. their least common multiple).
*/
alignment = Lcm<AlignmentOf<MemberType>::value,AlignmentOf<Reference>::value>::value,
/**
* @brief The aligned size of the type.
*
* This size is bigger than sizeof of the type and a multiple of
* the alignment requirement.
*/
alignedSize = ((unionSize % alignment == 0) ?
unionSize :
((unionSize / alignment + 1) * alignment)),
/**
* @brief The size of each chunk memory chunk.
*
* Will be adapted to be a multiple of the alignment plus
* an offset to handle the case that the pointer to the memory
* does not satisfy the alignment requirements.
*/
chunkSize = ((size % alignment == 0)?
size : ((size / alignment + 1)* alignment))
+ alignment - 1,
/**
* @brief The number of element each chunk can hold.
*/
elements = ((chunkSize - alignment + 1)/ alignedSize)
};
private:
/** @brief Chunk of memory managed by the pool. */
struct Chunk
{
//friend int testPool<s,T>();
/** @brief The memory we hold. */
char chunk_[chunkSize];
/**
* @brief Adress the first properly aligned
* position in the chunk.
*/
char* memory_;
/** @brief The next element */
Chunk *next_;
/**
* @brief Constructor.
*/
Chunk()
{
// Make sure the alignment is correct!
// long long should be 64bit safe!
unsigned long long lmemory = reinterpret_cast<unsigned long long>(chunk_);
if(lmemory % alignment != 0)
lmemory = (lmemory / alignment + 1)
* alignment;
memory_ = reinterpret_cast<char *>(lmemory);
}
};
public:
/** @brief Constructor. */
inline Pool();
/** @brief Destructor. */
inline ~Pool();
/**
* @brief Get a new or recycled object
* @return A pointer to the object memory.
*/
inline void* allocate();
/**
* @brief Free an object.
* @param o The pointer to memory block of the object.
*/
inline void free(void* o);
/**
* @brief Print elements in pool for debugging.
*/
inline void print(std::ostream& os);
private:
// Prevent Copying!
Pool(const Pool<MemberType,s>&);
void operator=(const Pool<MemberType,s>& pool) const;
/** @brief Grow our pool.*/
inline void grow();
/** @brief The first free element. */
Reference *head_;
/** @brief Our memory chunks. */
Chunk *chunks_;
/* @brief The number of currently allocated elements. */
//size_t allocated_;
};
/**
* @brief An allocator managing a pool of objects for reuse.
*
* This allocator is specifically useful for small data types
* where new and delete are too expensive.
*
* It uses a pool of memory chunks where the objects will be allocated.
* This means that assuming that N objects fit into memory only every N-th
* request for an object will result in memory allocation.
*
* @warning It is not suitable
* for the use in standard containers as it cannot allocate
* arrays of arbitrary size
*
* \tparam T The type that will be allocated.
* \tparam s The number of elements to fit into one memory chunk.
*/
template<class T, std::size_t s>
class PoolAllocator
{
//friend std::ostream& std::operator<<<>(std::ostream&,PoolAllocator<T,s>&);
public:
/**
* @brief Type of the values we construct and allocate.
*/
typedef T value_type;
enum
{
/**
* @brief The number of objects to fit into one memory chunk
* allocated.
*/
size=s*sizeof(value_type)
};
/**
* @brief The pointer type.
*/
typedef T* pointer;
/**
* @brief The constant pointer type.
*/
typedef const T* const_pointer;
/**
* @brief The reference type.
*/
typedef T& reference;
/**
* @brief The constant reference type.
*/
typedef const T& const_reference;
/**
* @brief The size type.
*/
typedef std::size_t size_type;
/**
* @brief The difference_type.
*/
typedef std::ptrdiff_t difference_type;
/**
* @brief Constructor.
*/
inline PoolAllocator();
/**
* @brief Coopy Constructor.
*/
template<typename U, std::size_t u>
inline PoolAllocator(const PoolAllocator<U,u>&)
{}
/**
* @brief Allocates objects.
* @param n The number of objects to allocate. Has to be one!
* @param hint Ignored hint.
* @return A pointer tp the allocated elements.
*/
inline pointer allocate(std::size_t n, const_pointer hint=0);
/**
* @brief Free objects.
*
* Does not call the destructor!
* @param n The number of objects to free. Has to be one!
* @param p Pointer to the first object.
*/
inline void deallocate(pointer p, std::size_t n);
/**
* @brief Construct an object.
* @param p Pointer to the object.
* @param value The value to initialize it to.
*/
inline void construct(pointer p, const_reference value);
/**
* @brief Destroy an object without freeing memory.
* @param p Pointer to the object.
*/
inline void destroy(pointer p);
/**
* @brief Convert a reference to a pointer.
*/
inline pointer address(reference x) const { return &x; }
/**
* @brief Convert a reference to a pointer.
*/
inline const_pointer address(const_reference x) const { return &x; }
/**
* @brief Not correctly implemented, yet!
*/
inline int max_size() const throw(){ return 1;}
/**
* @brief Rebind the allocator to another type.
*/
template<class U>
struct rebind
{
typedef PoolAllocator<U,s> other;
};
/** @brief The type of the memory pool we use. */
typedef Pool<T,size> PoolType;
private:
/**
* @brief The underlying memory pool.
*/
static PoolType memoryPool_;
};
// specialization for void
template <std::size_t s>
class PoolAllocator<void,s>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference to void members are impossible.
typedef void value_type;
template <class U> struct rebind
{
typedef PoolAllocator<U,s> other;
};
template<typename T, std::size_t t>
PoolAllocator(const PoolAllocator<T,t>&)
{}
};
template<typename T1, std::size_t t1, typename T2, std::size_t t2>
bool operator==(const PoolAllocator<T1,t1>&, const PoolAllocator<T2,t2>&)
{
return false;
}
template<typename T1, std::size_t t1, typename T2, std::size_t t2>
bool operator!=(const PoolAllocator<T1,t1>&, const PoolAllocator<T2,t2>&)
{
return true;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator==(const PoolAllocator<T,t1>&, const PoolAllocator<T,t2>&)
{
return Pool<T,t1>::chunkSize == Pool<T,t2>::chunkSize;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator!=(const PoolAllocator<T,t1>&, const PoolAllocator<T,t2>&)
{
return Pool<T,t1>::chunkSize != Pool<T,t2>::chunkSize;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator==(const PoolAllocator<T,t1>&, const PoolAllocator<void,t2>&)
{
return false;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator!=(const PoolAllocator<T,t1>&, const PoolAllocator<void,t2>&)
{
return true;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator==(const PoolAllocator<void,t1>&, const PoolAllocator<T,t2>&)
{
return false;
}
template<typename T, std::size_t t1, std::size_t t2>
bool operator!=(const PoolAllocator<void,t1>&, const PoolAllocator<T,t2>&)
{
return true;
}
template<std::size_t t1, std::size_t t2>
bool operator==(const PoolAllocator<void,t1>&, const PoolAllocator<void,t2>&)
{
return true;
}
template<std::size_t t1, std::size_t t2>
bool operator!=(const PoolAllocator<void,t1>&, const PoolAllocator<void,t2>&)
{
return false;
}
template<class T, std::size_t S>
inline Pool<T,S>::Pool()
:head_(0), chunks_(0)//, allocated_(0)
{
dune_static_assert(sizeof(T)<=unionSize, "Library Error: type T is too big");
dune_static_assert(sizeof(Reference)<=unionSize, "Library Error: type of referene is too big");
dune_static_assert(unionSize<=alignedSize, "Library Error: alignedSize too small");
dune_static_assert(sizeof(T)<=chunkSize, "Library Error: chunkSize must be able to hold at least one value");
dune_static_assert(sizeof(Reference)<=chunkSize, "Library Error: chunkSize must be able to hold at least one reference");
dune_static_assert((chunkSize - (alignment - 1)) % alignment == 0, "Library Error: compiler cannot calculate!");
dune_static_assert(elements>=1, "Library Error: we need to hold at least one element!");
dune_static_assert(elements*alignedSize<=chunkSize, "Library Error: aligned elements must fit into chuck!");
/* std::cout<<"s= "<<S<<" : T: "<<sizeof(T)<<" Reference: "<<sizeof(Reference)<<" union: "<<unionSize<<" alignment: "<<alignment<<
"aligned: "<<alignedSize<<" chunk: "<< chunkSize<<" elements: "<<elements<<std::endl;*/
}
template<class T, std::size_t S>
inline Pool<T,S>::~Pool()
{
/*
if(allocated_!=0)
std::cerr<<"There are still "<<allocated_<<" allocated elements by the Pool<"<<typeid(T).name()<<","<<S<<"> "
<<static_cast<void*>(this)<<"! This is a memory leak and might result in segfaults"
<<std::endl;
*/
// delete the allocated chunks.
Chunk *current=chunks_;
while(current!=0)
{
Chunk *tmp = current;
current = current->next_;
delete tmp;
}
}
template<class T, std::size_t S>
inline void Pool<T,S>::print(std::ostream& os)
{
Chunk* current=chunks_;
while(current){
os<<current<<" ";
current=current->next_;
}
os<<current<<" ";
}
template<class T, std::size_t S>
inline void Pool<T,S>::grow()
{
Chunk *newChunk = new Chunk;
newChunk->next_ = chunks_;
chunks_ = newChunk;
char* start = chunks_->memory_;
char* last = &start[elements*alignedSize];
Reference* ref = new (start) (Reference);
// grow is only called if head==0,
assert(!head_);
head_ = ref;
for(char* element=start+alignedSize; element<last; element=element+alignedSize){
Reference* next = new (element) (Reference);
ref->next_ = next;
ref = next;
}
ref->next_=0;
}
template<class T, std::size_t S>
inline void Pool<T,S>::free(void* b)
{
if(b){
Reference* freed = static_cast<Reference*>(b);
freed->next_ = head_;
head_ = freed;
//--allocated_;
}else
std::cerr<< "Tried to free null pointer! "<<b<<std::endl;
}
template<class T, std::size_t S>
inline void* Pool<T,S>::allocate()
{
if(!head_)
grow();
Reference* p = head_;
head_ = p->next_;
//++allocated_;
return p;
}
template<class T, std::size_t s>
typename PoolAllocator<T,s>::PoolType PoolAllocator<T,s>::memoryPool_;
template<class T, std::size_t s>
inline PoolAllocator<T,s>::PoolAllocator()
{ }
template<class T, std::size_t s>
inline typename PoolAllocator<T,s>::pointer
PoolAllocator<T,s>::allocate(std::size_t n, const_pointer hint)
{
if(n==1)
return static_cast<T*>(memoryPool_.allocate());
else
throw std::bad_alloc();
}
template<class T, std::size_t s>
inline void PoolAllocator<T,s>::deallocate(pointer p, std::size_t n)
{
for(size_t i=0; i<n; i++)
memoryPool_.free(p++);
}
template<class T, std::size_t s>
inline void PoolAllocator<T,s>::construct(pointer p, const_reference value)
{
::new (static_cast<void*>(p)) T(value);
}
template<class T, std::size_t s>
inline void PoolAllocator<T,s>::destroy(pointer p)
{
p->~T();
}
/** @} */
}
#endif
|